feat: add solution 2019/03
This commit is contained in:
52
src/solution/year_2019/day_03.py
Normal file
52
src/solution/year_2019/day_03.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import collections
|
||||
from typing import Any
|
||||
|
||||
DIR_MAP = {
|
||||
"U": (0, -1),
|
||||
"D": (0, 1),
|
||||
"L": (-1, 0),
|
||||
"R": (1, 0),
|
||||
}
|
||||
|
||||
|
||||
def _parse_input(input_data: str) -> list[list[tuple[str, int]]]:
|
||||
wires = []
|
||||
for line in input_data.splitlines():
|
||||
wire = [(w[0], int(w[1:])) for w in line.split(",")]
|
||||
wires.append(wire)
|
||||
return wires
|
||||
|
||||
|
||||
def part_1_2(input_data: str) -> Any:
|
||||
wires = _parse_input(input_data)
|
||||
|
||||
locs = collections.defaultdict(dict)
|
||||
|
||||
m1 = None
|
||||
m2 = None
|
||||
|
||||
for wid, wire in enumerate(wires):
|
||||
x, y, w = 0, 0, 0
|
||||
|
||||
for direction, distance in wire:
|
||||
dx, dy = DIR_MAP[direction]
|
||||
for _ in range(distance):
|
||||
w += 1
|
||||
x += dx
|
||||
y += dy
|
||||
|
||||
loc = locs[(x, y)]
|
||||
loc.setdefault(wid, w)
|
||||
|
||||
if len(loc) == 2:
|
||||
d1 = abs(x) + abs(y)
|
||||
m1 = min(m1 or d1, d1)
|
||||
|
||||
d2 = loc[0] + loc[1]
|
||||
m2 = min(m2 or d2, d2)
|
||||
|
||||
return m1, m2
|
||||
|
||||
|
||||
def test_part_1_2(example_data):
|
||||
assert part_1_2(example_data) == (159, 610)
|
||||
Reference in New Issue
Block a user