-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday14.py
53 lines (42 loc) · 1.54 KB
/
day14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from progressbar import ProgressBar
def part1(puzzle_input: int) -> str:
recipes = [3, 7]
elf1, elf2 = 0, 1
while len(recipes) < (puzzle_input + 10):
recipe_sum = recipes[elf1] + recipes[elf2]
tens = recipe_sum // 10
ones = recipe_sum % 10
if tens > 0:
recipes.append(tens)
recipes.append(ones)
elf1 = (elf1 + recipes[elf1] + 1) % len(recipes)
elf2 = (elf2 + recipes[elf2] + 1) % len(recipes)
return ''.join([str(d) for d in recipes[puzzle_input: puzzle_input + 10]])
def part2(puzzle_input: str) -> int:
puzzle_input_len = len(puzzle_input)
recipes = [3, 7]
elf1, elf2 = 0, 1
i = 0
with ProgressBar() as p:
while puzzle_input not in ''.join(str(d) for d in recipes[-(puzzle_input_len + 1):]):
recipe_sum = recipes[elf1] + recipes[elf2]
tens = recipe_sum // 10
ones = recipe_sum % 10
if tens > 0:
recipes.append(tens)
recipes.append(ones)
elf1 = (elf1 + recipes[elf1] + 1) % len(recipes)
elf2 = (elf2 + recipes[elf2] + 1) % len(recipes)
i += 1
p.update(i)
if ''.join(str(d) for d in recipes[-puzzle_input_len:]) == puzzle_input:
return len(recipes) - puzzle_input_len
else:
return len(recipes) - puzzle_input_len - 1
def main():
puzzle_input = 320851
print(f'Part 1: {part1(puzzle_input)}')
puzzle_input = '320851'
print(f'Part 2: {part2(puzzle_input)}')
if __name__ == "__main__":
main()