81 lines
1.9 KiB
Python
81 lines
1.9 KiB
Python
from tools.aoc import AOCDay
|
|
from tools.coordinate import Coordinate, DistanceAlgorithm
|
|
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):
|
|
inputs = [
|
|
[
|
|
(6, "test_input03"),
|
|
(159, "test_input03_2"),
|
|
(135, "test_input03_3"),
|
|
(1431, "input03")
|
|
],
|
|
[
|
|
(30, "test_input03"),
|
|
(610, "test_input03_2"),
|
|
(410, "test_input03_3"),
|
|
(48012, "input03")
|
|
]
|
|
]
|
|
|
|
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), algorithm=DistanceAlgorithm.MANHATTAN)
|
|
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
|
|
|
|
|
|
if __name__ == '__main__':
|
|
day = Day(2019, 3)
|
|
day.run(verbose=True)
|