Our great sponsors
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
-
advent-of-code-go
All 8 years of adventofcode.com solutions in Go/Golang; 2015 2016 2017 2018 2019 2020 2021 2022
-
adventofcode
Advent of Code solutions of 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 and 2023 in Scala (by sim642)
-
Advent-of-Code-2021
Made it through all 25 days of Advent of Code for the second time! (by Leftfish)
-
AoC
Advent of Code - My solutions, in multiple programming languages - See https://adventofcode.com/ (by gubatron)
-
advent-of-code-2021
Discontinued Code I used for solving https://adventofcode.com/2021 (by pavel1269)
-
Advent-of-Code
This repo contains my answers for the 25 days of coding challenge at https://adventofcode.com (by BeckTimothy)
-
advent-of-code-my-solutions
These are my solutions for the advent-of-code puzzles. In bash for 2021, and rest in Go
-
SaaSHub
SaaSHub - Software Alternatives and Reviews. SaaSHub helps you find the best software and product alternatives
python and rust no external libs were used, including benchmarking code for both to measure timing performance
One can do matrix exponentiation to calculate the value, which runs in time O (log D). See my implementation on GitHub.
Very similar to mine, I used .rotate(1) on my ages array then added the element at the end to the element at index 6. https://github.com/MichaelCG8/aoc2021/blob/main/src/bin/06.rs
Python solution using deque (GitHub). Much faster than my first attempt, lol
F#, Readable simple 4 functions, opted for for loop and Array mutation
Loving it! Your code is similar in structure as mine but you did a much more concise job :)
Cleaned-up solution.
Python
Good exercise on when to a hash table (or in this case, a Counter)
Rust. Unsurprisingly, I did the naive thing for part 1 and then had to re-write for part 2. I'm happy with how clean part 2 is, I just used a Rust array for the counts -- though I'm sure there's a more clean solution for shifting an iterator.
Github
My Scala solution.
Go 1237/4880
JS, 79/2651
Part 1
Racket: https://github.com/Bogdanp/aoc2021/blob/8bc6e8f772885b8ada3b7bdda09d42feb680e567/day06.rkt
C# solution same as most others but with a bit of LINQ
Kotlin
Raku 7425/4406 (thanks to some really confusing compiler errors about ways that Whatever and lambdas can't be used.
Python3
Python 3
Python
Full code
Solved it in Elixir. Here's the link
Go day 6. It's a little verbose but you have to remember to map the days, not the fish 😂 GitHub
Also, a Web Assembly solution.
Checkout the full repo: wasi0013/advent_of_code on GitHub
Elixir
I got part 1 fairly quick even though I overengineered it at first. Turned out to be much easier than I thought. Just keep track of the numbers of fish at each age, rather than tracking instances of fish!
My Rust Solution
My solution in Rust. Runs pretty fast: ~4 microseconds. =D
Java nothing special:
Python solution that takes about a second for part 2. The logic is the same for both parts, just changed the number of days it runs for.
Haskell 6676/2310
My personal solution uses an array (https://github.com/abnew123/aoc2021/blob/master/aoc2021/Day6.java ) although the fact 9 is hardcoded is a bit annoying to me (thinking of changing to a list).
My solution in Javascript, got it down to O(n) after 90 minutes of desperation... pretty sure today's puzzle is taken from a known algorithm, will find out!
Solution and writeup here: https://github.com/xavdid/advent-of-code/tree/main/solutions/2021/day_6
https://github.com/A-UNDERSCORE-D/aoc2021/blob/main/aoc/go/six/06.go -- Nothing super fancy. Nice ability to pick arrays over slices here though. Probably contributes a bit of speedup.
Kotlin, nothing too fancy. Since my template only accepts Int as a result I had to improvise (and maybe should adjust my template)
Neat TypeScript solution! https://github.com/rajitbanerjee/advent-of-code/blob/master/2021/day06-lanternfish/expo.ts
My first thoughts were OOPish, but then I read the puzzle again and saw the dreaded word: 'exponential'. So here's another Python solution with deque.
Haskell
12834/8563 [Python3] First one I'm actually kind of proud of. 1:06 between submissions gets me 4000 places. Took so long (to enter a different 'days') because I had to get off a train before I could sit down again lol. Github for all days: https://github.com/Bulkje/AdventofCode2021
Kotlin solution using a fish counter map:
My solution in Java
C# code
code
Good ol' C code. Very simple program once you think of making a circular buffer from the population histogram by age bins, and simulating every day. I got stuck for a while trying to come up with a direct mathematical function which never gave me the exact number. The crux:
Python solution!
I've been using this as an opportunity to spend more time learning parts of FP-TS (https://github.com/gcanti/fp-ts)
Rust 4.4us Solution:
Code in Github
C# Quick and easy this week! Just needed to change one number to solve Part 2! https://github.com/LEPT0N/toybox/commit/8344ca9e88f58fec5bc96a00feb6f6f0796e61f1
Day 6 in TypeScript, the naive approach used for part 1 made my node vm crash twice, but the new approach brought the execution time down to <1ms
As I suppose many of us (haven't read the thread yet), I wrote an elegant OO solution with a LanternFish and LanternSchool class that worked fine for part 1, but had no chance of doing part 2. (See GitHub.)
Super proud of my solution using Python + NumPy, which is considerably fast.
Part 1 w/ nice-looking OOP (Python)
Python. Figured part 2 would render a naive brute-force approach unusable (as it usually is with these kinds of puzzles) so I tried to come up with an efficient solution from the get go. Results pretty much instantly for both parts.
Part 1 0.0027ms (2.7μs)
github - codesandbox
My solution in python, partitioning the population by day until reproduction, and keeping track of the group dynamics through time.
Here's my solution anyway, definitely learned a lot today.
Python3 using Counter from the Collections module. Runs in about 40ms. (Github)
My solution in Clojure, although need to perform some time on optimization as the current version is really slow... Feel free to suggest possible improvements
Once again a BASIC solution. I was a little worried about computation times but it was quite doable after all
c# (.cs for ctrl+f users) implementation using a `dict`
Source Writeup
Slightly altered this to work outside my repository. Total execution time for the actual puzzle input on my poor old PC was around 0.0003 seconds, so I'm quite pleased with my efficiency today in not caring about the life of single fish ;)
Yeah, I should've expected part 2 to have a ridiculous amount of fish... I went with a HashMap for saving how many fishes were at ages 0-8 each day. These are my favourite types of exercises, simple code and the solution is not immediately apparent. Cleanest code yet! GitHub
Rust GitHub
GitHub Link
Scala
Like most other folks, I did the naive approach for part 1 and then part 2 was untenable. Props to everyone who did the smart thing from the beginning. As always, my full code including tests is on Github.
Racket https://github.com/whiter4bbit/advent-of-code/blob/master/2021/day6.rkt
Python 3: https://github.com/Laudian/adventofcode/blob/main/2021/06/day06_01.py
Code itself is here: https://github.com/madisp/aoc_kotlin/blob/main/src/main/kotlin/day6_fast.kt
I use https://github.com/gobanos/cargo-aoc which is based on Criterion.rs. The measured time does not include parsing.
Egel
Same approach as mine.
I'm sure I could trim a bit of fat here, but why bother?
I had a similar idea, but used a list instead of a map so I could do pattern matching on it to easily get the numbers and create the new list. GitHub (only look at part 2, part 1 is way too slow :D).
Part 2 in Julia. Tricky! I definitely did part 1 by modeling every fish before realizing how dumb that was.
With each day puzzles are becoming more and more challenging and interesting. This is my solution using: PHP
typescript: https://github.com/nesro/adventofcode/blob/master/2021-day-06.ts
nodejs solution https://github.com/k-koehler/code-challenges/blob/master/adventofcode/2021/day6/lanternfish.js
So far the easiest day in Rust: https://github.com/pavel1269/advent-of-code-2021/blob/main/src/day06/mod.rs
Rust: github link I thought to group the fish right from the start so the second part involved no work. Clocks in at 175µs for part 2.
3 Solutions in Rust: Stupid, Big Brain and Sensible
Pyxell
github
https://gitlab.com/autra/adventofcode https://gitlab.com/feike/adventofcode https://github.com/dflss/advent-of-code-2021 https://github.com/qwesda/AOC2021-postgresql
https://gitlab.com/autra/adventofcode https://gitlab.com/feike/adventofcode https://github.com/dflss/advent-of-code-2021 https://github.com/qwesda/AOC2021-postgresql
https://gitlab.com/autra/adventofcode https://gitlab.com/feike/adventofcode https://github.com/dflss/advent-of-code-2021 https://github.com/qwesda/AOC2021-postgresql
https://gitlab.com/autra/adventofcode https://gitlab.com/feike/adventofcode https://github.com/dflss/advent-of-code-2021 https://github.com/qwesda/AOC2021-postgresql
Code for today here.
Group the fish by their timer, which can be anything from 0 to 8. The neat part is the rotate_left to decrease the timers of each group. Fish with timer 0 will automatically end up at timer 8, and so the only thing left to do is adding another copy with timer 6. Core function: rust fn multiply(timers: Vec, generations: usize) -> usize { // Index equals timer value, so index 0 contains the count of fish with timer 0 let mut counts_by_timer = vec![0usize; 9]; timers.into_iter().for_each(|f| { counts_by_timer[f] += 1; }); for _ in 0..generations { // A left rotation represents the timer (=index) decreasing by 1. // The fish with timer 0 will not only produce new fish with timer 8, // but also reset their timer to 6 let count_of_fish_with_timer_0 = counts_by_timer[0]; counts_by_timer.rotate_left(1); counts_by_timer[6] += count_of_fish_with_timer_0; // == counts_by_timer[8] } counts_by_timer.into_iter().sum() } Full code at Github
Python: under 0.5 ms for both solutions https://github.com/bluhb/AdventOfCode2021/blob/master/Project/Day6/main.py
Python 3 Got my solution running in less than 1ms using Counters
I'm just horrible at formatting my code... So I figured I would post the link. Anyway, one of my first attempts at F#, and I love it :D https://github.com/ClaerhoutN/AdventOfCode/blob/master/AOC_2021_6/Program.fs
My solution in Python. Today was fun and very easy. I immediately came to the same conclusion as others here to simply count the number of fish per age group.
My other solutions are here: https://github.com/WilliamLP/AdventOfCode/tree/master/2021
My solution in Rust, took 158seconds to finish part 2 using multithreaded programming. 3ms for getting the solution to 80 days, pretty proud (day 6 of using Rust as well). I first tried to find exponential rate, but to no avail (I came pretty close)
Here's my solution using F# and Jupyter Notebook. Took the approach many people took here to keep track of the sum of fishes at for each timer value. Then step through each day.
A fitting solution to the lantern fish puzzle in fish code.
Julia: I had fun re-writing my original solution (which used bins) with a memoized recursive call.
View my code on Github: https://github.com/BeckTimothy/Advent-of-Code/blob/master/2021/12-06-21/challenge-1/script.js
Python 3: a quite expected implementation for both parts, using a dict
As I see there is no bash solution yet, here is mine: https://github.com/ColasNahaboo/advent-of-code-my-solutions/blob/main/bash/2021/days/d06-1.sh Runs in less than 100ms. It is a linear solution with bins, but the bins are not based on the fishes timers. There is a bin for each day, with the number of timers reaching zero ==> fishes born on the next day.
Part 1 and 2 Combined
C# https://github.com/Joamoi/AdventOfCode2021/blob/main/Day6/Program.cs
Github
https://github.com/Marterich/AoC/blob/main/2021/day06/solve.py
[gitHub - focus on readability](https://github.com/KorbinianSchuster/AdventOfCode2021/blob/master/6/6.py)
GitHub
Rust
After having a solution in Python (which looks like everybody else's solution ;) (https://github.com/sotsoguk/AdventOfCode2021/blob/36c224078a3370e5ea8383a74f71f1a334ed1077/python/day06/day06.py)
Keeping newborn fish in a different array makes things easier to follow.https://github.com/EnisBerk/adventofcode/blob/master/day6/tools.py
That's what I did for part one. Didn't find it necessary for part 2
I moved the index to the "zero days left" slot on by 1 each day (conceptually anyway), instead of moving any values: My Rust solution
rust https://github.com/yorhodes/advent-of-code/blob/master/2021/day6/src/main.rs love when this happens fn main() { println!("part 1: {}", part_1(DATA, 80)); println!("part 2: {}", part_1(DATA, 256)); }
solved it pretty fast with an object, then realized that using an object/hashtable with 0-8 as keys is pretty much the same as using an array. But whatever. github link
My Kotlin solution
OCaml solution:
Part 1 was a direct stating of the problem - I read in all of the fish, and for eighty iterations, applied the simulation rules.
You can also add https://github.com/xocolatl/advent-of-code/ to your list (it's https://twitter.com/pg_xocolatl a pg contributor). He was such an inspiration to me last year.
Python Implementation
Python Part 1 and Part 2
You might be interested in OffsetArrays.jl.
Catching up from a set back on day 5 (I almost had a working sweep algorithm but gave up) and the weekend in general. Here's the Clojure source and tests. As usual, feedback welcome!
My F# solution.
Full code: https://github.com/nicuveo/advent-of-code/blob/main/2021/haskell/src/Day06.hs Stream: https://www.twitch.tv/videos/1227052466
Super short solution in Clojure. I have a write-up for explaining the process of optimizing the algorithm.
Nice! I got stuck with part2, I didn't realize that you can count per day and tried to solve it as exponential growth math problem. Eventually, I looked at your solution and got the idea. Here's my version. First time doing aoc and new to Scala.
GitHub/Lanternfish.java
[Source Code]
I did this day in Clojure. The secret is in the function naming. justinhj/adventofcode2021-day6
Related posts
- -🎄- 2021 Day 7 Solutions -🎄-
- Demystifying bitwise operations, a gentle C tutorial
- [2022] [Angular / Typescript] Collection of all visualizations I created this year as an interactive website
- How to organize Rust code for Advent of Code puzzles?
- [2022 Day 4] [Angular / Typescript] Interactive website to visualize your inputs