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 04bce9efb52bc9b4d89a75a5110a85f2bf64bb8b
parent 091a3f52fab534bfc9c416a649eed44f85e46d85
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Tue,  7 Dec 2021 08:56:52 -0500

I need to start moving more generally useful stuff to `utils`

Diffstat:
Mday06_part1.py | 15++++-----------
Mday06_part2.py | 5++---
Mutils.py | 7+++++++
3 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/day06_part1.py b/day06_part1.py @@ -12,19 +12,13 @@ school of lanternfish using an initial list of fish and their ages""" # indices. import functools -import numpy as np -import pandas as pd -from utils import get_puzzle_input +from utils import get_puzzle_input, convert_int_line_to_series def convert_input_to_series(input_string): """Parse the input (a sequence of fish with their age in days) and return a Series, where the indices represent fish ages (in days) and the values represent the count of fish that age""" - return ( - pd.Series(input_string.rstrip().split(',')) - .astype(np.int64) - .value_counts() - ) + return convert_int_line_to_series(input_string).value_counts() # For this implementation, we'll advance the fish in age and spawn new fish all # in one step--even though, conceptually, it would be nice to separate these @@ -73,12 +67,11 @@ def solve_puzzle(input_string): """Return the numeric solution to the puzzle""" return count_fish_after(convert_input_to_series(input_string), 80) -def main(*argv): +def main(): """Run when the file is called as a script""" assert solve_puzzle("3,4,3,1,2\n") == 5934 print("Number of lanternfish after 80 days:", solve_puzzle(get_puzzle_input(6))) if __name__ == "__main__": - import sys - main(*sys.argv) + main() diff --git a/day06_part2.py b/day06_part2.py @@ -24,12 +24,11 @@ def solve_puzzle(input_string): """Return the numeric solution to the puzzle""" return count_fish_after(convert_input_to_series(input_string), 256) -def main(*argv): +def main(): """Run when the file is called as a script""" assert solve_puzzle("3,4,3,1,2\n") == 26984457539 print("Number of lanternfish after 256 days:", solve_puzzle(get_puzzle_input(6))) if __name__ == "__main__": - import sys - main(*sys.argv) + main() diff --git a/utils.py b/utils.py @@ -2,6 +2,13 @@ import os.path import requests +import numpy as np +import pandas as pd + +def convert_int_line_to_series(input_string): + """Converts one (optionally newline-terminated) string of comma-separated + integers to a pandas Series""" + return pd.Series(input_string.rstrip().split(',')).astype(np.int64) def get_puzzle_input(day_number, binary=False): """Downloads and returns the input data for the puzzle on a given day.