2017 Day 11 Part 1
This commit is contained in:
parent
bd0b54b3ca
commit
85f3aa600d
4 changed files with 70 additions and 0 deletions
1
2017/.gitignore
vendored
Normal file
1
2017/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
input
|
||||||
0
2017/__init__.py
Normal file
0
2017/__init__.py
Normal file
10
2017/aoc.py
Normal file
10
2017/aoc.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def input_path(day: int) -> str:
|
||||||
|
return os.path.join(os.path.dirname(__file__), "input", f"day{day:>02}")
|
||||||
|
|
||||||
|
|
||||||
|
def puzzle_input(day: int) -> str:
|
||||||
|
with open(input_path(day)) as f:
|
||||||
|
return f.read().strip()
|
||||||
59
2017/day11.py
Normal file
59
2017/day11.py
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Tuple, Iterable
|
||||||
|
|
||||||
|
import aoc
|
||||||
|
|
||||||
|
HexPos = Tuple[int, int, int]
|
||||||
|
|
||||||
|
|
||||||
|
class Direction(Enum):
|
||||||
|
N = 0
|
||||||
|
NE = 1
|
||||||
|
SE = 2
|
||||||
|
S = 3
|
||||||
|
SW = 4
|
||||||
|
NW = 5
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def parse(cls, s: str):
|
||||||
|
match s:
|
||||||
|
case 'n': return Direction.N
|
||||||
|
case 's': return Direction.S
|
||||||
|
case 'ne': return Direction.NE
|
||||||
|
case 'nw': return Direction.NW
|
||||||
|
case 'se': return Direction.SE
|
||||||
|
case 'sw': return Direction.SW
|
||||||
|
case _: raise ValueError(f"unknown direction: {s}")
|
||||||
|
|
||||||
|
|
||||||
|
def part1():
|
||||||
|
steps = map(Direction.parse, aoc.puzzle_input(11).split(","))
|
||||||
|
return distance(walk(steps))
|
||||||
|
|
||||||
|
|
||||||
|
def walk(steps: Iterable[Direction]) -> HexPos:
|
||||||
|
pos = (0, 0, 0)
|
||||||
|
for step in steps:
|
||||||
|
pos = move(pos, step)
|
||||||
|
return pos
|
||||||
|
|
||||||
|
|
||||||
|
def distance(pos: HexPos) -> int:
|
||||||
|
assert sum(pos) == 0, pos
|
||||||
|
return sum(map(abs, pos)) // 2
|
||||||
|
|
||||||
|
|
||||||
|
def move(pos: HexPos, dir: Direction) -> HexPos:
|
||||||
|
(q, r, s) = pos
|
||||||
|
match dir:
|
||||||
|
case Direction.N: return (q, r-1, s+1)
|
||||||
|
case Direction.S: return (q, r+1, s-1)
|
||||||
|
|
||||||
|
case Direction.NE: return (q+1, r-1, s)
|
||||||
|
case Direction.SW: return (q-1, r+1, s)
|
||||||
|
|
||||||
|
case Direction.NW: return (q-1, r, s+1)
|
||||||
|
case Direction.SE: return (q+1, r, s-1)
|
||||||
|
|
||||||
|
|
||||||
|
print(part1())
|
||||||
Loading…
Reference in a new issue