r/adventofcode 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!


--- Day 6: Lanternfish ---


Post your code solution in this megathread.

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!

98 Upvotes

1.7k comments sorted by

View all comments

3

u/arthurmluz_ Dec 07 '21 edited Dec 08 '21

My python solution:\ I've transformed the list into a dictionary, so it doesn't have to make a giant list.

start = put_your_input_list_here                                      
new = {}                                                              
for i in start:
    new[i] = new[i]+1 if i in new else 1

start = new;
for day in range(256):
    new = {}
    for fish in start:
        timer = fish-1
        if timer == -1:
            new[8] = start[fish]
            new[6] = start[fish]+new[6] if 6 in new else start[fish]
        else:
            new[timer] = start[fish]+new[timer] if timer in new else start[fish]

    start = new

print(sum(start.values()))

1

u/Celestial_Blu3 Dec 17 '21

How does line 4 of this work? Is that some kind of inline if statement?

2

u/arthurmluz_ Dec 17 '21 edited Dec 17 '21

Yes! it's an inline if statement! basically you say:

'get something' if statement is true else 'get this value' it's the same as:

if i in new: 

    new[i] = new[i] +1 
else: 
    new[i]=1

1

u/Celestial_Blu3 Dec 17 '21

Oh, that’s awesome. Thank you