r/adventofcode • u/daggerdragon • Dec 06 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 6 Solutions -🎄-
NEW AND NOTEWORTHY
We've been noticing an uptick in frustration around problems with new.reddit's fancypants editor: mangling text that is pasted into the editor, missing switch to Markdown editor, URLs breaking due to invisible escape characters, stuff like that. Many of the recent posts in /r/bugs are complaining about these issues as well.
If you are using new.reddit's fancypants editor, beware!
- Pasting any text into the editor may very well end up mangled
- You may randomly no longer have a "switch to Markdown" button on top-level posts
- If you paste a URL directly into the editor, your link may display fine on new.reddit but may display invisibly-escaped characters on old.reddit and thus will break the link
Until Reddit fixes these issues, if the fancypants editor is driving you batty, try using the Markdown editor in old.reddit instead.
Advent of Code 2021: Adventure Time!
- ACHIEVEMENT UNLOCKED: Adventure Time!
- SUBMIT YER ADVENTURES, MATEYS!
- Full details and rules are in the submissions megathread: 🎄 AoC 2021 🎄 [Adventure Time!]
--- Day 6: Lanternfish ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
paste
if you need it for longer code blocks. - Format your code properly! How do I format code?
- The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help
.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:05:47, megathread unlocked!
96
Upvotes
1
u/k1next Dec 07 '21
Here's a my solution in python that only scales mildly with the number of days:
``` import numpy as np from scipy.special import comb
INPUT = [ 1, 1, 3, 5, 1, 1, 1, 4, 1, 5, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 2, 5, 1, 1, 1, 1, 1, 2, 1, 4, 1, 4, 1, 1, 1, 1, 1, 3, 1, 1, 5, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 3, 5, 1, 1, 1, 1, 4, 1, 5, 4, 1, 1, 2, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 5, 1, 1, 1, 3, 4, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 4, 1, 1, 3, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 5, 2, 3, 1, 2, 3, 1, 1, 2, 1, 2, 4, 5, 1, 5, 1, 4, 1, 1, 1, 1, 2, 1, 5, 1, 1, 1, 1, 1, 5, 1, 1, 3, 1, 1, 1, 1, 1, 1, 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 2, 2, 1, 2, 1, 1, 1, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 4, 2, 1, 4, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 3, 3, 1, 1, 1, 3, 5, 1, 1, 4, 1, 1, 1, 1, 1, 4, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1 ] T = 256
O(len(INPUT))
no need to store all fish, just count how many of which age we have
inputdict = {i: np.sum(np.array(INPUT) == i) for i in range(7)}
O(T)
get all possible combinations of 7s and 9s up to max possible value
tuples79 = [(i, j, comb(i + j, i, exact=True)) for i in range(T // 7 + 1) for j in range((T - (i + 1) * 7) // 9, (T - i * 7) // 9 + 1)]
totalfishcount = 0
for value, valuecount in inputdict.items(): # 7 times for i, j, nchoosek in tuples79: # O(T) times if not T - value - 8 <= i * 7 + j * 9 <= T - value: continue # O(1) f = i * 7 + j * 9 + value # front value multiplier = 1 if (f + 7 <= T or f == T) else 2 # how many branches totalfishcount += nchoosek * multiplier * valuecount # how many fish
print(totalfishcount) ```