Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 297495a

Browse files
committed
Added days 2017-19 and 2017-20
1 parent e56725c commit 297495a

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

2017/19-A Series of Tubes.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, pathfinding, string
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """.....|..........
8+
.....|..+--+....
9+
.....A..|..C....
10+
.F---|----E|--+.
11+
.....|..|..|..D.
12+
.....+B-+..+--+.""",
13+
"expected": ['ABCDEF', '38'],
14+
}
15+
16+
test = 'real'
17+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
18+
test_data[test] = {"input": open(input_file, "r+").read(),
19+
"expected": ['UICRNSDOK', '16064'],
20+
}
21+
22+
# -------------------------------- Control program execution -------------------------------- #
23+
24+
case_to_test = 'real'
25+
part_to_test = 2
26+
verbose_level = 1
27+
28+
# -------------------------------- Initialize some variables -------------------------------- #
29+
30+
puzzle_input = test_data[case_to_test]['input']
31+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
32+
puzzle_actual_result = 'Unknown'
33+
34+
# -------------------------------- Actual code execution -------------------------------- #
35+
36+
37+
lines = puzzle_input.splitlines()
38+
if lines[len(lines)-1] == '':
39+
del lines[len(lines)-1]
40+
41+
width = max(len(line) for line in lines)
42+
grid = {(x, y): lines[y][x].replace('.', ' ') for x in range(width) for y in range(len(lines))}
43+
44+
direction = (0, 1)
45+
x, y = lines[0].index('|'), 0
46+
letters_seen = ''
47+
steps_taken = 1
48+
49+
cross_directions = {(0, 1): [(1, 0), (-1, 0)], (0, -1): [(1, 0), (-1, 0)], (1, 0): [(0, 1), (0, -1)], (-1, 0): [(0, 1), (0, -1)]}
50+
51+
while (x, y) in grid and grid[(x, y)] != ' ':
52+
new_cell = grid[(x, y)]
53+
54+
if new_cell in string.ascii_uppercase:
55+
letters_seen += new_cell
56+
elif new_cell == '+':
57+
new_direction = cross_directions[direction][0]
58+
new_x, new_y = x + new_direction[0], y + new_direction[1]
59+
60+
if (new_x, new_y) in grid:
61+
if grid[(new_x, new_y)] == ' ':
62+
direction = cross_directions[direction][1]
63+
else:
64+
direction = new_direction
65+
else:
66+
direction = cross_directions[direction][1]
67+
68+
x, y = x + direction[0], y + direction[1]
69+
steps_taken += 1
70+
71+
if part_to_test == 1:
72+
puzzle_actual_result = letters_seen
73+
else:
74+
puzzle_actual_result = steps_taken - 1
75+
76+
77+
78+
79+
# -------------------------------- Outputs / results -------------------------------- #
80+
81+
if verbose_level >= 3:
82+
print ('Input : ' + puzzle_input)
83+
print ('Expected result : ' + str(puzzle_expected_result))
84+
print ('Actual result : ' + str(puzzle_actual_result))
85+
86+
87+
88+

2017/20-Particle Swarm.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """p=<3,0,0>, v=<2,0,0>, a=<-1,0,0>
8+
p=<4,0,0>, v=<0,0,0>, a=<-2,0,0>""",
9+
"expected": ['Unknown', 'Unknown'],
10+
}
11+
12+
test += 1
13+
test_data[test] = {"input": """p=<-6,0,0>, v=<3,0,0>, a=<0,0,0>
14+
p=<-4,0,0>, v=<2,0,0>, a=<0,0,0>
15+
p=<-2,0,0>, v=<1,0,0>, a=<0,0,0>
16+
p=<3,0,0>, v=<-1,0,0>, a=<0,0,0>""",
17+
"expected": ['Unknown', 'Unknown'],
18+
}
19+
20+
test = 'real'
21+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
22+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
23+
"expected": ['Unknown', 'Unknown'],
24+
}
25+
26+
# -------------------------------- Control program execution -------------------------------- #
27+
28+
case_to_test = 'real'
29+
part_to_test = 2
30+
verbose_level = 1
31+
32+
# -------------------------------- Initialize some variables -------------------------------- #
33+
34+
puzzle_input = test_data[case_to_test]['input']
35+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
36+
puzzle_actual_result = 'Unknown'
37+
38+
39+
# -------------------------------- Actual code execution -------------------------------- #
40+
41+
max_accel = 10**6
42+
43+
if part_to_test == 1:
44+
part_nr = 0
45+
for string in puzzle_input.split('\n'):
46+
_, _, acceleration = string.split(' ')
47+
acceleration = list(map(int, acceleration[3:-1].split(',')))
48+
49+
if max_accel > sum(map(abs, acceleration)):
50+
max_accel = sum(map(abs, acceleration))
51+
closest_part = part_nr
52+
53+
part_nr += 1
54+
55+
puzzle_actual_result = closest_part
56+
57+
58+
59+
else:
60+
particles = {}
61+
collisions = []
62+
part_nr = 0
63+
saved_len = 0
64+
for string in puzzle_input.split('\n'):
65+
position, speed, acceleration = string.split(' ')
66+
position = list(map(int, position[3:-2].split(',')))
67+
speed = list(map(int, speed[3:-2].split(',')))
68+
acceleration = list(map(int, acceleration[3:-1].split(',')))
69+
70+
particles[part_nr] = [position, speed, acceleration]
71+
72+
part_nr += 1
73+
74+
for i in range(10**4):
75+
collisions = []
76+
for part_nr in particles:
77+
position, speed, acceleration = particles[part_nr]
78+
speed = [speed[x] + acceleration[x] for x in range (3)]
79+
position = [position[x] + speed[x] for x in range (3)]
80+
particles[part_nr] = [position, speed, acceleration]
81+
collisions.append(position)
82+
83+
coordinates = [','.join(map(str, collision)) for collision in collisions]
84+
85+
list_particles = list(particles.keys())
86+
for part_nr in list_particles:
87+
if collisions.count(particles[part_nr][0]) > 1:
88+
del particles[part_nr]
89+
90+
if i % 10 == 0 and len(particles) == saved_len:
91+
break
92+
elif i % 10 == 0:
93+
saved_len = len(particles)
94+
95+
print(i, len(particles))
96+
97+
puzzle_actual_result = len(particles)
98+
99+
100+
101+
102+
# -------------------------------- Outputs / results -------------------------------- #
103+
104+
if verbose_level >= 3:
105+
print ('Input : ' + puzzle_input)
106+
print ('Expected result : ' + str(puzzle_expected_result))
107+
print ('Actual result : ' + str(puzzle_actual_result))
108+
109+
110+
111+

0 commit comments

Comments
 (0)