-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
62 lines (44 loc) · 1.43 KB
/
day3.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
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_instructions_from_file(file_path=top_dir + "resources/year2015_day3_input.txt"):
with open(file_path) as f:
for l in f:
return l.strip()
directions = {
"<": (-1, 0), # West
">": (1, 0), # East
"v": (0, -1), # South
"^": (0, +1), # North
}
def get_path(instructions):
x, y = 0, 0
yield x, y
for ins in instructions:
dx, dy = directions[ins]
x += dx
y += dy
yield x, y
def get_nb_houses(instructions):
return len(set(get_path(instructions)))
def get_nb_houses_with_robo(instructions):
ins1, ins2 = instructions[1::2], instructions[0::2]
return len(set(get_path(ins1)) | set(get_path(ins2)))
def run_tests():
assert get_nb_houses(">") == 2
assert get_nb_houses("^>v<") == 4
assert get_nb_houses("^v^v^v^v^v") == 2
assert get_nb_houses_with_robo("^v") == 3
assert get_nb_houses_with_robo("^>v<") == 3
assert get_nb_houses_with_robo("^v^v^v^v^v") == 11
def get_solutions():
instructions = get_instructions_from_file()
print(get_nb_houses(instructions) == 2081)
print(get_nb_houses_with_robo(instructions) == 2341)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)