aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 7e1f3d0f3d281d19fe34d714b4838f9aa8206821 (patch)
parent ebb6d16853a66713fac9edc654b2534282fc2cec
Author: Alex Karle <alex@alexkarle.com>
Date:   Mon,  6 Dec 2021 00:16:48 -0500

day6: Add python solution -- fun!

Wow, this was a fun one! I totally went for the naive solution of
appending to the array in pt A... pt B of course showed my solution
didn't scale whatsoever, and I came up with the constant space solution,
which is really much more elegant.

Diffstat:
A6/a.py | 28++++++++++++++++++++++++++++
A6/b.py | 28++++++++++++++++++++++++++++
A6/input | 1+
3 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/6/a.py b/6/a.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# XXX: This solution is totally not scalable, and could +# EASILY use the solution to pt B for a faster solution. +# I'm keeping it for posterity to show that I brute forced +# pt A :) +import sys + +fish = [] + +def repro(fish): + new = [] + for f in fish: + if f == 0: + new += [6, 8] + else: + new.append(f - 1) + + return new + +for l in sys.stdin: + # just keep the last one (only one) + fish = [int(x) for x in l.split(',')] + +for i in range(80): + fish = repro(fish) + +print(len(fish)) diff --git a/6/b.py b/6/b.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +import sys + +# rather than keep a raw list, count number of each type! +counts = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +def repro(fish): + new = [] + for f in fish: + if f == 0: + new += [6, 8] + else: + new.append(f - 1) + + return new + +for l in sys.stdin: + # just keep the last one (only one) + fish = [int(x) for x in l.split(',')] + for f in fish: + counts[f] += 1 + +for i in range(256): + ready = counts.pop(0) + counts[6] += ready + counts.append(ready) + +print(sum(counts)) diff --git a/6/input b/6/input @@ -0,0 +1 @@ +1,4,1,1,1,1,5,1,1,5,1,4,2,5,1,2,3,1,1,1,1,5,4,2,1,1,3,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,1,1,1,1,1,1,1,4,1,1,1,1,5,1,4,1,1,4,1,1,1,1,4,1,1,5,5,1,1,1,4,1,1,1,1,1,3,2,1,1,1,1,1,2,3,1,1,2,1,1,1,3,1,1,1,2,1,2,1,1,2,1,1,3,1,1,1,3,3,5,1,4,1,1,5,1,1,4,1,5,3,3,5,1,1,1,4,1,1,1,1,1,1,5,5,1,1,4,1,2,1,1,1,1,2,2,2,1,1,2,2,4,1,1,1,1,3,1,2,3,4,1,1,1,4,4,1,1,1,1,1,1,1,4,2,5,2,1,1,4,1,1,5,1,1,5,1,5,5,1,3,5,1,1,5,1,1,2,2,1,1,1,1,1,1,1,4,3,1,1,4,1,4,1,1,1,1,4,1,4,4,4,3,1,1,3,2,1,1,1,1,1,1,1,4,1,3,1,1,1,1,1,1,1,5,2,4,2,1,4,4,1,5,1,1,3,1,3,1,1,1,1,1,4,2,3,2,1,1,2,1,5,2,1,1,4,1,4,1,1,1,4,4,1,1,1,1,1,1,4,1,1,1,2,1,1,2