Skip to content

Commit b22e8e7

Browse files
committed
Added days 2017-12 and 2017-13
1 parent 96bc519 commit b22e8e7

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

2017/12-Digital Plumber.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
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """0 <-> 2
8+
1 <-> 1
9+
2 <-> 0, 3, 4
10+
3 <-> 2, 4
11+
4 <-> 2, 3, 6
12+
5 <-> 6
13+
6 <-> 4, 5""",
14+
"expected": ['Unknown', 'Unknown'],
15+
}
16+
17+
test += 1
18+
test_data[test] = {"input": """""",
19+
"expected": ['Unknown', 'Unknown'],
20+
}
21+
22+
test = 'real'
23+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
24+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
25+
"expected": ['378', 'Unknown'],
26+
}
27+
28+
# -------------------------------- Control program execution -------------------------------- #
29+
30+
case_to_test = 'real'
31+
part_to_test = 2
32+
verbose_level = 1
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+
pipes = {}
44+
programs = []
45+
for string in puzzle_input.split('\n'):
46+
source, _, *targets = string.split(' ')
47+
targets = [x.replace(',', '') for x in targets]
48+
49+
if not source in pipes:
50+
pipes[source] = []
51+
52+
pipes[source] += targets
53+
54+
for target in targets:
55+
if not target in pipes:
56+
pipes[target] = []
57+
else:
58+
pipes[target].append(source)
59+
60+
programs = pipes.keys()
61+
62+
village = pathfinding.Graph(programs, pipes)
63+
village.breadth_first_search('0')
64+
65+
if part_to_test == 1:
66+
puzzle_actual_result = len(village.distance_from_start)
67+
68+
else:
69+
nb_groups = 1
70+
programs_in_groups = list(village.distance_from_start.keys())
71+
for program in programs:
72+
if program in programs_in_groups:
73+
continue
74+
75+
nb_groups += 1
76+
77+
village.reset_search()
78+
village.breadth_first_search(program)
79+
programs_in_groups += list(village.distance_from_start.keys())
80+
81+
puzzle_actual_result = nb_groups
82+
83+
84+
85+
86+
87+
# -------------------------------- Outputs / results -------------------------------- #
88+
89+
if verbose_level >= 3:
90+
print ('Input : ' + puzzle_input)
91+
print ('Expected result : ' + str(puzzle_expected_result))
92+
print ('Actual result : ' + str(puzzle_actual_result))
93+
94+
95+
96+

2017/13-Packet Scanners.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """0: 3
8+
1: 2
9+
4: 4
10+
6: 4""",
11+
"expected": ['Unknown', 'Unknown'],
12+
}
13+
14+
test += 1
15+
test_data[test] = {"input": """""",
16+
"expected": ['Unknown', 'Unknown'],
17+
}
18+
19+
test = 'real'
20+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
21+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
22+
"expected": ['3184', '3878062'],
23+
}
24+
25+
# -------------------------------- Control program execution -------------------------------- #
26+
27+
case_to_test = 'real'
28+
part_to_test = 2
29+
verbose_level = 1
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+
levels = {}
41+
for string in puzzle_input.split('\n'):
42+
depth, size = string.split(': ')
43+
depth, size = int(depth), int(size)
44+
levels[depth] = size
45+
46+
47+
if part_to_test == 1:
48+
scanners = {x:0 for x in levels}
49+
severity = 0
50+
for position in range(max(levels.keys())+1):
51+
# Move packet
52+
if position in scanners:
53+
if scanners[position] == 0:
54+
severity += position * levels[position]
55+
if part_to_test == 2:
56+
severity = 1
57+
break
58+
59+
# Move scanners
60+
scanners = {x:min(position+1, 2*(levels[x]-1) - position-1) % (2*levels[x]-2) for x in scanners}
61+
62+
puzzle_actual_result = severity
63+
64+
else:
65+
for delay in range (10**15):
66+
caught = False
67+
for depth, size in levels.items():
68+
if ((delay + depth) / (2*(size-1))).is_integer():
69+
caught = True
70+
break
71+
72+
if not caught:
73+
puzzle_actual_result = delay
74+
break
75+
76+
# Fails for 0-1999
77+
78+
# -------------------------------- Outputs / results -------------------------------- #
79+
80+
if verbose_level >= 3:
81+
print ('Input : ' + puzzle_input)
82+
print ('Expected result : ' + str(puzzle_expected_result))
83+
print ('Actual result : ' + str(puzzle_actual_result))
84+
85+
86+
87+

0 commit comments

Comments
 (0)