from aoc import AOCDay from coordinate import Coordinate from math import inf from typing import Any, Dict, List def getWireCoordinates(wires: List[List[str]]) -> List[Dict[tuple, int]]: s = [] for w in wires: s.append({}) x = 0 y = 0 dist = 0 for d in w: direction = d[0] steps = int(d[1:]) for _ in range(steps): dist += 1 if direction == "R": x += 1 elif direction == "L": x -= 1 elif direction == "U": y -= 1 elif direction == "D": y += 1 if (x, y) not in s[-1]: s[-1][(x, y)] = dist return s class Day(AOCDay): test_solutions_p1 = [6, 159, 135] test_solutions_p2 = [30, 610, 410] def part1(self) -> Any: wires = self.getInputAsArraySplit() s = getWireCoordinates(wires) minDist = inf for x in s[0]: if x not in s[1]: continue mDist = Coordinate(*x).getDistanceTo(Coordinate(0, 0), mode=0) if mDist < minDist: minDist = mDist return minDist def part2(self) -> Any: wires = self.getInputAsArraySplit() s = getWireCoordinates(wires) minDist = inf for x in s[0]: if x in s[1] and s[0][x] + s[1][x] < minDist: minDist = s[0][x] + s[1][x] return minDist