From 85f3aa600d71aac040cf90e953525cae174b740b Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Tue, 12 Aug 2025 20:25:34 -0400 Subject: [PATCH] 2017 Day 11 Part 1 --- 2017/.gitignore | 1 + 2017/__init__.py | 0 2017/aoc.py | 10 ++++++++ 2017/day11.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 2017/.gitignore create mode 100644 2017/__init__.py create mode 100644 2017/aoc.py create mode 100644 2017/day11.py diff --git a/2017/.gitignore b/2017/.gitignore new file mode 100644 index 0000000..3f9177e --- /dev/null +++ b/2017/.gitignore @@ -0,0 +1 @@ +input diff --git a/2017/__init__.py b/2017/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/2017/aoc.py b/2017/aoc.py new file mode 100644 index 0000000..54fb636 --- /dev/null +++ b/2017/aoc.py @@ -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() diff --git a/2017/day11.py b/2017/day11.py new file mode 100644 index 0000000..a8af405 --- /dev/null +++ b/2017/day11.py @@ -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())