feat: add solution 2019/03
This commit is contained in:
2
data/example/2019/03.txt
Normal file
2
data/example/2019/03.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
R75,D30,R83,U83,L12,D49,R71,U7,L72
|
||||||
|
U62,R66,U55,R34,D71,R55,D58,R83
|
||||||
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