day01 finished
This commit is contained in:
parent
a355de5d8b
commit
a607fbb329
6 changed files with 2332 additions and 1 deletions
0
advent/days/day01/__init__.py
Normal file
0
advent/days/day01/__init__.py
Normal file
2255
advent/days/day01/data/input.txt
Normal file
2255
advent/days/day01/data/input.txt
Normal file
File diff suppressed because it is too large
Load diff
14
advent/days/day01/data/test01.txt
Normal file
14
advent/days/day01/data/test01.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
45
advent/days/day01/solution.py
Normal file
45
advent/days/day01/solution.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Iterator, Generator
|
||||
|
||||
day_num = 1
|
||||
|
||||
|
||||
def part1(lines: Iterator[str]) -> int:
|
||||
return max(parse_lines(lines))
|
||||
|
||||
|
||||
def part2(lines: Iterator[str]) -> int:
|
||||
return sum(sorted(parse_lines(lines))[-3:])
|
||||
|
||||
|
||||
def parse_lines(lines: Iterator[str]) -> Generator[int, None, None]:
|
||||
"""
|
||||
Parses lines of string as integers and returns the sum of blocks separated by blank lines
|
||||
|
||||
Parameters
|
||||
----------
|
||||
lines : Iterator[str]
|
||||
The lines to be parsed
|
||||
|
||||
Returns
|
||||
-------
|
||||
Generator[int, None, None]
|
||||
A generator for the sum of each block
|
||||
|
||||
Raises
|
||||
------
|
||||
ValueError
|
||||
If a line is neither empty nor represents a valid int
|
||||
"""
|
||||
current = 0
|
||||
|
||||
for line in lines:
|
||||
if line:
|
||||
current += int(line)
|
||||
else:
|
||||
yield current
|
||||
current = 0
|
||||
|
||||
if current != 0:
|
||||
yield current
|
||||
17
advent/days/day01/test_solution.py
Normal file
17
advent/days/day01/test_solution.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from advent.common import utils
|
||||
|
||||
from .solution import day_num, part1, part2
|
||||
|
||||
|
||||
def test_part1():
|
||||
data = utils.read_data(day_num, 'test01.txt')
|
||||
expected = 24_000
|
||||
result = part1(data)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_part2():
|
||||
data = utils.read_data(day_num, 'test01.txt')
|
||||
expected = 45_000
|
||||
result = part2(data)
|
||||
assert result == expected
|
||||
Loading…
Add table
Add a link
Reference in a new issue