From 34d9cf0260a953714fa3bb5a16b6caa5eae02d4f Mon Sep 17 00:00:00 2001 From: Benjo Date: Mon, 6 Dec 2021 10:27:03 +0100 Subject: [PATCH] day 6 --- 06/06.py | 50 ++++++++++++++++++++++++++++++++++---------------- 06/input | 1 + 2 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 06/input diff --git a/06/06.py b/06/06.py index 66b8a8e..3a8e89f 100644 --- a/06/06.py +++ b/06/06.py @@ -2,32 +2,50 @@ import numpy as np def readFish(): - with open("testInput") as file: + with open("input") as file: return np.array(file.read().split(","), dtype=np.uint8) -def getCount(fish, days): - return 0 +def getCount(fish, daysLeft, usedList, usedMap): + if daysLeft <= fish: + return 1 + + if usedList[fish][daysLeft]: + return usedMap[(fish, daysLeft)] + + count = 1 + for d in range(0, daysLeft - fish, 7): + count += getCount(9, daysLeft - fish - d, usedList, usedMap) + + usedList[fish][daysLeft] = True + usedMap[(fish, daysLeft)] = count + + return count def solve(days): - fish = readFish() + # need to save/read known results for performance + usedMap = {} + usedList = np.zeros((10, days + 1), bool) + + fish = readFish() count = 0 for f in fish: - count += getCount(f, 80) + count += getCount(f, days, usedList, usedMap) + + # numpy version too slow for part 2 + # for i in range(days): + # decrease = fish > 0 + # zeros = fish == 0 + # fish[decrease] -= 1 + # fish[zeros] = 6 + # fish = np.concatenate((fish, zeros[zeros].astype(np.uint8) * 8)) + # print(i, len(fish)) - for i in range(0): - decrease = fish > 0 - zeros = fish == 0 - fish[decrease] -= 1 - fish[zeros] = 6 - fish = np.concatenate((fish, zeros[zeros].astype(np.uint8) * 8)) - print(i, len(fish)) + return count - return len(fish) +print(solve(80)) +print(solve(256)) -# print(solve(80)) -import cProfile -cProfile.run("print(solve(200))") diff --git a/06/input b/06/input new file mode 100644 index 0000000..3d65700 --- /dev/null +++ b/06/input @@ -0,0 +1 @@ +3,5,1,2,5,4,1,5,1,2,5,5,1,3,1,5,1,3,2,1,5,1,1,1,2,3,1,3,1,2,1,1,5,1,5,4,5,5,3,3,1,5,1,1,5,5,1,3,5,5,3,2,2,4,1,5,3,4,2,5,4,1,2,2,5,1,1,2,4,4,1,3,1,3,1,1,2,2,1,1,5,1,1,4,4,5,5,1,2,1,4,1,1,4,4,3,4,2,2,3,3,2,1,3,3,2,1,1,1,2,1,4,2,2,1,5,5,3,4,5,5,2,5,2,2,5,3,3,1,2,4,2,1,5,1,1,2,3,5,5,1,1,5,5,1,4,5,3,5,2,3,2,4,3,1,4,2,5,1,3,2,1,1,3,4,2,1,1,1,1,2,1,4,3,1,3,1,2,4,1,2,4,3,2,3,5,5,3,3,1,2,3,4,5,2,4,5,1,1,1,4,5,3,5,3,5,1,1,5,1,5,3,1,2,3,4,1,1,4,1,2,4,1,5,4,1,5,4,2,1,5,2,1,3,5,5,4,5,5,1,1,4,1,2,3,5,3,3,1,1,1,4,3,1,1,4,1,5,3,5,1,4,2,5,1,1,4,4,4,2,5,1,2,5,2,1,3,1,5,1,2,1,1,5,2,4,2,1,3,5,5,4,1,1,1,5,5,2,1,1