Our great sponsors
-
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.
-
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.
-
AdventOfCode2021
Advent of Code Solutions for 21 in rust, shooting for <1ms total runtime by the end (by IndianBoy42)
-
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-kotlin
Solving all 25 days of the AOC 2021 event here: https://adventofcode.com (by mofeejegi)
-
advent-of-code-2021
Discontinued Code I used for solving https://adventofcode.com/2021 (by pavel1269)
-
AoCPractice2018
My personal solutions to the Advent of Code challenges for 2018 and 2019, all in Python.
-
adventofcode
This repo contains (almost) all of my solutions for adventofcode problems. (by kiriDevs)
-
nanim
Nanim is an easy-to-use framework to create smooth GPU-accelerated animations that can be previewed live inside a glfw window and, when ready, rendered to videos at an arbitrary resolution and framerate.
-
SaaSHub
SaaSHub - Software Alternatives and Reviews. SaaSHub helps you find the best software and product alternatives
Wrong how? I get the correct values for my input, and the correct one for the input/result in OPs repo (tested both 7 and 6): https://github.com/honzatlusty/advent-of-code-2021/blob/master/7/
rust and python with benchmarks
You can find it on github: https://github.com/cptwunderlich/adventofcode2021/tree/main/day7
Thanks for the code! I referenced it in my example, for Q1. I couldn't get Q2, as is, to work with their puzzle input tho.
TypeScript. For part 2, calculated fuel cost for average ± 1, and took min.
This is similar to my solutionsolution in Julia (which I'm learning).
Thought I did well, but was still outside the leader board today :(
Part 1 | Part 2
Python. Took me a while to recall the formula...
Python3 <1000 for both, 500ing so I can't see ranks...
Java: https://github.com/abnew123/aoc2021/blob/master/aoc2021/Day7.java
My Scala solution.
Go, ?/? (Leaderboards are temporarily disabled)
1st Challenge
Rust, basic bruteforce solution. Played around with trying to find an analytic solution for a couple mins, but ended up just brute forcing it, and surprisingly, its not absurdly slow.
Day 1
Second Version, remembering Triangle Numbers exist (well, I remembered they existed, I just forgot the formula): https://github.com/Bpendragon/AdventOfCodeCSharp/blob/ea69b3/AdventOfCode/Solutions/Year2021/Day07-Solution.cs
I ended up writing solve functions anyways: https://github.com/nathanvy/AOC2021/blob/main/7.lisp
Java 17, could be shorter, but I find this more readable (Gauss has helped a lot.)
Raku, 3004 / 2778. The following (MIT-licensed) code is a code golf version of what I originally wrote.
C# code tries all positions between first and last crab
I'm not sure if it was strictly necessary to optimize this, but I implemented a binary search for the most efficient fuel value, and just swapped out the fuel calculation function for part two. https://github.com/BreadFish64/AOC/blob/master/AOC/treachery_of_whales.cpp
Link to github
Raku solution
On github
Github
Part 1
Kotlin
My solutions in JS: part one - part two
Easy enough -- Python 3 solution
Full solution is at https://github.com/ethsgo/aoc.
Python
Done in Dataweave
Your solution is so much cleaner than mine
Kotlin: https://github.com/dragneelfps/advent-of-code/blob/master/src/Day7.kt
Ruby
Haskell ????/????
As usual, first implementation. Actually started by doing sum(range(1, steps+1)), and after solving remembered that theres a math thing for that (sum(0..n) = n * (n + 1) / 2). Nothing super fancy other than that. Code Finally starting to see speedup from pypy. That JIT is amazing but has its costs.
elixir Probably not very efficient and quite ugly. But it works... If anyone has any sugestions how to make it better (how to become better @elixir) i would greatly appriciate it
Python https://github.com/fridokus/advent-of-code/blob/master/2021/7.py
Go day 7. I'm pretty sure there is a faster way to do this, just couldn't think of it. GitHub
Ruby: https://github.com/sreedevk/advent-of-code/blob/main/ruby/2021/day7/main.rb
Kotlin solution for day 7, can be done fairly concisely:
My Rust solution
My modern C++ solution github ( compiled C++17 )
Part 1
Compact solution in Rust
Solution in Crab Native Tongue: Rust
My solution in python. Brute force, nothing fancy, using the closed formula for the sum of integers less than n.
The problem space is small enough to explore exhaustively. But I thought I would also give a try to using z3 prover. This is what I came up with for the first part.
Julia. So...I guess I really overthought this one with a bisection algorithm? Sometimes, with Advent of Code, it helps your part 2 if you generalize early. I thought I was being all clever doing a bisection search that minimizes a generic loss function, but now I see that this optimization wasn't necessary.
Github
Python 3 O(n) runtime for both solutions, using median and mean to calculate the best possible meeting place. Part B uses the Gaussian Sum to calculate the cost. Solution A & B
.Python brute force: https://github.com/kemmel-dev/AdventOfCode2021/blob/master/day7/d7p2.py
Todays problem looked perfect for Octave (again). Also did my daily python solution (https://github.com/sotsoguk/AdventOfCode2021/blob/a6102d061994ce5dc1f5dddfa881b65cac40b209/python/day07/day07.py) [Day07.py]
all solutions so far
Python solution (GitHub) using Gauss formula for part 2
Solution in GameMaker Language.
all solutions
Source code. Took me quite a while to realize that 1 + 2 + 3 + ... + n is not a factorial :( Other than that, pretty simple today.
[GitLab link] https://gitlab.com/bastiendussapapb/adventofcode-2021/-/blob/main/Notebook/day7.ipynb
Haskell, using gradient descent. The code's actually short enough to post here! Writeup on my blog and code on Gitlab.
My overengineered Kotlin solution: https://github.com/ThomasBollmeier/advent-of-code-kotlin-2021/blob/main/src/de/tbollmeier/aoc2021/day07/Day07.kt
💡 source code to Day 7 : The Treachery of Whales
The first one I'll actually share, it's already on GitHub
You can try my input if you want. https://github.com/l0zzyy/AdventOfCode/tree/main/2021/7
Part 1 and 2 in Rust. Felt really silly not realizing that part 1 is just the median by definition, and instead going with a complicated DP solution.
solve.py
Full code for Part 1 and Part 2, as well as my ramblings.
Part 1
PowerShell
PHP Part 1 6 lines PHP Part 2 7 lines
My solution in Javascript, kinda brute force, but still under 300 ms
My solution in Clojure, however should optimize part two as it takes 7s and is pretty naive approach.
Part 1
PHP
In the spirit of "premature optimization is the root of all evil, I just brute forced it. The test suite ran in 70ms on my system so that was good enough for me not to worry about further optimization.
Python
Julia: https://github.com/dunefox/adventofcode21/blob/main/7/main.jl
Full repo here.
Part1 and Part 2
Rust: https://github.com/McSick/AdventOfCode2021/blob/main/07/crab-attack/src/main.rs
I don’t like it, but I don’t dislike it enough to change it.
My solution in Python. I realised that I could use the median for part 1 and mean for part 2 like some others here.
Ruby solution (github)
Python Repo (With days 1-7)
PHP, bruteforce. Part 2 below, it uses a formula to calculate the sum of a range. I'll now go check the optimal solutions.
Solution
D
Crab Dance 🦀🕺 solution + animation using nanim https://pietroppeter.github.io/adventofnim/2021/day07.html
code
part1 part2
[Github]
Python monte carlo-like solution. Github
Python 3.8 solution
Finally caught up! Here's the Clojure source and tests for today.
Shortest readable Python (no external libraries) and C++
You can also checkout the solution on my github repo https://github.com/wbratz/adventofcode/tree/master/day7
Puzzle 2 BlockDiagram
My final script is in my repo, but an earlier commit has this brute force version. https://github.com/einarjon/adventofcode.sh in the 2021 folder.
I wrote myself a little AoC framework, which supports testing and performance evaluation. I use Unit tests to run my code against the examples (see here).
Kotlin median and mean. I'm sure there's some simplification that could be done, but I ain't doin' it. Day 8 awaits.
GitHub/CrabSubmarines.java
While benchmarks are the only proper way to get runtimes, a crate like Logging Timer will allow you to accurately spit out times just by annotating a function. See https://github.com/dwalker109/aoc-2021/blob/main/day07/src/main.rs for an example.
F#
Straightforward Kotlin solution
I messed up. First I tried getting the mean average position to move all the crabs to, but that number was way off. So then I tried bisecting my way to an answer: https://github.com/ElGoorf/aoc2021/blob/main/07/01.js
Golang solution (I am learning the language via Advent of Code): https://github.com/gwpmad/advent-of-code-2021/blob/main/7/main.go
Related posts
- -🎄- 2021 Day 6 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