summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2020-12-14 11:35:46 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2020-12-14 11:35:46 +0100
commit9d38cb75c2e23a173adc7e390777c50361c31436 (patch)
tree361e34d71d1bac3cc57c5d5a3ec2ab3d4d42ae3c
parent2dae8ff6da421943e26af83866765a024c8f6650 (diff)
download2021-9d38cb75c2e23a173adc7e390777c50361c31436.tar.gz
2021-9d38cb75c2e23a173adc7e390777c50361c31436.tar.bz2
2021-9d38cb75c2e23a173adc7e390777c50361c31436.zip
Added day 8
-rw-r--r--day8/__init__.py75
-rw-r--r--day8/example.txt9
-rw-r--r--day8/input.txt624
3 files changed, 708 insertions, 0 deletions
diff --git a/day8/__init__.py b/day8/__init__.py
new file mode 100644
index 0000000..c6d086a
--- /dev/null
+++ b/day8/__init__.py
@@ -0,0 +1,75 @@
1from copy import copy
2from typing import Generator, Iterator, Any, List, Tuple
3
4from aoc import BaseAssignment
5
6
7class Assignment(BaseAssignment):
8 @staticmethod
9 def acc(line, accumulated_value, value):
10 return line + 1, accumulated_value + value
11
12 @staticmethod
13 def jmp(line, accumulated_value, value):
14 return line + value, accumulated_value
15
16 @staticmethod
17 def nop(line, accumulated_value, value):
18 return line + 1, accumulated_value
19
20 def read_input(self, example=False) -> List:
21 return list(super().read_input(example))
22
23 def parse_item(self, item: str) -> Tuple:
24 instruction, value = item.split(' ')
25
26 return getattr(self, instruction), int(value)
27
28 def run(self, input: List) -> int:
29 executed_lines = set()
30 accumulated_value = 0
31 line = 0
32
33 while True:
34 if line in executed_lines:
35 raise Exception(
36 f'Loop detected at line: {line}. '
37 f'Accumulated value: {accumulated_value}'
38 )
39
40 try:
41 instruction, value = input[line]
42 except IndexError:
43 break
44
45 executed_lines.add(line)
46 line, accumulated_value = instruction(
47 line, accumulated_value, value
48 )
49
50 return accumulated_value
51
52
53class AssignmentOne(Assignment):
54 pass
55
56
57class AssignmentTwo(Assignment):
58 def patch_instructions(self, input: List) -> Generator:
59 for index, (instruction, value) in enumerate(input):
60 if instruction is self.acc:
61 continue
62
63 patched_input = copy(input)
64 patched_input[index] = (
65 self.nop if instruction is self.jmp else self.jmp,
66 value
67 )
68 yield patched_input
69
70 def run(self, input: List) -> int:
71 for patched_input in self.patch_instructions(input):
72 try:
73 return super().run(patched_input)
74 except Exception:
75 continue
diff --git a/day8/example.txt b/day8/example.txt
new file mode 100644
index 0000000..6fee349
--- /dev/null
+++ b/day8/example.txt
@@ -0,0 +1,9 @@
1nop +0
2acc +1
3jmp +4
4acc +3
5jmp -3
6acc -99
7acc +1
8jmp -4
9acc +6 \ No newline at end of file
diff --git a/day8/input.txt b/day8/input.txt
new file mode 100644
index 0000000..340826c
--- /dev/null
+++ b/day8/input.txt
@@ -0,0 +1,624 @@
1acc -7
2acc +6
3acc +4
4nop +191
5jmp +199
6acc +44
7acc -9
8jmp +505
9acc -12
10acc +45
11jmp +204
12jmp +129
13acc +17
14nop +287
15jmp +584
16acc +16
17jmp +363
18acc +4
19nop +142
20acc +34
21nop +345
22jmp +522
23jmp +53
24acc -10
25jmp +524
26jmp +492
27jmp +319
28acc -9
29jmp +550
30acc -19
31jmp +15
32acc +24
33jmp +30
34acc -19
35acc +12
36acc -2
37jmp +274
38nop +91
39acc +10
40acc +4
41jmp +501
42acc +49
43acc +29
44jmp +488
45jmp +504
46jmp +277
47acc +20
48acc +34
49jmp -40
50acc +10
51acc -4
52acc -19
53acc +38
54jmp +239
55acc -16
56acc -3
57nop +513
58jmp +526
59jmp +131
60nop +539
61acc -11
62jmp +470
63acc +30
64jmp +166
65acc +17
66acc -16
67nop +315
68jmp +364
69acc +15
70nop -61
71acc -12
72nop +147
73jmp -31
74acc -9
75jmp +324
76acc +0
77jmp +1
78jmp +321
79acc +0
80acc +6
81acc -17
82acc +13
83jmp +461
84jmp +184
85acc +22
86jmp +182
87jmp +504
88nop +131
89acc +12
90acc -6
91acc +29
92jmp +187
93acc +17
94jmp +67
95jmp -2
96acc +50
97acc +17
98jmp +442
99acc +8
100nop +146
101acc -12
102acc +32
103jmp +237
104jmp +1
105acc +34
106acc +1
107acc +18
108jmp +274
109acc +17
110acc -12
111jmp +282
112acc +49
113acc +11
114acc +28
115acc +40
116jmp +79
117acc +19
118acc -8
119nop +87
120jmp +347
121acc +48
122nop +189
123jmp +419
124acc +31
125jmp +1
126acc +31
127jmp +1
128jmp -94
129nop -45
130nop +412
131acc -14
132acc +35
133jmp -49
134jmp +177
135jmp +127
136jmp +360
137jmp +114
138acc -11
139nop +248
140jmp -64
141acc +31
142acc +23
143acc +4
144nop +110
145jmp +61
146acc +45
147nop +444
148jmp +218
149jmp -131
150acc +36
151jmp -142
152nop +361
153acc -3
154acc +6
155jmp +161
156acc +24
157acc -7
158acc +4
159acc +31
160jmp +91
161jmp -20
162jmp +1
163nop -11
164jmp -146
165acc +25
166acc +33
167jmp +52
168acc -7