-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·130 lines (120 loc) · 3.73 KB
/
part_b.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
import functools
import math
import utils
from year_2017.day_13 import part_a
class Challenge(utils.BaseChallenge):
part_a_for_testing = part_a
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
3913186
"""
return FirewallExtended.from_firewall_text(_input)\
.get_minimum_safe_delay()
class FirewallExtended(part_a.Firewall):
def get_minimum_safe_delay(self):
"""
>>> FirewallExtended.from_firewall_text(
... "0: 3\\n"
... "1: 2\\n"
... "4: 4\\n"
... "6: 4\\n"
... ).get_minimum_safe_delay()
10
"""
scanner_lengths = {
layer: self.get_scanner_length(_range)
for layer, _range in self.layers.items()
}
scanner_period = functools.reduce(
math.lcm, set(scanner_lengths.values()))
scanners_0_steps = [
range((length - layer) % length, scanner_period, length)
for layer, length in scanner_lengths.items()
]
safe_delays = (
delay
for delay in range(scanner_period)
if not any(
delay in scanner_0_steps
for scanner_0_steps in scanners_0_steps
)
)
return next(safe_delays)
def delay(self, count):
"""
>>> print(FirewallExtended.from_firewall_text(
... "0: 3\\n"
... "1: 2\\n"
... "4: 4\\n"
... "6: 4\\n"
... ).delay(0).show())
0 1 2 3 4 5 6
[S] [S] ... ... [S] ... [S]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ]
>>> print(FirewallExtended.from_firewall_text(
... "0: 3\\n"
... "1: 2\\n"
... "4: 4\\n"
... "6: 4\\n"
... ).delay(1).show())
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
>>> print(FirewallExtended.from_firewall_text(
... "0: 3\\n"
... "1: 2\\n"
... "4: 4\\n"
... "6: 4\\n"
... ).delay(3).show())
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... [ ]
[S] [S] [ ] [ ]
[ ] [ ] [ ]
[S] [S]
>>> print(FirewallExtended.from_firewall_text(
... "0: 3\\n"
... "1: 2\\n"
... "4: 4\\n"
... "6: 4\\n"
... ).delay(4).show())
0 1 2 3 4 5 6
[S] [S] ... ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[ ] [S] [S]
[ ] [ ]
>>> print(FirewallExtended.from_firewall_text(
... "0: 3\\n"
... "1: 2\\n"
... "4: 4\\n"
... "6: 4\\n"
... ).delay(5).show())
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
>>> print(FirewallExtended.from_firewall_text(
... "0: 3\\n"
... "1: 2\\n"
... "4: 4\\n"
... "6: 4\\n"
... ).delay(10).show())
0 1 2 3 4 5 6
[ ] [S] ... ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
"""
packet = self.packet
for _ in range(count):
self.step()
self.packet = packet
return self
Challenge.main()
challenge = Challenge()