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.
-
advent-of-code-2021
My solutions for Advent of Code 2021: https://adventofcode.com/2021 (by BradonZhang)
-
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
Discontinued My solutions to Advent of Code 2021. (My first completed AoC!) (by terminalnode)
-
SaaSHub
SaaSHub - Software Alternatives and Reviews. SaaSHub helps you find the best software and product alternatives
How about a python translation of my haskell solution?
F#, Converted TypeScript solution to F#, List.choose made it more easy. Under 80 lines part 2 only code with Type specific modules.
Seems plenty fast though! And I get nice trees of cubes and their subcubes that are lit. https://github.com/hermanstehouwer/adventofcode2021/blob/main/lib/cube.ex if you are interested.
My subtraction algorithm always generates 6 cuboids, except when there is no intersection. It then discards the ones with size 0.
Rust, 286/106. I just compressed the volume by every distinct coordinate, then summed up all the dx * dy * dz subvolumes. Only required 500MB of RAM! Could've been 8x less with bitvec.
Python (140/111)
Rust
Pluto.jl notebook (requires your AoC session to fetch your input)
My thoughts exactly. I did the cube sub-division thing. In order to not get lost, I directly wrote "clean", tested classes for 1D range and cubes. Overall it was a smooth ride, got me my personal best (1232nd), but at the cost of >20s execution time! Now I feel humbled by the solutions I'm seeing here.
Free Pascal - 2251/1744 34.5 seconds runtime No objects, no recursion After brute forcing part 1, I stared at part 2 until I though only handling the different values of X,Y,Z and letting the grid represent variable size cubes.. only to hit memory size limits anyway... and then I learned how to use BITPACKED array, and was able to eventually brute force part 2. Whew!
Raku 5216/1574.
Elixir, Python 1708/791
Python
C# 3039/1750
JS 727/2619, but was interrupted for ~3 hours doing part 2.
Code available on GitHub.
Rust
My solution in Go. The first part is ugly but wanted to do it faster.
I came up with a similiar solution, but i forgot to include the possibility for multiple turn offs and ons, so your solution helped me out form my own. However, I somehow still get the wrong answer, if anyone could help me I'd really appreciate it :D https://github.com/ckainz11/AdventOfCode2021/blob/main/src/main/kotlin/day22/Day22.kt
My code is not yet as efficient as it could be: i takes about 4-5 seconds for both parts.
Rust
Should've seen that coming in part two
Golang
I used the same basic idea in my non-recursive solution. However mine is MUCH slower, probably because it requires a lot of list manipulation that isn't necessary in the recursive approach.
In the rest of the code I simply iterate through cubes and multiply the edges with the saved values.
rust
PHP
Well, I ain't proud of today but it is what it is. Egel code for day 22, task 2, and a with pretty colors.
Python takes around 1 minute for both parts, no extra libraries.
Here’s pretty much the same thing translated to TypeScript: https://github.com/N8Brooks/deno_aoc/blob/main/year_2021/day_22.ts
Haskell (Part 1 only)
Ha! I had the same idea. I feel smart now :) Rust
Python: set arithmetic... with cubes
My Solution
warning, code is crazy ugly
Your first idea wasn't that bad, if you just figured out a smarter way to generate less cuboids, like in this solution or in mine.
Witness the weirdness for yourself at this here link: https://github.com/codemicro/adventOfCode/tree/master/challenges/2021/22-reactorReboot (includes screenshots)
Rust GitHub. My approach is to go through all cuboids and always remove their intersection with all previously added cuboids. Then, If it is a "on" cuboid it is added to the list of cuboids. This way no cuboids will overlap and I can sum all their volumes in the end.
full code
Kotlin, Python, and Rust solutions are the same solution but much uglier.
Rust https://github.com/Crazytieguy/advent-2021/blob/master/src/bin/day22/main.rs Imagining the geometry of this was very tricky for me. For a while I contemplated if calculating the intersection of each pair of cuboids would give me enough information to know how many cubes are on at the end, and finally decided that I would also have to calculate the intersections of those and so on, so I gave up. Instead I decided that my state will be a vector of cuboids that are garuanteed to be non overlapping, and on each command I would subtract the current cuboid from each of these non overlapping cuboids (leaving 1 to 6 cuboids depending on intersection), and finally either add in the current cuboid (for on command) or leave it out (for off).
Anyone got any ideas why it may fail? Here's my solution. Input data is here and I've triple checked that it's pasted correctly.
Zig
I think my part 2 is one of the simplest solution I have seen so far and still runs decently fast (200 ms on my MB Pro) - even though there are many much faster on here.
Python
code here on github
Rust
thank you! this is the most easy and straightforward approach to this problem. the only downside is the speed — it's not great. my golang solution takes almost 2 seconds to compute final answer.
holy shit, your solution is amazing. It's so clear. It took me a day and a really complicated solution to get the answer in more than 5 mins runtime (https://github.com/Harshsa28/Advent_of_code_2021/blob/main/src/22.py).
Related posts
- [2023] A list of solutions for this year and years past
- [2022 day 19] after trying ever day for the last 11 days, nothing seems to work
- [2022 Days 1-10] [Python] A graphical representation of The Beast, a single line of code that solves every day's challenge so far!
- Looking for good python and rust solutions repos
- -🎄- 2021 Day 10 Solutions -🎄-