Skip to content

Commit 2ec9114

Browse files
committed
Added days 2019-06 and 2019-07
1 parent d1bc9e6 commit 2ec9114

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

2019/06-Universal Orbit Map.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, pathfinding
3+
4+
from complex_utils import *
5+
from tree import Tree
6+
7+
test_data = {}
8+
9+
test = 1
10+
test_data[test] = {
11+
"input": """COM)B
12+
B)C
13+
C)D
14+
D)E
15+
E)F
16+
B)G
17+
G)H
18+
D)I
19+
E)J
20+
J)K
21+
K)L
22+
K)YOU
23+
I)SAN""",
24+
"expected": ["42 (without SAN and YOU), 54 (with)", "4"],
25+
}
26+
27+
test = "real"
28+
input_file = os.path.join(
29+
os.path.dirname(__file__),
30+
"Inputs",
31+
os.path.basename(__file__).replace(".py", ".txt"),
32+
)
33+
test_data[test] = {
34+
"input": open(input_file, "r+").read().strip(),
35+
"expected": ["151345", "391"],
36+
}
37+
38+
# -------------------------------- Control program execution ------------------------- #
39+
40+
case_to_test = "real"
41+
part_to_test = 2
42+
43+
# -------------------------------- Initialize some variables ------------------------- #
44+
45+
puzzle_input = test_data[case_to_test]["input"]
46+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
47+
puzzle_actual_result = "Unknown"
48+
49+
50+
# -------------------------------- Actual code execution ----------------------------- #
51+
52+
all_nodes = {"COM": Tree("COM")}
53+
for string in puzzle_input.split("\n"):
54+
orbitee, orbiter = string.split(")")
55+
if orbitee not in all_nodes:
56+
all_nodes[orbitee] = Tree(orbitee)
57+
if orbiter not in all_nodes:
58+
all_nodes[orbiter] = Tree(orbiter)
59+
60+
all_nodes[orbitee].add_child(all_nodes[orbiter])
61+
all_nodes[orbiter].parent = all_nodes[orbitee]
62+
63+
if part_to_test == 1:
64+
nb_orbits = 0
65+
for node in all_nodes.values():
66+
nb_orbits += node.count_descendants()
67+
68+
puzzle_actual_result = nb_orbits
69+
70+
71+
else:
72+
puzzle_actual_result = (
73+
all_nodes["SAN"].get_degree_of_separation(all_nodes["YOU"]) - 2
74+
)
75+
76+
77+
# -------------------------------- Outputs / results --------------------------------- #
78+
79+
print("Expected result : " + str(puzzle_expected_result))
80+
print("Actual result : " + str(puzzle_actual_result))

2019/07-Amplification Circuit.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, pathfinding, itertools
3+
4+
from complex_utils import *
5+
from IntCode import *
6+
7+
test_data = {}
8+
9+
test = 1
10+
test_data[test] = {
11+
"input": """3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0""",
12+
"expected": ["43210 (from phase setting sequence 4,3,2,1,0)", "Unknown"],
13+
}
14+
15+
test += 1
16+
test_data[test] = {
17+
"input": """3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0""",
18+
"expected": ["54321 (from phase setting sequence 0,1,2,3,4)", "Unknown"],
19+
}
20+
21+
test += 1
22+
test_data[test] = {
23+
"input": """3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,
24+
-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,
25+
53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10""",
26+
"expected": ["Unknown", "18216 (from phase setting sequence 9,7,8,5,6)"],
27+
}
28+
29+
test = "real"
30+
input_file = os.path.join(
31+
os.path.dirname(__file__),
32+
"Inputs",
33+
os.path.basename(__file__).replace(".py", ".txt"),
34+
)
35+
test_data[test] = {
36+
"input": open(input_file, "r+").read().strip(),
37+
"expected": ["929800", "15432220"],
38+
}
39+
40+
# -------------------------------- Control program execution ------------------------- #
41+
42+
case_to_test = "real"
43+
part_to_test = 2
44+
45+
# -------------------------------- Initialize some variables ------------------------- #
46+
47+
puzzle_input = test_data[case_to_test]["input"]
48+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
49+
puzzle_actual_result = "Unknown"
50+
51+
52+
# -------------------------------- Actual code execution ----------------------------- #
53+
54+
if part_to_test == 1:
55+
max_signal = 0
56+
for settings in itertools.permutations("01234"):
57+
amplifiers = [IntCode(puzzle_input, i) for i in range(5)]
58+
for i in range(5):
59+
amplifiers[i].add_input(int(settings[i]))
60+
amplifiers[0].add_input(0)
61+
62+
amplifiers[0].run()
63+
for i in range(1, 5):
64+
amplifiers[i].add_input(amplifiers[i - 1].outputs[-1])
65+
amplifiers[i].run()
66+
67+
max_signal = max(max_signal, amplifiers[4].outputs[-1])
68+
69+
puzzle_actual_result = max_signal
70+
71+
72+
else:
73+
max_signal = 0
74+
for settings in itertools.permutations("56789"):
75+
amplifiers = [IntCode(puzzle_input, i) for i in range(5)]
76+
for i in range(5):
77+
amplifiers[i].add_input(int(settings[i]))
78+
amplifiers[0].add_input(0)
79+
80+
while not all([x.state == "Stopped" for x in amplifiers]):
81+
for i in range(0, 5):
82+
if len(amplifiers[i - 1].outputs) > 0:
83+
amplifiers[i].add_input(amplifiers[i - 1].outputs)
84+
amplifiers[i - 1].outputs = []
85+
amplifiers[i].restart()
86+
amplifiers[i].run()
87+
88+
max_signal = max(max_signal, amplifiers[4].outputs[-1])
89+
90+
puzzle_actual_result = max_signal
91+
92+
93+
# -------------------------------- Outputs / results --------------------------------- #
94+
95+
print("Expected result : " + str(puzzle_expected_result))
96+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy