Skip to content

Commit 15f4d2b

Browse files
committed
Added days 2019-08 and 2019-09
1 parent 2ec9114 commit 15f4d2b

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

2019/08-Space Image Format.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, pathfinding
3+
4+
from complex_utils import *
5+
6+
test_data = {}
7+
8+
test = 1
9+
test_data[test] = {
10+
"input": """""",
11+
"expected": ["Unknown", "Unknown"],
12+
}
13+
14+
test = "real"
15+
input_file = os.path.join(
16+
os.path.dirname(__file__),
17+
"Inputs",
18+
os.path.basename(__file__).replace(".py", ".txt"),
19+
)
20+
test_data[test] = {
21+
"input": open(input_file, "r+").read().strip(),
22+
"expected": ["2480", "ZYBLH"],
23+
}
24+
25+
# -------------------------------- Control program execution ------------------------- #
26+
27+
case_to_test = "real"
28+
part_to_test = 2
29+
30+
# -------------------------------- Initialize some variables ------------------------- #
31+
32+
puzzle_input = test_data[case_to_test]["input"]
33+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
34+
puzzle_actual_result = "Unknown"
35+
36+
37+
# -------------------------------- Actual code execution ----------------------------- #
38+
39+
layers = []
40+
width = 25
41+
height = 6
42+
size = width * height
43+
layers = [
44+
puzzle_input[i * size : i * size + size] for i in range(len(puzzle_input) // size)
45+
]
46+
47+
if part_to_test == 1:
48+
layers.sort(key=lambda a: a.count("0"))
49+
fewest_zero = layers[0]
50+
puzzle_actual_result = fewest_zero.count("1") * fewest_zero.count("2")
51+
52+
53+
else:
54+
image = ["2"] * size
55+
for layer in layers:
56+
image = [image[i] if image[i] != "2" else layer[i] for i in range(len(image))]
57+
58+
output = ""
59+
for row in range(height):
60+
output += "".join(image[row * width : (row + 1) * width])
61+
output += "\n"
62+
63+
output = "\n" + output.replace("2", "x").replace("1", "#").replace("0", " ")
64+
puzzle_actual_result = output
65+
66+
67+
# -------------------------------- Outputs / results --------------------------------- #
68+
69+
print("Expected result : " + str(puzzle_expected_result))
70+
print("Actual result : " + str(puzzle_actual_result))

2019/09-Sensor Boost.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, pathfinding
3+
4+
from complex_utils import *
5+
from IntCode import *
6+
7+
test_data = {}
8+
9+
test = 1
10+
test_data[test] = {
11+
"input": """109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99""",
12+
"expected": [
13+
"109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99",
14+
"Unknown",
15+
],
16+
}
17+
18+
test = "real"
19+
input_file = os.path.join(
20+
os.path.dirname(__file__),
21+
"Inputs",
22+
os.path.basename(__file__).replace(".py", ".txt"),
23+
)
24+
test_data[test] = {
25+
"input": open(input_file, "r+").read().strip(),
26+
"expected": ["3380552333", "78831"],
27+
}
28+
29+
# -------------------------------- Control program execution ------------------------- #
30+
31+
case_to_test = "real"
32+
part_to_test = 2
33+
34+
# -------------------------------- Initialize some variables ------------------------- #
35+
36+
puzzle_input = test_data[case_to_test]["input"]
37+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
38+
puzzle_actual_result = "Unknown"
39+
40+
41+
# -------------------------------- Actual code execution ----------------------------- #
42+
43+
computer = IntCode(puzzle_input)
44+
computer.add_input(part_to_test)
45+
computer.run()
46+
if len(computer.outputs) == 1:
47+
puzzle_actual_result = computer.outputs[0]
48+
else:
49+
puzzle_actual_result = "Errors on opcodes : " + ",".join(map(str, computer.outputs))
50+
51+
52+
# -------------------------------- Outputs / results --------------------------------- #
53+
54+
print("Expected result : " + str(puzzle_expected_result))
55+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)