Content-Length: 517922 | pFad | http://github.com/Piratmac/adventofcode-python/commit/9726372195c6700db1be3b84b2aff0202a25be4e

1B Added day 2020-08 · Piratmac/adventofcode-python@9726372 · GitHub
Skip to content

Commit 9726372

Browse files
committed
Added day 2020-08
1 parent c14864c commit 9726372

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

2020/08-Handheld Halting.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools, copy
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """nop +0
38+
acc +1
39+
jmp +4
40+
acc +3
41+
jmp -3
42+
acc -99
43+
acc +1
44+
jmp -4
45+
acc +6""",
46+
"expected": ["5", "8"],
47+
}
48+
49+
test = "real"
50+
input_file = os.path.join(
51+
os.path.dirname(__file__),
52+
"Inputs",
53+
os.path.basename(__file__).replace(".py", ".txt"),
54+
)
55+
test_data[test] = {
56+
"input": open(input_file, "r+").read(),
57+
"expected": ["1134", "1205"],
58+
}
59+
60+
61+
# -------------------------------- Control program execution ------------------------- #
62+
63+
case_to_test = 1
64+
part_to_test = 2
65+
66+
# -------------------------------- Initialize some variables ------------------------- #
67+
68+
puzzle_input = test_data[case_to_test]["input"]
69+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
70+
puzzle_actual_result = "Unknown"
71+
72+
73+
# -------------------------------- Actual code execution ----------------------------- #
74+
75+
76+
class Program:
77+
def __init__(self, instructions):
78+
self.instructions = [
79+
[x.split(" ")[0], int(x.split(" ")[1])] for x in instructions.split("\n")
80+
]
81+
self.accumulator = 0
82+
self.current_line = 0
83+
self.operations = {
84+
"nop": self.nop,
85+
"acc": self.acc,
86+
"jmp": self.jmp,
87+
}
88+
89+
def run(self):
90+
while current_line <= len(self.operations):
91+
self.run_once()
92+
93+
def run_once(self):
94+
instr = self.instructions[self.current_line]
95+
print("Before", self.current_line, self.accumulator, instr)
96+
self.operations[instr[0]](instr)
97+
98+
def nop(self, instr):
99+
self.current_line += 1
100+
pass
101+
102+
def acc(self, instr):
103+
self.current_line += 1
104+
self.accumulator += instr[1]
105+
106+
def jmp(self, instr):
107+
self.current_line += instr[1]
108+
109+
110+
if part_to_test == 1:
111+
program = Program(puzzle_input)
112+
113+
visited = []
114+
while (
115+
program.current_line < len(program.instructions)
116+
and program.current_line not in visited
117+
):
118+
visited.append(program.current_line)
119+
program.run_once()
120+
121+
puzzle_actual_result = program.accumulator
122+
123+
124+
else:
125+
initial_program = Program(puzzle_input)
126+
all_nop_jmp = [
127+
i
128+
for i, instr in enumerate(initial_program.instructions)
129+
if instr[0] in ("jmp", "nop")
130+
]
131+
for val in all_nop_jmp:
132+
program = copy.deepcopy(initial_program)
133+
program.instructions[val][0] = (
134+
"nop" if program.instructions[val][0] == "jpm" else "nop"
135+
)
136+
137+
visited = []
138+
while (
139+
program.current_line < len(program.instructions)
140+
and program.current_line not in visited
141+
):
142+
visited.append(program.current_line)
143+
program.run_once()
144+
145+
if program.current_line == len(program.instructions):
146+
puzzle_actual_result = program.accumulator
147+
break
148+
149+
150+
# -------------------------------- Outputs / results --------------------------------- #
151+
152+
print("Case :", case_to_test, "- Part", part_to_test)
153+
print("Expected result : " + str(puzzle_expected_result))
154+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Piratmac/adventofcode-python/commit/9726372195c6700db1be3b84b2aff0202a25be4e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy