-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
44 lines (33 loc) · 1.04 KB
/
__init__.py
File metadata and controls
44 lines (33 loc) · 1.04 KB
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
from typing import List, Dict
from aocpy import BaseChallenge
def parse(instr: str) -> List[int]:
return list(map(int, instr.strip().split(",")))
def count_fish_by_timer(all_fish: List[int]) -> Dict[int, int]:
m = {}
for fish in all_fish:
m[fish] = m.get(fish, 0) + 1
return m
def iterate_fish_once(sum_dict: Dict[int, int]):
nm = {}
# shift back
for i in range(9):
nm[i - 1] = sum_dict.get(i, 0)
# apply -1 value
nm[6] = nm.get(6, 0) + nm.get(-1, 0)
nm[8] = nm.get(-1, 0)
del nm[-1]
return nm
def sum_of_fish_after_n(fish: List[int], n: int) -> int:
by_timer = count_fish_by_timer(fish)
for _ in range(n):
by_timer = iterate_fish_once(by_timer)
return sum([by_timer[k] for k in by_timer])
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
fish = parse(instr)
return sum_of_fish_after_n(fish, 80)
@staticmethod
def two(instr: str) -> int:
fish = parse(instr)
return sum_of_fish_after_n(fish, 256)