Skip to content

Commit f7e470a

Browse files
committed
Added day 2021-06
1 parent 3c96b32 commit f7e470a

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

2021/06-Lanternfish.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """3,4,3,1,2""",
38+
"expected": ["26 @ day 18, 5934 @ day 80", "26984457539"],
39+
}
40+
41+
test = "real"
42+
input_file = os.path.join(
43+
os.path.dirname(__file__),
44+
"Inputs",
45+
os.path.basename(__file__).replace(".py", ".txt"),
46+
)
47+
test_data[test] = {
48+
"input": open(input_file, "r+").read(),
49+
"expected": ["396210", "1770823541496"],
50+
}
51+
52+
53+
# -------------------------------- Control program execution ------------------------- #
54+
55+
case_to_test = "real"
56+
part_to_test = 2
57+
58+
# -------------------------------- Initialize some variables ------------------------- #
59+
60+
puzzle_input = test_data[case_to_test]["input"]
61+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
62+
puzzle_actual_result = "Unknown"
63+
64+
65+
# -------------------------------- Actual code execution ----------------------------- #
66+
67+
# Conver integer to 36-character binary
68+
# str_value = "{0:>036b}".format(value)
69+
# Convert binary string to number
70+
# value = int(str_value, 2)
71+
72+
fishes = defaultdict(lambda: 0)
73+
new_fish_plus_1 = defaultdict(lambda: 0)
74+
new_fish_plus_2 = defaultdict(lambda: 0)
75+
76+
77+
if part_to_test == 1:
78+
nb_gen = 80
79+
else:
80+
nb_gen = 256
81+
for fish in ints(puzzle_input):
82+
fishes[fish] += 1
83+
84+
for day in range(nb_gen + 1):
85+
new_fish = defaultdict(lambda: 0)
86+
for i in fishes:
87+
if day % 7 == i:
88+
new_fish[(day + 2) % 7] += fishes[day % 7]
89+
90+
for i in new_fish_plus_2:
91+
fishes[i] += new_fish_plus_2[i]
92+
new_fish_plus_2 = new_fish_plus_1.copy()
93+
new_fish_plus_1 = new_fish.copy()
94+
95+
print("End of day", day, ":", sum(fishes.values()) + sum(new_fish_plus_2.values()))
96+
97+
puzzle_actual_result = sum(fishes.values()) + sum(new_fish_plus_2.values())
98+
99+
100+
# -------------------------------- Outputs / results --------------------------------- #
101+
102+
print("Case :", case_to_test, "- Part", part_to_test)
103+
print("Expected result : " + str(puzzle_expected_result))
104+
print("Actual result : " + str(puzzle_actual_result))
105+
# Date created: 2021-12-06 08:17:14.668559
106+
# Part 1: 2021-12-06 09:36:08 (60 min for meetings + shower)
107+
# Part 2: 2021-12-06 09:37:07 (60 min for meetings + shower)

0 commit comments

Comments
 (0)