Remove unneeded class (thanks to meowmeowwarrior)
This commit is contained in:
parent
96e86d6a87
commit
7d0d3e504e
2 changed files with 50 additions and 49 deletions
|
|
@ -6,15 +6,13 @@ day_num = 10
|
||||||
|
|
||||||
|
|
||||||
def part1(lines: Iterator[str]) -> int:
|
def part1(lines: Iterator[str]) -> int:
|
||||||
return sum(Signal.grab_values(lines))
|
return sum(grab_values(lines))
|
||||||
|
|
||||||
|
|
||||||
def part2(lines: Iterator[str]) -> list[str]:
|
def part2(lines: Iterator[str]) -> list[str]:
|
||||||
return Signal.draw(lines, 40, 6)
|
return draw(lines, 40, 6)
|
||||||
|
|
||||||
|
|
||||||
class Signal:
|
|
||||||
@staticmethod
|
|
||||||
def parse(line: str) -> None | int:
|
def parse(line: str) -> None | int:
|
||||||
"""
|
"""
|
||||||
Parses the a line into the two possible instructions.
|
Parses the a line into the two possible instructions.
|
||||||
|
|
@ -28,29 +26,32 @@ class Signal:
|
||||||
case _:
|
case _:
|
||||||
raise Exception(f"Unknown line: {line}")
|
raise Exception(f"Unknown line: {line}")
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def cycles(lines: Iterator[str]) -> Iterator[int]:
|
def cycles(lines: Iterator[str]) -> Iterator[int]:
|
||||||
"""
|
"""
|
||||||
Cycles throw the instructions and yields a new value on each cycle
|
Cycles through the instructions and yields a new value for each cycle
|
||||||
"""
|
"""
|
||||||
register = 1
|
register = 1
|
||||||
for line in lines:
|
for line in lines:
|
||||||
yield register
|
yield register
|
||||||
command = Signal.parse(line)
|
match parse(line):
|
||||||
if command is not None:
|
case None:
|
||||||
|
pass
|
||||||
|
case value:
|
||||||
|
yield register
|
||||||
|
register += value
|
||||||
yield register
|
yield register
|
||||||
register += command
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def grab_values(lines: Iterator[str]) -> Iterator[int]:
|
def grab_values(lines: Iterator[str]) -> Iterator[int]:
|
||||||
for cycle, value in enumerate(Signal.cycles(lines), start=1):
|
for cycle, value in enumerate(cycles(lines), start=1):
|
||||||
if cycle in [20, 60, 100, 140, 180, 220]:
|
if cycle in [20, 60, 100, 140, 180, 220]:
|
||||||
yield cycle * value
|
yield cycle * value
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def draw(lines: Iterator[str], width: int, height: int) -> list[str]:
|
def draw(lines: Iterator[str], width: int, height: int) -> list[str]:
|
||||||
picture = ""
|
picture = ""
|
||||||
for cycle, sprite in enumerate(Signal.cycles(lines)):
|
for cycle, sprite in enumerate(cycles(lines)):
|
||||||
crt_pos = cycle % width
|
crt_pos = cycle % width
|
||||||
if sprite - 1 <= crt_pos and crt_pos <= sprite + 1:
|
if sprite - 1 <= crt_pos and crt_pos <= sprite + 1:
|
||||||
picture += '#'
|
picture += '#'
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
from advent.common import utils
|
from advent.common import utils
|
||||||
|
|
||||||
from .solution import Signal, day_num, part1, part2
|
from .solution import cycles, day_num, draw, grab_values, part1, part2
|
||||||
|
|
||||||
|
|
||||||
def test_part1():
|
def test_part1():
|
||||||
|
|
@ -19,20 +19,20 @@ def test_part2():
|
||||||
|
|
||||||
def test_small():
|
def test_small():
|
||||||
lines = utils.read_data(day_num, 'test02.txt')
|
lines = utils.read_data(day_num, 'test02.txt')
|
||||||
expected = [1, 1, 1, 4, 4]
|
expected = [1, 1, 1, 4, 4, -1]
|
||||||
result = list(Signal.cycles(lines))
|
result = list(cycles(lines))
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def test_grab_values():
|
def test_grab_values():
|
||||||
lines = utils.read_data(day_num, 'test01.txt')
|
lines = utils.read_data(day_num, 'test01.txt')
|
||||||
expected = [420, 1140, 1800, 2940, 2880, 3960]
|
expected = [420, 1140, 1800, 2940, 2880, 3960]
|
||||||
result = list(Signal.grab_values(lines))
|
result = list(grab_values(lines))
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
|
||||||
def test_draw():
|
def test_draw():
|
||||||
lines = utils.read_data(day_num, 'test01.txt')
|
lines = utils.read_data(day_num, 'test01.txt')
|
||||||
expected = list(utils.read_data(day_num, 'expected.txt'))
|
expected = list(utils.read_data(day_num, 'expected.txt'))
|
||||||
result = Signal.draw(lines, 40, 6)
|
result = draw(lines, 40, 6)
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue