-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc201711.py
47 lines (34 loc) · 1.01 KB
/
aoc201711.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
"""AoC 11, 2017: Hex Ed."""
# Standard library imports
import pathlib
import sys
# Third party imports
import numpy as np
# Using 3D cube coordinates: https://math.stackexchange.com/a/2643016
DIRECTIONS = {
"n": (1, -1, 0),
"ne": (0, -1, 1),
"se": (-1, 0, 1),
"s": (-1, 1, 0),
"sw": (0, 1, -1),
"nw": (1, 0, -1),
}
def parse_data(puzzle_input):
"""Parse input."""
return np.array([DIRECTIONS[step] for step in puzzle_input.split(",")])
def part1(data):
"""Solve part 1."""
return np.max(np.abs(data.sum(axis=0)))
def part2(data):
"""Solve part 2."""
return np.max(np.abs(data.cumsum(axis=0)))
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
print("\n".join(str(solution) for solution in solutions))