Skip to content

Commit 12323f6

Browse files
committed
Solve puzzles for day 6
1 parent 5e6176d commit 12323f6

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 120

06/lanternfish.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#! /usr/bin/env python3
2+
from pathlib import Path
3+
from copy import deepcopy
4+
5+
########################################
6+
# Naive approach (not correct!!!)
7+
########################################
8+
9+
def increment_day(lanternfish_timers: "list[int]", regular_cycle: int = 6, new_cycle: int = 8) -> "list[int]":
10+
"""Increment the day for a group of reproducing lantern fish
11+
12+
Args:
13+
lanternfish_timers (list[int]): The current reproductive countdown timers for the lantern fish.
14+
regular_cycle (int, optional): The normal countdown timer for a lantern fish. Defaults to 6.
15+
new_cycle (int, optional): The initial countdown timer for a lantern fish that has just been born. Defaults to
16+
8.
17+
18+
Returns:
19+
list[int]: The new list of lantern fish countdown timers after incrementing the day.
20+
"""
21+
new_timers = deepcopy(lanternfish_timers)
22+
for i, timer in enumerate(lanternfish_timers):
23+
if timer == 0:
24+
new_timers[i] = regular_cycle
25+
new_timers.append(new_cycle)
26+
else:
27+
new_timers[i] -= 1
28+
29+
return new_timers
30+
31+
32+
def simulate_lanternfish_growth(initial_timer_state: "list[int]", simulation_days: int, regular_cycle: int = 6,
33+
new_cycle: int = 8) -> int:
34+
"""Simulate the lanternfish over a given number of days and determine the final population size.
35+
36+
Args:
37+
initial_timer_state (list[int]): The initial reproductive countdown timers for the lantern fish.
38+
simulation_days (int): The number of days to run the simulation
39+
regular_cycle (int, optional): The normal countdown timer for a lantern fish. Defaults to 6.
40+
new_cycle (int, optional): The initial countdown timer for a lantern fish that has just been born. Defaults to
41+
8.
42+
43+
Returns:
44+
int: The number of lantern fish at the end of the simulation.
45+
"""
46+
lanternfish_timers = initial_timer_state
47+
for i in range(simulation_days):
48+
lanternfish_timers = increment_day(lanternfish_timers=lanternfish_timers, regular_cycle=regular_cycle,
49+
new_cycle=new_cycle)
50+
return len(lanternfish_timers)
51+
52+
53+
########################################
54+
# Correct approach
55+
########################################
56+
class LanternfishTracker():
57+
def __init__(self, timer_list: "list[int]"):
58+
self.timers = [0 for x in range(9)]
59+
for time in timer_list:
60+
self.timers[time] += 1
61+
62+
def increment_day(self) -> None:
63+
lanternfish = self.timers.pop(0)
64+
self.timers[6] += lanternfish
65+
self.timers.append(lanternfish)
66+
67+
def simulate_growth(self, days: int) -> int:
68+
[self.increment_day() for i in range(days)]
69+
return sum(self.timers)
70+
71+
72+
def puzzle_1(inputfile: Path, diagonal=False) -> int:
73+
with open(inputfile, 'r') as file:
74+
initial_state = [int(x) for x in file.readline().strip('\n').split(sep=',')]
75+
return simulate_lanternfish_growth(initial_timer_state=initial_state, simulation_days=80)
76+
77+
78+
def puzzle_2(inputfile: Path) -> int:
79+
with open(inputfile, 'r') as file:
80+
initial_state = [int(x) for x in file.readline().strip('\n').split(sep=',')]
81+
tracker = LanternfishTracker(timer_list=initial_state)
82+
return tracker.simulate_growth(days=256)
83+
84+
85+
def main():
86+
inputfile = Path("puzzle_1_input.txt")
87+
print(f"Puzzle 1 Answer = {puzzle_1(inputfile)}")
88+
print(f"Puzzle 2 Answer = {puzzle_2(inputfile)}")
89+
90+
91+
if __name__ == "__main__":
92+
main()

06/puzzle_1_input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4,2,4,1,5,1,2,2,4,1,1,2,2,2,4,4,1,2,1,1,4,1,2,1,2,2,2,2,5,2,2,3,1,4,4,4,1,2,3,4,4,5,4,3,5,1,2,5,1,1,5,5,1,4,4,5,1,3,1,4,5,5,5,4,1,2,3,4,2,1,2,1,2,2,1,5,5,1,1,1,1,5,2,2,2,4,2,4,2,4,2,1,2,1,2,4,2,4,1,3,5,5,2,4,4,2,2,2,2,3,3,2,1,1,1,1,4,3,2,5,4,3,5,3,1,5,5,2,4,1,1,2,1,3,5,1,5,3,1,3,1,4,5,1,1,3,2,1,1,1,5,2,1,2,4,2,3,3,2,3,5,1,5,1,2,1,5,2,4,1,2,4,4,1,5,1,1,5,2,2,5,5,3,1,2,2,1,1,4,1,5,4,5,5,2,2,1,1,2,5,4,3,2,2,5,4,2,5,4,4,2,3,1,1,1,5,5,4,5,3,2,5,3,4,5,1,4,1,1,3,4,4,1,1,5,1,4,1,2,1,4,1,1,3,1,5,2,5,1,5,2,5,2,5,4,1,1,4,4,2,3,1,5,2,5,1,5,2,1,1,1,2,1,1,1,4,4,5,4,4,1,4,2,2,2,5,3,2,4,4,5,5,1,1,1,1,3,1,2,1

06/puzzle_1_test_input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,4,3,1,2

06/test_lanternfish.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from lanternfish import increment_day, simulate_lanternfish_growth, LanternfishTracker
2+
from lanternfish import puzzle_1, puzzle_2, Path
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def initial_state():
8+
return [3, 4, 3, 1, 2]
9+
10+
11+
@pytest.fixture
12+
def lanternfish_tracker(initial_state):
13+
return LanternfishTracker(timer_list=initial_state)
14+
15+
16+
def test_increment_day(initial_state):
17+
day_1_state = [2, 3, 2, 0, 1]
18+
day_2_state = [1, 2, 1, 6, 0, 8]
19+
assert increment_day(lanternfish_timers=initial_state) == day_1_state
20+
assert increment_day(lanternfish_timers=day_1_state) == day_2_state
21+
22+
23+
def test_simulate_lanternfish_growth(initial_state):
24+
assert simulate_lanternfish_growth(initial_timer_state=initial_state, simulation_days=80) == 5934
25+
26+
27+
def test_puzzle_1():
28+
assert puzzle_1(Path(__file__).parent / "puzzle_1_test_input.txt") == 5934
29+
30+
31+
def test_long_simulation(lanternfish_tracker):
32+
assert lanternfish_tracker.simulate_growth(days=256) == 26984457539
33+
34+
35+
def test_puzzle_2():
36+
assert puzzle_2(Path(__file__).parent / "puzzle_1_test_input.txt") == 26984457539

0 commit comments

Comments
 (0)