-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday13.py
76 lines (54 loc) · 1.84 KB
/
day13.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import progressbar
def parse_input(filename: str) -> dict:
lines = [line.strip() for line in open(filename).readlines()]
depths = {}
for line in lines:
layer, depth = line.split(': ')
depths[int(layer)] = int(depth)
return depths
def play(depths: dict) -> int:
current = {}
directions = {}
for depth in depths:
current[depth] = 0
directions[depth] = 1
caught = []
max_layer = max(depths)
for pico_second in range(max_layer + 1):
if pico_second in current and current[pico_second] == 0:
caught.append(pico_second)
for depth in current:
if current[depth] == depths[depth] - 1 or (current[depth] == 0 and pico_second != 0):
directions[depth] = -directions[depth]
current[depth] += directions[depth]
return sum([c * depths[c] for c in caught])
def play_simplified(depths: dict) -> int:
rounds = {}
for depth in depths:
rounds[depth] = (depths[depth] - 1) * 2
caught = [depth for depth in rounds if depth % rounds[depth] == 0]
return sum([c * depths[c] for c in caught])
def part1(depths: dict) -> int:
# return play(depths)
return play_simplified(depths)
def play_with_delay(rounds: dict, delay: int) -> bool:
for depth in rounds:
if (depth + delay) % rounds[depth] == 0:
return True
return False
def part2(depths: dict) -> int:
rounds = {}
for depth in depths:
rounds[depth] = (depths[depth] - 1) * 2
with progressbar.ProgressBar() as p:
delay = 0
while play_with_delay(rounds, delay):
delay += 1
p.update(delay)
return delay
def main():
depths = parse_input('2017/input/day13.txt')
print(f'Part 1: {part1(depths)}')
print(f'Part 2: {part2(depths)}')
if __name__ == "__main__":
main()