-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaxicab.py
More file actions
42 lines (30 loc) · 844 Bytes
/
taxicab.py
File metadata and controls
42 lines (30 loc) · 844 Bytes
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
""" Advent of Code 2016. Day 1: No Time for a Taxicab """
import numpy as np
with open("input.txt") as f:
dirs = [d.strip() for d in f.read().split(",")]
# North and East are positive
# face are: north = 0, east = 1, south = 2, west = 3
turn = {
"L": lambda x: (x-1) % 4,
"R": lambda x: (x+1) % 4
}
def move(dirs, part=1):
face = 0 # north
pos = [0, 0]
seen = {tuple(pos)}
for t, *blocks in dirs:
face = turn[t](face)
idx = face % 2
sign = (idx + 1 - face)
to_go = int("".join(blocks))
final = pos[idx] + sign * to_go
if part == 1:
pos[idx] = final
while pos[idx] != final:
pos[idx] += sign
if tuple(pos) in seen:
return sum(np.abs(pos))
seen.add(tuple(pos))
return sum(np.abs(pos))
print("Part 1:\t", move(dirs))
print("Part 2:\t", move(dirs, part=2))