From 7e1f3d0f3d281d19fe34d714b4838f9aa8206821 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Mon, 6 Dec 2021 00:16:48 -0500 Subject: [PATCH] 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. --- 6/a.py | 28 ++++++++++++++++++++++++++++ 6/b.py | 28 ++++++++++++++++++++++++++++ 6/input | 1 + 3 files changed, 57 insertions(+) create mode 100755 6/a.py create mode 100755 6/b.py create mode 100644 6/input diff --git a/6/a.py b/6/a.py new file mode 100755 index 0000000..0e1c18e --- /dev/null +++ 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 new file mode 100755 index 0000000..e2979bd --- /dev/null +++ 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 new file mode 100644 index 0000000..e2e33f5 --- /dev/null +++ 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 -- libgit2 1.8.1