🦌 - 2023 DAY 9 SOLUTIONS -🦌
Day 9: Mirage Maintenance
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒 Thread is locked until there's at least 100 2 star entries on the global leaderboard
🔓 Unlocked after 5 mins
25 replies
Nim
Pretty easy one today. Made a
Pyramidtype to hold the values and their layers of diffs, and anextendfunction to predict the next value. For part 2 I just had to make anextendLeftversion of it that inserts and subtracts instead of appending and adding.Hi there! Looks like you linked to a Lemmy community using a URL instead of its name, which doesn't work well for people on different instances. Try fixing it like this: ![email protected]
Nim
Part 1:
The extrapolated value to the right is just the sum of all last values in the diff pyramid.
45 + 15 + 6 + 2 + 0 = 68Part 2:
The extrapolated value to the left is just a right-folded difference (right-associated subtraction) between all first values in the pyramid. e.g.
10 - (3 - (0 - (2 - 0))) = 5So, extending the pyramid is totally unneccessary.
Total runtime: 0.9 ms
Puzzle rating: Easy, but interesting 6.5/10
Full Code: day_09/solution.nim
Snippet:
(Cursed) Python
I solved the actual thing recursively in Rust, but I decided that wasn't cursed enough, so I present: Polynomial fitting!
APL
I finally managed to make use of ⍣ :D
Rust
Discrete derivatives!
Crystal
recursion is awesome! (sometimes)
Ruby
[email protected] [LANGUAGE: Ruby]
I found today really easy thankfully. Hardest part was remembering the language features haha
https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day09/day09.rb
edit: code golfing this one was easy too! man this day really worked out huh
code golf
Rank 148!! Even beat Leo Uino today!
Optimized: https://codeberg.org/Sekoia/adventofcode/src/branch/main/src/y2023/day9.rs
Less optimized, though not quite my initial version: https://codeberg.org/Sekoia/adventofcode/commit/72dfd77b92518aefd9dbe3e661885528f737f861
how in the world are you getting top 1k with rust? sheesh!
parser!(lines(repeat_sep(i64, " ")))Today was pretty ideal for my setup. In general I think Rust is really good for later days, because the safety and explicitness make small mistakes rarer (like if you get an element from a HashMap that doesn't exist, you don't get a None later down the road (unless you want it, in which case it's explicit), you get an exception where it happened.
I just really like Rust :3
I guess I'll have to take rustaceans who claim they're more productive in rust than python seriously now
Scala3
Python
Easy one today
:::spoiler code
:::
Dart
I was getting a bad feeling when it explained in such detail how to solve part 1 that part 2 was going to be some sort of nightmare of traversing all those generated numbers in some complex fashion, but this has got to be one of the shortest solutions I've ever written for an AoC challenge.
I even have time to knock out a quick Uiua solution before going out today, using experimental recursion support. Bleeding edge code:
Language: Python
::: spoiler Part 1
Pretty straightforward. Took advantage of
itertools.pairwise.:::
::: spoiler Part 2
Only thing that changed from the first part was that I used
functools.reduceto take the differences of the first elements of the generated sequences (rather than the sum of the last elements for Part 1).:::
GitHub Repo
[Language: Lean4]
This one was very easy, almost trivial. Lean4 did demand a proof of termination though, and I'm still not very good at writing proofs...
I'm also pretty happy that this time I was able to re-use most of part 1 for part 2, and part 2 being a one-liner therefore.
As always, here is only the file with the actual solution, some helper functions are implemented in different files - check my github for the whole project.
:::spoiler Solution
:::
C#
Used recursion to determine the differences for part 1 and then extracted the variations in processing from predicting the end vs. the beginning of the history and passed them in as Func variables to the recursive method.
Day 9
Snippet:
TypeScript
GitHub link
It's nice to have a quick easy one for a change
::: spoiler Code
:::
Using a class here actually made part 2 super simple, just copy and paste a function. Initially I was a bit concerned about what part 2 would be, but looking at the lengths of the input data, there looked to be a resonable limit to how many additional rows there could be.
::: spoiler python import re import math import argparse import itertools
:::
Language: Python
Github
Raku
First time using Grammar Actions Object to make parsing a little cleaner. I thought about not keeping track of the left and right values (and I originally didn't for part 1), but I think keeping track allows for an easier to understand solution.
View code on github
edit: although I don't know why
@values.all != 0evaluates to true why any value is not zero. I thought that@values.any != 0would do that, but it seems that their behavior is flipped from my expectations.edit2: Oh, I think I understand now.
!=is a shortcut for!==, and!==is actually the equality operator that is then negated. You can negate most relational operators in Raku by prefixing them with!. So the junction is actually binding to the==equality operator and not the!==inequality operator. Therefore@values.all != 0becomes!(@values.all == 0). I'm not sure why they would choose this order of operations, though.edit3: Ah, it's in the documentation, so it's not even an oversight. https://github.com/rakudo/rakudo/issues/3748
:::spoiler Code (probably still doesn't render correctly)
:::
A pretty simple one today, but fun to do. I could probably clean up the parsing code (AKA my theme for this year), and create just one single vector instead of having the original history separated out from all of the sequences, but this is what made sense to me on my first pass so it's how I did it.
https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day09.rs