-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
executable file
·61 lines (47 loc) · 1.18 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
#!/usr/bin/env python3
# [Day 11: Hex Ed](https://adventofcode.com/2017/day/11)
import sys
from pathlib import Path
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
STEP_X = 1 # 1.5
STEP_Y = 1 # math.sqrt(3) / 2
def steps(x, y):
x = abs(x)
y = abs(y)
s = 0
while x != 0:
s += 1
if x >= y:
x -= STEP_X
y -= STEP_Y
else:
y -= STEP_Y * 2
return s
def walk(data):
m = 0
x, y = 0, 0
for d in data.split(","):
match d:
case "ne":
x = x + STEP_X
y = y + STEP_Y
case "se":
x = x + STEP_X
y = y - STEP_Y
case "s":
y = y - STEP_Y * 2
case "n":
y = y + STEP_Y * 2
case "nw":
x = x - STEP_X
y = y + STEP_Y
case "sw":
x = x - STEP_X
y = y - STEP_Y
case _:
raise ValueError(d)
m = max(m, steps(x, y))
print(steps(x, y))
print(m)
walk(data)