advent_of_code_2021

My attempts to work through the 2021 Advent of Code problems.
git clone https://git.eamoncaddigan.net/advent_of_code_2021.git
Log | Files | Refs | README | LICENSE

commit 726e9f7efd14ce878e96b649fe4ffc41fa8cb4d4
parent 04bce9efb52bc9b4d89a75a5110a85f2bf64bb8b
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Tue,  7 Dec 2021 13:08:40 -0500

Solution to day 7, part 1

Diffstat:
Aday07_part1.py | 28++++++++++++++++++++++++++++
1 file changed, 28 insertions(+), 0 deletions(-)

diff --git a/day07_part1.py b/day07_part1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +"""Advent of Code 2021, day 7 (part 2): optimize 'crab submarine' alignment to +minimize fuel""" + +from utils import get_puzzle_input, convert_int_line_to_series + +def find_minimum_fuel_use(positions): + """Given the positions of the 'crab submarines', find the minimum fuel use + to line them up""" + return float(calculate_fuel_use_to(positions, positions.median())) + +def calculate_fuel_use_to(start_positions, end_position): + """Given a series that represents the (1-D) positions of a collection of 'crab + submarines', find the fuel used to move to the given end position""" + return start_positions.sub(end_position).abs().sum() + +def solve_puzzle(input_string): + """Return the numeric solution to the puzzle""" + return find_minimum_fuel_use(convert_int_line_to_series(input_string)) + +def main(): + """Run when the file is called as a script""" + assert solve_puzzle("16,1,2,0,4,2,7,1,2,14\n") == 37 + print("Fuel spent to align crabs:", + f"{solve_puzzle(get_puzzle_input(7)):.0f}") + +if __name__ == "__main__": + main()