Skip to content

Commit 4efbb88

Browse files
committed
Added days 2019-03, 2019-04, 2019-05
1 parent 712a775 commit 4efbb88

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

2019/03-Crossed Wires.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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": """R75,D30,R83,U83,L12,D49,R71,U7,L72
11+
U62,R66,U55,R34,D71,R55,D58,R83""",
12+
"expected": ["159", "610"],
13+
}
14+
15+
test = "real"
16+
input_file = os.path.join(
17+
os.path.dirname(__file__),
18+
"Inputs",
19+
os.path.basename(__file__).replace(".py", ".txt"),
20+
)
21+
test_data[test] = {
22+
"input": open(input_file, "r+").read().strip(),
23+
"expected": ["308", "12934"],
24+
}
25+
26+
# -------------------------------- Control program execution ------------------------- #
27+
28+
case_to_test = "real"
29+
part_to_test = 2
30+
31+
# -------------------------------- Initialize some variables ------------------------- #
32+
33+
puzzle_input = test_data[case_to_test]["input"]
34+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
35+
puzzle_actual_result = "Unknown"
36+
37+
38+
# -------------------------------- Actual code execution ----------------------------- #
39+
40+
wires = []
41+
for i in range(len(puzzle_input.split("\n"))):
42+
wire = puzzle_input.split("\n")[i]
43+
position = 0
44+
wires.append(list())
45+
for line in wire.split(","):
46+
direction = {"U": north, "D": south, "L": west, "R": east}[line[0]]
47+
for step in range(int(line[1:])):
48+
position += direction
49+
wires[i].append(position)
50+
51+
common = list(set(wires[0]).intersection(set(wires[1])))
52+
53+
54+
if part_to_test == 1:
55+
common = complex_sort(common, "manhattan")
56+
puzzle_actual_result = int(manhattan_distance(0, common[0]))
57+
58+
59+
else:
60+
min_distance = 10 ** 20
61+
for spot in common:
62+
distance = (
63+
wires[0].index(spot) + wires[1].index(spot) + 2
64+
) # 2 because start is not included
65+
min_distance = min(min_distance, distance)
66+
67+
puzzle_actual_result = min_distance
68+
69+
70+
# -------------------------------- Outputs / results --------------------------------- #
71+
72+
print("Expected result : " + str(puzzle_expected_result))
73+
print("Actual result : " + str(puzzle_actual_result))

2019/04-Secure Container.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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": """112233-112233""",
11+
"expected": ["1", "Unknown"],
12+
}
13+
14+
test = "real"
15+
test_data[test] = {
16+
"input": "273025-767253",
17+
"expected": ["910", "598"],
18+
}
19+
20+
# -------------------------------- Control program execution ------------------------- #
21+
22+
case_to_test = "real"
23+
part_to_test = 2
24+
25+
# -------------------------------- Initialize some variables ------------------------- #
26+
27+
puzzle_input = test_data[case_to_test]["input"]
28+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
29+
puzzle_actual_result = "Unknown"
30+
31+
32+
# -------------------------------- Actual code execution ----------------------------- #
33+
34+
35+
def has_double(password):
36+
password = str(password)
37+
return any([True for x in "0123456789" if x + x in password])
38+
39+
40+
def numbers_increase(password):
41+
password = str(password)
42+
return all([password[i + 1] >= password[i] for i in range(len(password) - 1)])
43+
44+
45+
def larger_group_test(password):
46+
password = str(password)
47+
doubles = [x for x in "0123456789" if x * 2 in password]
48+
if not doubles:
49+
return True
50+
larger_group = [x for x in doubles for n in range(3, 7) if x * n in password]
51+
return any([x not in larger_group for x in doubles])
52+
53+
54+
if part_to_test == 1:
55+
start, end = map(int, puzzle_input.split("-"))
56+
matches = 0
57+
for i in range(start, end + 1):
58+
if has_double(i) and numbers_increase(i):
59+
matches += 1
60+
61+
puzzle_actual_result = matches
62+
63+
64+
else:
65+
start, end = map(int, puzzle_input.split("-"))
66+
matches = 0
67+
for i in range(start, end + 1):
68+
if has_double(i) and numbers_increase(i) and larger_group_test(i):
69+
matches += 1
70+
71+
puzzle_actual_result = matches
72+
73+
74+
# -------------------------------- Outputs / results --------------------------------- #
75+
76+
print("Expected result : " + str(puzzle_expected_result))
77+
print("Actual result : " + str(puzzle_actual_result))
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+
from IntCode import IntCode
6+
7+
test_data = {}
8+
9+
test = 1
10+
test_data[test] = {
11+
"input": """1101,100,-1,4,0""",
12+
"expected": ["Unknown", "Unknown"],
13+
}
14+
test += 1
15+
test_data[test] = {
16+
"input": """3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99""",
17+
"expected": [
18+
"Unknown",
19+
"output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8",
20+
],
21+
}
22+
23+
test = "real"
24+
input_file = os.path.join(
25+
os.path.dirname(__file__),
26+
"Inputs",
27+
os.path.basename(__file__).replace(".py", ".txt"),
28+
)
29+
test_data[test] = {
30+
"input": open(input_file, "r+").read().strip(),
31+
"expected": ["15097178", "1558663"],
32+
}
33+
34+
# -------------------------------- Control program execution ------------------------- #
35+
36+
case_to_test = "real"
37+
part_to_test = 2
38+
IntCode.verbose_level = 1
39+
40+
# -------------------------------- Initialize some variables ------------------------- #
41+
42+
puzzle_input = test_data[case_to_test]["input"]
43+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
44+
puzzle_actual_result = "Unknown"
45+
46+
47+
# -------------------------------- Actual code execution ----------------------------- #
48+
49+
if part_to_test == 1:
50+
computer = IntCode(puzzle_input)
51+
computer.inputs.append(1)
52+
computer.run()
53+
54+
if computer.state == "Stopped":
55+
puzzle_actual_result = computer.outputs[-1]
56+
57+
58+
else:
59+
computer = IntCode(puzzle_input)
60+
computer.inputs.append(5)
61+
computer.run()
62+
63+
if computer.state == "Stopped":
64+
puzzle_actual_result = computer.outputs[-1]
65+
66+
67+
# -------------------------------- Outputs / results --------------------------------- #
68+
69+
print("Expected result : " + str(puzzle_expected_result))
70+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)