1
0
Fork 0

formatting and cosmetic updates to 2020.

This commit is contained in:
Gabe Venberg 2026-04-17 16:21:34 +02:00
parent 84c4cf9991
commit cd75e58f77
28 changed files with 320 additions and 287 deletions

View file

@ -2,34 +2,39 @@
import pathlib
import sys
import re
from dataclasses import dataclass
from pprint import pprint
def row_to_int(string: str)->int:
return int(string.replace('F','0').replace('B','1'),2)
def col_to_int(string: str)->int:
return int(string.replace('L','0').replace('R','1'),2)
def row_to_int(string: str) -> int:
return int(string.replace("F", "0").replace("B", "1"), 2)
def col_to_int(string: str) -> int:
return int(string.replace("L", "0").replace("R", "1"), 2)
def parse(puzzle_input: str):
"""Parse input"""
rows_and_cols = [(row_to_int(x[:7]),col_to_int(x[7:])) for x in puzzle_input.splitlines()]
return set([x[0]*8+x[1] for x in rows_and_cols])
rows_and_cols = [
(row_to_int(x[:7]), col_to_int(x[7:])) for x in puzzle_input.splitlines()
]
return set([x[0] * 8 + x[1] for x in rows_and_cols])
def part1(data):
"""Solve part 1"""
return max(data)
def part2(data):
"""Solve part 2"""
for row in range(128):
for col in range(8):
potentialID=row*8+col
potentialID = row * 8 + col
if potentialID not in data:
if (potentialID+1 in data) and (potentialID-1 in data):
if (potentialID + 1 in data) and (potentialID - 1 in data):
return potentialID
def solve(puzzle_input):
"""Solve the puzzle for the given input"""
data = parse(puzzle_input)
@ -38,6 +43,7 @@ def solve(puzzle_input):
return solution1, solution2
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"{path}:")

View file

@ -6,28 +6,24 @@ import day5 as aoc
PUZZLE_DIR = pathlib.Path(__file__).parent
#these test fixtures setup the test, mainly by reading the filename into a string in this simple case.
# these test fixtures setup the test, mainly by reading the filename into a string in this simple case.
@pytest.fixture
def example1():
puzzle_input = (PUZZLE_DIR / "example1").read_text().strip()
def example():
puzzle_input = (PUZZLE_DIR / "example.txt").read_text().strip()
return aoc.parse(puzzle_input)
@pytest.fixture
def example2():
puzzle_input = (PUZZLE_DIR / "input").read_text().strip()
return aoc.parse(puzzle_input)
# @pytest.mark.skip(reason="Not implemented")
def test_parse_example1(example1):
def test_parse(example):
"""Test that input is parsed properly"""
assert example1 == {119, 357, 567, 820}
assert example == {119, 357, 567, 820}
# @pytest.mark.skip(reason="Not implemented")
def test_part1_example1(example1):
def test_part1(example):
"""Test part 1 on example input"""
assert aoc.part1(example1) == 820
assert aoc.part1(example) == 820
# @pytest.mark.skip(reason="Not implemented")
def test_part2_example2(example2):
def test_part2(example):
"""Test part 2 on example input"""
assert aoc.part2(example2) == 640
assert aoc.part2(example) == 640