Compare commits
3 Commits
00000190d0
...
000002201f
| Author | SHA1 | Date | |
|---|---|---|---|
|
000002201f
|
|||
|
00000210a8
|
|||
|
000002007f
|
@@ -113,11 +113,11 @@ def part_2(input_data: str) -> Any:
|
||||
return None
|
||||
|
||||
|
||||
def test_part_1(example_data)
|
||||
def test_part_1(example_data):
|
||||
assert part_1(example_data) == 0
|
||||
|
||||
|
||||
def test_part_2(example_data)
|
||||
def test_part_2(example_data):
|
||||
assert part_2(example_data) == 0
|
||||
"""
|
||||
|
||||
|
||||
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
|
||||
1
data/example/2019/04.txt
Normal file
1
data/example/2019/04.txt
Normal file
@@ -0,0 +1 @@
|
||||
229228-249228
|
||||
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)
|
||||
66
src/solution/year_2019/day_04.py
Normal file
66
src/solution/year_2019/day_04.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _test_len(s: str, n: int):
|
||||
return len(s) == n
|
||||
|
||||
|
||||
def _test_increasing(s: str):
|
||||
return all(s[i - 1] <= s[i] for i in range(1, len(s)))
|
||||
|
||||
|
||||
def _test_adjacent(s: str):
|
||||
return any(s[i - 1] == s[i] for i in range(1, len(s)))
|
||||
|
||||
|
||||
def _get_index(s: str, i: int) -> str | None:
|
||||
if not (0 <= i < len(s)):
|
||||
return None
|
||||
|
||||
return s[i]
|
||||
|
||||
|
||||
def _test_adjacent_2(s: str) -> bool:
|
||||
for i in range(1, len(s)):
|
||||
s1 = s[i]
|
||||
s2 = s[i - 1]
|
||||
|
||||
s0 = _get_index(s, i - 2)
|
||||
s3 = _get_index(s, i + 1)
|
||||
|
||||
if s1 == s2 and s1 != s0 and s2 != s3:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def _is_valid_password(n: int) -> bool:
|
||||
s = str(n)
|
||||
return all([_test_len(s, 6), _test_adjacent(s), _test_increasing(s)])
|
||||
|
||||
|
||||
def _is_valid_password_2(n: int) -> bool:
|
||||
s = str(n)
|
||||
return all([_test_len(s, 6), _test_adjacent_2(s), _test_increasing(s)])
|
||||
|
||||
|
||||
def part_1(input_data: str) -> Any:
|
||||
a, b = input_data.split("-")
|
||||
a, b = int(a), int(b)
|
||||
|
||||
return len([x for x in range(a, b + 1) if _is_valid_password(x)])
|
||||
|
||||
|
||||
def part_2(input_data: str) -> Any:
|
||||
a, b = input_data.split("-")
|
||||
a, b = int(a), int(b)
|
||||
|
||||
return len([x for x in range(a, b + 1) if _is_valid_password_2(x)])
|
||||
|
||||
|
||||
def test_part_1(example_data):
|
||||
assert part_1(example_data) == 316
|
||||
|
||||
|
||||
def test_part_2(example_data):
|
||||
assert part_2(example_data) == 218
|
||||
Reference in New Issue
Block a user