-
Notifications
You must be signed in to change notification settings - Fork 3
/
day01.py
executable file
·68 lines (51 loc) · 1.7 KB
/
day01.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
#!/usr/bin/env python
from collections import defaultdict
DIRECTIONS = ["north", "east", "south", "west"]
def turn_right(this_dir_i):
return (this_dir_i + 1) % len(DIRECTIONS)
def turn_left(this_dir_i):
return (this_dir_i - 1) % len(DIRECTIONS)
def move(location, dir_i):
direction = DIRECTIONS[dir_i]
if direction == "north":
return location + complex(0, 1)
if direction == "east":
return location + complex(1, 0)
if direction == "south":
return location + complex(0, -1)
if direction == "west":
return location + complex(-1, 0)
raise Exception("move: Unknown Direction")
def parse(filename):
return [parse_step(step) for step in open(filename).readline().strip().split(", ")]
def parse_step(step):
direction = step[0]
distance = int(step[1:])
return (direction, distance)
def manhattan(coord):
return abs(coord.real) + abs(coord.imag)
def solve(steps):
dir_i = 0 # Start Looking North
location = complex(0, 0)
seen = defaultdict(lambda: 0)
answer2 = None
for (direction, distance) in steps:
if direction == "R":
dir_i = turn_right(dir_i)
elif direction == "L":
dir_i = turn_left(dir_i)
else:
raise Exception("solve: Unknown Direction")
for _ in range(distance):
location = move(location, dir_i)
seen[location] += 1
if seen[location] == 2 and answer2 is None:
answer2 = int(manhattan(location))
answer1 = int(manhattan(location))
return answer1, answer2
data = parse("../inputs/01/input.txt")
answer1, answer2 = solve(data)
print("Part 1:")
print(answer1)
print("Part 2:")
print(answer2)