-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
75 lines (68 loc) · 2.17 KB
/
day12.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
from aoc import timer, read_file
from math import sin, cos
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
def part1(data):
FACING = EAST
POS_X = 0
POS_Y = 0
for direction, count in data:
if direction == 'F':
if FACING == EAST: POS_X += count
elif FACING == WEST: POS_X -= count
elif FACING == NORTH: POS_Y += count
elif FACING == SOUTH: POS_Y -= count
elif direction == 'R':
FACING = (FACING + count // 90) % 4
elif direction == 'L':
FACING = (FACING + -count // 90) % 4
elif direction == 'N':
POS_Y += count
elif direction == 'E':
POS_X += count
elif direction == 'S':
POS_Y -= count
elif direction == 'W':
POS_X -= count
else:
raise Exception('invalid direction')
print(abs(POS_X) + abs(POS_Y))
def part2(data):
WAYPOINT_X_DELTA = 10
WAYPOINT_Y_DELTA = 1
FACING = EAST
POS_X = 0
POS_Y = 0
for direction, count in data:
if direction == 'F':
POS_X += (count * WAYPOINT_X_DELTA)
POS_Y += (count * WAYPOINT_Y_DELTA)
elif direction == 'R':
for _ in range(count // 90):
WAYPOINT_X_DELTA, WAYPOINT_Y_DELTA = (WAYPOINT_Y_DELTA, -WAYPOINT_X_DELTA)
elif direction == 'L':
for _ in range((count // 90)):
WAYPOINT_X_DELTA, WAYPOINT_Y_DELTA = (-WAYPOINT_Y_DELTA, WAYPOINT_X_DELTA)
elif direction == 'N':
WAYPOINT_Y_DELTA += count
elif direction == 'E':
WAYPOINT_X_DELTA += count
elif direction == 'S':
WAYPOINT_Y_DELTA -= count
elif direction == 'W':
WAYPOINT_X_DELTA -= count
else:
raise Exception('invalid direction')
print(f"{direction}{count} {FACING=}, {POS_X=}, {POS_Y=}, {WAYPOINT_X_DELTA=}, {WAYPOINT_Y_DELTA=}, {count=}")
print(POS_X, POS_Y)
print(abs(POS_X) + abs(POS_Y))
if __name__ == "__main__":
data = read_file(12)
data = [(line[0], int(line[1:])) for line in data]
part1(data)
part2(data)
# south = 2
# rotate -270 (-3) so we should get 3
#