Skip to content

Commit 7e8c5f4

Browse files
committed
Added day 2018-10
1 parent 153aa77 commit 7e8c5f4

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

2018/10-The Stars Align.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, parse, pathfinding
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """position=< 9, 1> velocity=< 0, 2>
8+
position=< 7, 0> velocity=<-1, 0>
9+
position=< 3, -2> velocity=<-1, 1>
10+
position=< 6, 10> velocity=<-2, -1>
11+
position=< 2, -4> velocity=< 2, 2>
12+
position=<-6, 10> velocity=< 2, -2>
13+
position=< 1, 8> velocity=< 1, -1>
14+
position=< 1, 7> velocity=< 1, 0>
15+
position=<-3, 11> velocity=< 1, -2>
16+
position=< 7, 6> velocity=<-1, -1>
17+
position=<-2, 3> velocity=< 1, 0>
18+
position=<-4, 3> velocity=< 2, 0>
19+
position=<10, -3> velocity=<-1, 1>
20+
position=< 5, 11> velocity=< 1, -2>
21+
position=< 4, 7> velocity=< 0, -1>
22+
position=< 8, -2> velocity=< 0, 1>
23+
position=<15, 0> velocity=<-2, 0>
24+
position=< 1, 6> velocity=< 1, 0>
25+
position=< 8, 9> velocity=< 0, -1>
26+
position=< 3, 3> velocity=<-1, 1>
27+
position=< 0, 5> velocity=< 0, -1>
28+
position=<-2, 2> velocity=< 2, 0>
29+
position=< 5, -2> velocity=< 1, 2>
30+
position=< 1, 4> velocity=< 2, 1>
31+
position=<-2, 7> velocity=< 2, -2>
32+
position=< 3, 6> velocity=<-1, -1>
33+
position=< 5, 0> velocity=< 1, 0>
34+
position=<-6, 0> velocity=< 2, 0>
35+
position=< 5, 9> velocity=< 1, -2>
36+
position=<14, 7> velocity=<-2, 0>
37+
position=<-3, 6> velocity=< 2, -1>""",
38+
"expected": ['Unknown', 'Unknown'],
39+
}
40+
41+
test = 'real'
42+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
43+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
44+
"expected": ['RLEZNRAN', '10240'],
45+
}
46+
47+
# -------------------------------- Control program execution -------------------------------- #
48+
49+
case_to_test = 'real'
50+
part_to_test = 1
51+
52+
# -------------------------------- Initialize some variables -------------------------------- #
53+
54+
puzzle_input = test_data[case_to_test]['input']
55+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
56+
puzzle_actual_result = 'Unknown'
57+
58+
59+
# -------------------------------- Actual code execution -------------------------------- #
60+
stars = []
61+
for string in puzzle_input.split('\n'):
62+
if string == '':
63+
continue
64+
r = parse.parse('position=<{:>d},{:>d}> velocity=<{:>d},{:>d}>', string)
65+
stars.append(list(map(int, r)))
66+
67+
star_map = pathfinding.Graph()
68+
for i in range (2*10**4):
69+
stars = [(x+vx,y+vy,vx,vy) for x, y, vx, vy in stars]
70+
vertices = [(x, y) for x, y, vx, vy in stars]
71+
72+
73+
# This was solved a bit manually
74+
# I noticed all coordinates would converge around 0 at some point
75+
# That point was around 10300 seconds
76+
# Then made a limit: all coordinates should be within 300 from zero
77+
# (my first test was actually 200, but that was gave no result)
78+
# This gave ~ 20 seconds of interesting time
79+
# At the end it was trial and error to find 10 240
80+
coords = [v[0] in range(-300, 300) for v in vertices] + [v[1] in range(-300, 300) for v in vertices]
81+
82+
if all(coords) and i == 10239:
83+
star_map.vertices = vertices
84+
print (i+1)
85+
print (star_map.vertices_to_grid(wall=' '))
86+
87+
88+
89+
90+
# -------------------------------- Outputs / results -------------------------------- #
91+
92+
print ('Expected result : ' + str(puzzle_expected_result))
93+
print ('Actual result : ' + str(puzzle_actual_result))
94+
95+
96+
97+

0 commit comments

Comments
 (0)