-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
88 lines (63 loc) · 2.27 KB
/
day11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_steps_from_file(file_path=top_dir + "resources/year2017_day11_input.txt"):
with open(file_path) as f:
for l in f:
return l.strip().split(",")
# Map directions in diagonale coordinates
#
# --+ +-- --+(-1,1) +-- _
# \ n / \ / /| y
# nw +--+ ne (-1, 0) +---+ (0, 1) /
# / \ / \ /
# --+ +-- <==> --+ (0,0) +-- /
# \ / \ / \
# sw +--+ se (0, -1) +---+ (1, 0) \
# / s \ / \ \
# --+ +-- --+ (1,-1)+-- _\| x
coords = {
"nw": (-1, 0),
"n": (-1, 1),
"ne": (0, 1),
"sw": (0, -1),
"s": (1, -1),
"se": (1, 0),
}
def get_path(steps):
x, y = (0, 0)
yield (x, y)
for step in steps:
dx, dy = coords[step]
x += dx
y += dy
yield (x, y)
def get_final_position(steps):
return list(get_path(steps))[-1]
assert (0, 0) == get_final_position(c for c in ("n", "s"))
assert (0, 0) == get_final_position(c for c in ("nw", "se"))
assert (0, 0) == get_final_position(c for c in ("ne", "sw"))
assert (0, 0) == get_final_position(c for c in coords)
def get_distance(coord):
x, y = coord
return max(abs(x), abs(y))
def get_final_distance(steps):
return get_distance(get_final_position(steps))
def get_max_distance(steps):
return max(get_distance(p) for p in get_path(steps))
def run_tests():
assert get_final_distance("ne,ne,ne".split(",")) == 3
assert get_final_distance("ne,ne,sw,sw".split(",")) == 0
assert get_final_distance("ne,ne,s,s".split(",")) == 2
assert get_final_distance("se,sw,se,sw,sw".split(",")) == 3
def get_solutions():
steps = get_steps_from_file()
print(get_final_distance(steps) == 707)
print(get_max_distance(steps) == 1490)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)