From 508d4ed9926575f450ea8410bc63c32bfa3df7ed Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Tue, 7 Dec 2021 17:49:32 -0500 Subject: [PATCH] Adding day07 part 1 --- puzzles/day07.py | 22 ++++++++++++++++++++-- puzzles/test_day07.py | 4 +--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/puzzles/day07.py b/puzzles/day07.py index 8dd55dc..2bdb5c0 100644 --- a/puzzles/day07.py +++ b/puzzles/day07.py @@ -7,10 +7,28 @@ Run with: import pathlib import sys +from statistics import median from typing import List, Tuple + +class AlignCrabSubs: + positions: List[int] + + def __init__(self, positions: List[int]) -> None: + self.positions = positions + + def calc_least_fuel(self) -> int: + target = median(self.positions) + fuel = 0 + for num in self.positions: + fuel += num - target if num > target else target - num + + return int(fuel) + + def part1(inputs: List[int]) -> int: - return False + subs = AlignCrabSubs(inputs) + return subs.calc_least_fuel() def part2(inputs: List[int]) -> int: @@ -19,7 +37,7 @@ def part2(inputs: List[int]) -> int: def parse(inputs: str) -> List[int]: """Parse the input string""" - return [int(line) for line in inputs.split()] + return [int(value) for value in inputs.split(",")] def solve(path: str) -> Tuple[int, int]: diff --git a/puzzles/test_day07.py b/puzzles/test_day07.py index 1c6e0ba..b699ed6 100644 --- a/puzzles/test_day07.py +++ b/puzzles/test_day07.py @@ -16,7 +16,6 @@ def day07_data(): return aoc.parse(pathlib.Path(input_path).read_text().strip()) -@pytest.mark.skip(reason="Not implemented") def test_example1(example_data): assert aoc.part1(example_data) == 37 @@ -26,9 +25,8 @@ def test_example2(example_data): assert aoc.part2(example_data) == ... -@pytest.mark.skip(reason="Not implemented") def test_part1(day07_data): - assert aoc.part1(day07_data) == ... + assert aoc.part1(day07_data) == 343441 @pytest.mark.skip(reason="Not implemented")