r/adventofcode • u/daggerdragon • Dec 09 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 9 Solutions -❄️-
THE USUAL REMINDERS
- All of our rules, FAQs, resources, etc. are in our community wiki.
Outstanding moderator challenges:
Community fun event 2023: ALLEZ CUISINE!
- Submissions megathread is now unlocked!
- 13 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's secret ingredient is… *whips off cloth covering and gestures grandly*
Marketing
Every one of the best chefs in the world has had to prove their worth at some point. Let's see how you convince our panel of judges, the director of a restaurant, or even your resident picky 5 year old to try your dish solution!
- Make an in-world presentation sales pitch for your solution and/or its mechanics.
- Chef's choice whether to be a sleazebag used
carsled salesman or a dynamic and peppy entrepreneur elf!
ALLEZ CUISINE!
Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!
--- Day 9: Mirage Maintenance ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks
8
u/CCC_037 Dec 09 '23
[Language: Rockstar]
[Allez cuisine!]
I've broken from my normal fictional inspiration to present a quick ad. Which can be found here
(Yes, the code is the sales pitch)
3
u/WilkoTom Dec 09 '23 edited Dec 09 '23
[Language: Rust] [Allez Cuisine!]
There's no backward in going forward, as my grandfather used to say. Except for my answer to part 2 of this puzzle, which is taking backwards and flipping it around.
The best way to market something is through word of mouth, Short of spamming everyone you know to spam everyone they know, the next best way is to advertise a new product is to make sure everyone knows how great a cook you are already! Here's a flyer for the newest dish on my menu.
3
u/zup22 Dec 09 '23
[LANGUAGE: C++23]
Today I learned that you can create a compile-time infinite loop which eats all your RAM with recursive templates 🙃
Otherwise, pretty easy. Only facepalm moment was accidentally omitting minus signs when parsing the input. Runs in about 600 us. Github
2
u/phord Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
Ugh. I spent too long parsing the instructions. The answer was pretty simple, though. I didn't notice until the end that I didn't actually have to modify the rows to get their history, though. We just need to calculate the lead and tail values themselves.
Getting a Pascal's Triangle vibe. Surely there's a simpler method...
2
u/Sese_Mueller Dec 09 '23 edited Dec 09 '23
[Language: Rust] Was quite proud to have used combinatorics but the posed problem wasn’t optimization-focussed…
I could have just constructed the history instead. (Something something premature optimisation)
3656/2947 but very fast execution: less than 600 µs.
Edit: code
2
u/ASPICE-ai Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
Using built in function all to check for all zeros:
part2
2
u/pwnsforyou Dec 09 '23 edited Dec 09 '23
[LANGUAGE: rust]
For part 2 just do a
.rev()on the input. link2
u/flwyd Dec 09 '23 edited Dec 09 '23
[Language: Julia] (on GitHub)
My only problem was that after I'd implemented a recursive function to return the correct answer I thought I needed to return the sum of numbers in the right-hand diagonal. So I implemented that, noticed my answer on the example was way wrong, and then deleted all that. Since we've already had two punch-card challenges, why not put this on a third, this time with plenty of vertical whitespace:
The winner here is Julia's ability to subtract the first N numbers of an array from the last N with a simple
nums[2:end] - nums[1:end-1]. I was unfortunately unable to passendas an index parameter, so I passed a function to get the relevant element for part 1 vs. part 2.2
u/bofstein Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Google Sheets]
At first I thought I was going to have to finally learn how to do a recursive function. But then I realized that it couldn't generate more new rows than the number of columns, which was only 21, so it wouldn't be that hard to just build them all out. Each new set is the difference of the prior, with one column at the end deleted. Then for part 1 I added a new column that adds the new number below it, and for part to I subtracted the number below it.
Bulk of the time spent was mostly on finding dumb copy paste errors in the large doc.
https://docs.google.com/spreadsheets/d/1yQ3HuWPN6C_ACgF1P6lXGmwm-s-dVH8UH8X18NKuflE/edit?usp=sharing
2
u/wherrera10 Dec 09 '23
[LANGUAGE: Julia]
No need for an option to reverse when there's `pushfirst!()` to [do it all in one pass].
2
u/b0xc4t Dec 09 '23
[Language: Rust]
Both parts run in under 100us
2
u/Hoinkas Dec 09 '23 edited Dec 10 '23
[LANGUAGE: Python]
Fast Python script with numpy.diff option and, separately, Lagrange interpolating one
https://github.com/Hoinkas/AdventOfCode2023/blob/main/9_Day/9_Day_Puzzle_Hoinkas.py
2
u/wlmb Dec 09 '23 edited Dec 10 '23
[LANGUAGE: Perl]
Analysis: https://github.com/wlmb/AOC2023#day-9
Part 1: https://github.com/wlmb/AOC2023/blob/main/9a.pl
Part 2: https://github.com/wlmb/AOC2023/blob/main/9b.pl
2
u/bamless Dec 10 '23 edited Dec 11 '23
[LANGUAGE: J*] To be honest i was expecting part 2 to change the rule for how the history of a reading needed to be processed, so i coded it as a generic convolution with a filter...
Well, it didn't turn out that way :):
2
u/dommmyrock Dec 10 '23 edited Dec 11 '23
[LANGUAGE: Rust]
Part 1 and 2 https://github.com/dommyrock/aoc/blob/main/aoc_2023/day-09/src/bin/part1_2.rs
Macro to calculate differences , than sum up last / first elements.
32
u/Substantial_Sign_827 Dec 09 '23 edited Dec 11 '23
[LANGUAGE: PYTHON3]
Notice that the differences in each "layer" are divided differences with the denominator equal to 1. Therefore, basically, each value in the original array is a value generated by a polynomial P(x), and the 0th,1st,2nd,... elements are corresponding to P(0), P(1), P(2),...
Suppose the array has n elements corresponding to P(0), P(1),..., P(n-1):
- Part 1 requires us to find P(n)
- Part 2 requires us to find P(-1)
By applying Lagrange's interpolation formula, we will have a straightforward solution.
22
u/DFreiberg Dec 09 '23
[LANGUAGE: Mathematica] Mathematica, 820 / 544
Using Mathematica was almost cheating today; it took me eight minutes to understand what the problem was asking for, thirty seconds to code the solution, and one minute to wait for the timeout clock to expire so I could fix my off-by-one error.
Part 1:
Part 2:
18
u/4HbQ Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python] Code (6 lines)
Finally a day where recursion really shines!
Update: I was able to make it punchcard-sized using some lambda expressions:
15
u/gemdude46 Dec 09 '23
[LANGUAGE: Python]
Simple python 10-liner for both parts
12
u/ultranarcolepsy Dec 09 '23 edited Dec 10 '23
[LANGUAGE: Dyalog APL]
10
u/jonathan_paulson Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python 3] 11/11. Solution. Video.
Thinking about it recursively makes this problem easy. The next number is the last number plus the next number in the difference array (recursive call), or 0 if the input is all 0 (base case).
A nice trick I didn't spot: for part 2, you can just reverse the initial sequence - the previous number of the sequence is the next number of the reverse of the sequence!
10
u/timvisee Dec 09 '23 edited Dec 10 '23
[Language: Rust]
Using Binomial coefficient and Pascal's triangle wiki. Day 1 to 9 still under 1 millisecond in total! 🚀
I actually like my naive implementation better, though it's a bit slower. Efficient with an array on the stack, and reusing it for counting.
9
u/relativistic-turtle Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
As the sequences are defined they must be polynomials.
9
u/LordPos Dec 09 '23
[LANGUAGE: Haskell]
thought I'd post it here since it turned out really short
for part 2; replace
next . map readwithnext . reverse . map read8
u/770grappenmaker Dec 09 '23
[LANGUAGE: Kotlin] Little sad I did not get on the leaderboard, but still #113/#186 which is good enough for me.
8
u/chubbc Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Uiua]
Nice and succinct today. Link to pad.
Edit: Noticed I shorten
Extrapslightly by using arepeatinstead of ado. Should in principle be slower, but isn't noticable. In its most simplified form:7
u/jbafny Dec 09 '23
[Language: Haskell]
7
u/Cyclotheme Dec 09 '23
[LANGUAGE: QuickBASIC]
Github link
Because finite differences are a linear operator, I added all the rows componentwise before the extrapolation (one time on the original list, then on the reverse list).
Execution time at ~16MHz:
parsing ... 6.75s
computation ... 0.05s
7
u/Rodbourn Dec 09 '23
[LANGUAGE: C#]
https://github.com/Rodbourn/adventofcode/blob/main/Day009.cs
Runtime 831 ticks to solve both part 1 and 2 at the same time if you don't count parsing input. I have a background in numerical simulation, so this immediately was clearly finite difference. Finite difference weights can be calculated in general, and so here I just used the weights for 6th order, and 21st order finite difference for the 0th derivative at 6 and 21 on an interval spacing. Conveniently, extrapolating to the left is symmetric, and you just use the weights in reverse order. The extrapolation then just becomes w[i]*v[i] (weight and value).
For actually calculating the weights, I had translated a Fortran code into Matlab (13 years ago... oof) to calculate finite difference weights for any given derivative (zero'th here) at any position (N here) given any location of the data points (evenly distributed here).
https://github.com/Rodbourn/adventofcode/blob/main/FornbergWeights.m
This is definitely not widely available... (I'd keep a copy of it around as it's exceedingly handy in real world ;) )
To calculate the weights for 21, the matlab command would be
FornbergWeights(21,0:21-1, 0, 0)'
6
u/gurgeous Dec 09 '23
[LANGUAGE: Ruby] 371/438
Fun one:
6
u/rabuf Dec 09 '23
[LANGUAGE: Python]
The whole thing in three functions, plus summing after applying to every line of input. I missed, while coding it, that you could just reverse the lines for part 2.
6
u/langelgjm Dec 09 '23
[LANGUAGE: Python] Surprised I haven't seen a Python solution that uses itertools.pairwise. Textbook recursion.
6
u/ka-splam Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Dyalog APL]
6
u/clbrri Dec 09 '23
[LANGUAGE: C-style C++]
I did a linear-time solution (albeit with a quadratic time and linear space precomputation at program startup) by evaluating the 22nd row of Pascal's triangle and computing a dot product with that and the input vector.
34 lines of code. Runs in 1 minute 18 seconds on my Commodore 64.
Details and code in my blog.
5
u/EvilKanoa Dec 09 '23
[LANGUAGE: Typescript]
I was expecting much more complication for a weekend one, surprised especially with part 2 being so similar to part 1. I like the other answers that simply reversed the input, nice idea to reuse basically all of part 1 without any other changes!
Code for both parts here: https://github.com/EvilKanoa/AoC-2023/commit/834635b81c62515ce3194df233d0c51eefa768ef
5
u/NimbyDagda Dec 09 '23
[LANGUAGE: Python]
Part 2 seemed too simple, but maybe its only because of how I solved part 1.
Day 9
5
u/YenyaKas Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Perl]
The code is reasonably short, but only after spending long minutes on Part 2 and writing those subtractions correctly, getting the gold star, I later realized that
reverse @seqwould do the trick as well. Here is the code for Part 2 afterwards:All my AoC solutions in Perl
5
u/Mats56 Dec 09 '23
[LANGUAGE: Kotlin]
nice and short functional piece of code. Like this language.
5
u/Smylers Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Vim keystrokes]
After a couple of days of puzzles which were a good fit for Vim — see solutions for day 7 and day 8) — today's really wasn't. But I did it anyway — this is what's needed for part 1:
You might prefer the more readable version, formatted with additional line breaks.
First I solved on paper to work out we need the nth row of Pascal's triangle, where n is the number of readings in each history. The first 3 lines above (or up to the blank line in the more readable† version) generate this. In the sample input, each history has 6 readings, and this line gets added at the start of the input:
It's row 6 of Pascal's triangle with the final
1removed, a*separating each number, and alternating numbers negated. Multiplying each of these by the corresponding reading in each history and then summing them will give the next predicted reading. That's what the last 2 lines do. For instance the first line of the sample input becomes:which evaluates to
18, as required. Explanation of the keystrokes for the Pascal's-triangle-generating bit is in a reply to this comment below; as a side-effect it also saves the keystrokes for evaluating the current WORD in@e. That gets used again in the remaining steps, which apply the above list of terms to each reading in the lines below:"1register.+before each line of readings. Record doing this as the@p(for ‘plus’) keyboard macro, for later use.Pto restore the row that was just deleted. Record the@dkeystrokes which deletes the first term (up to and including the*) from that row, then goes down to the reading row and to the final (at this point, only)+and appends the just-deleted term. That makes the first reading in the sample input change from0into-1*0(which isn't very exciting, but is accurate). Then move along to the next space, go back up to the line above, and repeat until we run out of spaces. The first line in the sample is now+-1*0+6*3+-15*6+20*9+-15*12+6*15.@eto get the required18. Then go up to the now-blank line and useJto get rid of it (merge the following line, with our evaluated-number, on to it). Go down to the following line withj, paste the row of multiplication terms in again above it with"1P, and run@dto do the multiplications on that row.:norm, which is run separately on each line from the current one down to the last one.@dis going to end with failure each time (when it tries tof␣to a space that doesn't exist). Wrapping in:normboth provides an outer loop and prevents the failure in@dfrom exiting that outer loop. On the final line, thejwill cause the:normto fail early, after it's done the evaluation and join.@pagain to put a+before each line, then join them without any spaces, and use@eto evaluate and get the part 1 answer.So it turns out to be possible to solve today's puzzle in Vim, but I'm still not sure that it was a good idea. My concern is that things like this give Vim solutions a crazy reputation, putting people off using Vim for tasks to which it is much better suited, like the previous 2 puzzles. It still fits inside an 80×5 half-punch-card though!
† Well, less-unreadable, anyway.
5
u/sdolotom Dec 09 '23
[LANGUAGE: Clojure] Input is expected to be a parsed sequence of integer vectors.
5
u/Symbroson Dec 09 '23 edited Dec 09 '23
[Language: Ruby]
For my input I could just sum the differences and check for zero, but I realize that for some inputs this might not work.
delta.uniq.size == 1was my way to go here but I found a silly shortcut for my golf version to detect if I reached the end. Spoiler:dont.They will be empty anyways at some pointgolfed 137 bytes:
5
u/[deleted] Dec 09 '23
[LANGUAGE: Rust]
My Day 09 on GitHub
Not much to say really, this was a pretty straightforward implementation and also I think the fastest part 2 so far. Nice to have a quick day though after spending a lot of time learning math yesterday.
5
u/juanplopes Dec 09 '23
[LANGUAGE: Python]
Both parts in 6 lines
https://github.com/juanplopes/advent-of-code-2023/blob/main/day09.py
5
u/4HbQ Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
NumPy one-liner for both parts, using
np.diff()with all its parameters:Some explanation: we diff with n=21 to basically repeat the operation 21 times. Because we prepend and append with 0, the remaining values are the ones we're looking for.
This is a bit tricky to explain, but easy to show. For the third example input:
5
u/Domy__ Dec 12 '23
[Language: Python]
https://github.com/Domyy95/Challenges/blob/master/2023-12-Advent-of-code/9.py
5
u/1str1ker1 Dec 15 '23 edited Dec 15 '23
[LANGUAGE: Python]
The trick is to notice the next element is the sum of the previous elements multiplied by their corresponding position in a binomial expansion (pascal's triangle)
4
u/BradleySigma Dec 09 '23
[LANGUAGE: Python 3]
4
u/xelf Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
Part 2 took longer to read than to solve.
4
u/HAEC_EST_SPARTA Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Ruby]
Solution on sourcehut
This was a neat problem! I was expecting for some form of Sierpinski triangle-related optimisation to be required for Part 2, but both parts were trivially simulable.
Edit: Doh, I meant to reference Pascal's triangle instead.
4
u/jaybosamiya Dec 09 '23 edited Dec 09 '23
[LANGUAGE: APL]
Haven't done any golfing yet; just the straightforward solution here
EDIT: Golfed it down a bit by realizing a straightforward unnecessary thing I was doing
4
u/chubbc Dec 09 '23
[LANGUAGE: Julia]
Well... that was easy.
4
u/Sharparam Dec 09 '23
[LANGUAGE: Ruby]
Short and sweet today:
(GitHub link)
Confusingly easy for a day 9 that is also on the weekend.
4
u/Kehvarl Dec 09 '23
[LANGUAGE: Python3] 3895/3521
I feel certain there's a more clever way to solve this, but it fell to simple brute force. Part 2 required basically just changing what index I looked at and prepending instead of appending.
Part 2
4
u/adamsilkey Dec 09 '23
[LANGUAGE: Python]
Really not a hard one but I had a lot of dumb little bugs that messed me up. Went slow by going too fast.
However!!!
git restore day09.pysaved me a couple times in part 2. When I hated all the changes I was making, I could just start fresh from a known working part 1. Yay git!https://gist.github.com/adamsilkey/8aeb8a8486120493188fef0982fac905
4
u/Magyusz Dec 09 '23 edited Dec 09 '23
[Language: Javascript]
Just copy-paste into your browser console (F12) on the input page:
4
u/delventhalz Dec 09 '23
[LANGUAGE: JavaScript]
2288/2202
I feel like if I were less sleep deprived I could have finished in half the 20 minutes it took me. I kept looking for the catch. Been burned by recursion before. But it all worked as expected with no surprises really.
4
u/[deleted] Dec 09 '23 edited Dec 10 '23
[LANGUAGE: Forth]
To work through the input, I created a
processword that accumulates the last number of every derivative/reduction of the set of numbers on the stack. By adding thisprocessword to the end of each line in the input file, I could simplyinclude inputand be left with just the total accumulation on the stack. For part 2, I appended areverseto my reduce/accumulate word and just included the input file again.https://github.com/tcsullivan/advent-of-code/blob/master/day9/partboth.fth
4
u/s3aker Dec 09 '23
[LANGUAGE: raku]
solution
4
u/ondrsh Dec 09 '23
[LANGUAGE: Kotlin]
Not fast because it does 2 runs instead of 1. Doing it in a single run brings it down to <1ms, but the solution gets uglier.
https://github.com/ondrsh/AdventOfCode2023/blob/master/src/main/kotlin/Day9.kt
4
u/4HbQ Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
Playing a quick round of golf. Currently at 151 bytes for both parts:
Nothing too fancy, basically just a condensed version of my original solution. The main difference (besides readability): instead of extrapolating the normal and reversed sequences, the we now just compute the new last or first values.
4
u/vanveenfromardis Dec 09 '23 edited Dec 09 '23
[LANGUAGE: C#]
GitHub
I got a late start on this one and just solved it casually. This concept of repeated differences was exactly how my high school calculus teacher introduced us to the concept of a derivative.
Given a polynomial, evaluating
f(x)on consecutive integer values ofx(e.g. 1, 2, 3, ..., n) will yield a segment of the polynomial. Building a table of 1st differences, 2nd differences, and so on of these values, as we do in this problem, is isomorphic to evaluating it's derivative.Given a polynomial of degree
n, it's nth differences will be constant. For example, the 1st differences of the linear polynomialf(x) = 2x + 1will be simply be the constant2. Similarly, the 2nd differences of any quadratic will be constant.Therefore, this question is isomorphic to giving us a polynomial segment, and asking us to evaluate the proceeding and following values of
f(x)outside of the sampled range.Implementing this naively was easy with Linq:
4
u/[deleted] Dec 09 '23 edited Dec 09 '23
[Language: Python] Github
Having a maths background I couldn't help but use a bit of linear algebra
Each input is a polynomial sequence given by a formula like
f(n) = a + b*n + c*n**2 + d*n**3 + ...where the sequence is given by
f(0), f(1), f(2), f(3), ...The number of steps required to get a list of
0tells us the number of terms in the formula, so for the second sample input1 3 6 10 15 21we know the formula has3terms and therefore the form isf(n) = a + b*n + c*n**2and from the sequence given we see that
f(0) = 1 = af(1) = 3 = a + b + cf(2) = 6 = a + 2b + 4cThis gives us a system of simultaneous equations to solve for
a, b, c- I used the inverse matrix to do that. Once we have those coefficients it is easy to calculatef(n)for anyn. E.g. Part 2 is solved byf(-1)4
u/5kyl3r Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
you'll hate my golfed solution, and you're welcome haha
EDIT: updated to reflect 4HbQ's excellent suggestions. tested and it still works
4
u/WhiteSparrow Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Uiua]
I think I'm falling for Uiua!
4
u/rs10rs10 Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python 3]
Nice with a short one today:
4
u/huib_ Dec 09 '23 edited Dec 09 '23
[Language: Python 3.12]
4
u/lbm364dl Dec 09 '23
[LANGUAGE: Python] Code (6 lines)
Cool to use map with two iterables as a Python zip_with
4
u/Killavus Dec 09 '23
[Language: Rust]
Quite elegant recursive solution - I'm pretty happy about this :):
4
u/tymscar Dec 09 '23
[LANGUAGE: Rust]
Today was maybe too easy, but I enjoyed it quite a lot.
It was fun thinking of if there is a formula at play, but then in the end I just went with a recursive solution.
Part1 and part2
4
u/pkusensei Dec 09 '23
[Language: Rust]
This is straightforward. Now I can eat my breakfast.
4
u/108113221333123111 Dec 09 '23
[LANGUAGE: Python]
day 9 solution.
I'm relatively new to programming and pretty happy with this. Is it the fastest? No. Is it the fewest lines? Also no. Is it the most readable? Nope. But it works and I'm learning! This is the first year I've discovered AoC and I'm really enjoying the community aspect of solving these problems every day.
4
u/its_jsec Dec 09 '23
[LANGUAGE: Python]
Relatively happy with the terseness of the solution.
itertools.pairwiseto the rescue.paste
4
u/hrunt Dec 09 '23
[LANGUAGE: Python]
Code
For me, Part 1 was an immediate obvious recursive solution. For Part 2, I almost couldn't believe that the solution was as obvious as it was. I simply reversed the readings and passed them to my Part 1 solution.
I thought for sure Part 2 was going to be some sort of complexity bomb , but hey, after the wife's office holiday party last night, an easy day was just what the doctor ordered.
4
u/efvincent Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Haskell]
Strange that Eric tosses us such a softball on day 9. Possibly my shortest solution ever (I', not golfing after all).
4
u/Fyver42 Dec 09 '23
[LANGUAGE: assembly]
https://github.com/fyvonnet/AdventOfCode-2023-Assembly/blob/main/day09.asm
4
u/Gravitar64 Dec 09 '23
[LANGUAGE: Python]
Part 1 & 2, 15sloc, runtime 5ms
4
u/a_kleemans Dec 09 '23
[LANGUAGE: Rust]
My first piece of Rust code, ever. Was looking forward to a problem easy enough to try out something new, and today was the day.
Github Link
Only part 1, runs in ~1.3ms.
Looking forward to scan through other Rust submissions to improve!
4
u/jvdsandt Dec 10 '23
[LANGUAGE: Smalltalk]
Part2:
1
u/alexbaguette1 Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
had to change 3 characters for s2
print(total)
1
u/ThisNameIsntRandom Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python]
1
u/azgx00 Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Rust]
https://gist.github.com/isak102/51a73b51ce01c06796e8363154574c64
1
u/Longjumping_Primary4 Dec 09 '23
[Language: Deno, TypeScript]
Simple use of `push` and `unshift` array methods.
https://github.com/dmatis2/aoc23/blob/main/09.ts
1
u/Spatenheinz Dec 09 '23
[LANGUAGE: Haskell]
pretty easy, and satisfied with my concise solution:
1
u/Jolly_River Dec 09 '23 edited Dec 11 '23
[LANGUAGE: TypeScript]
With RECURSION ! :D
Complete solutions : https://github.com/DarioTheLuca/my_advent_of_code/blob/master/aoc2023/src/day09/index.ts
1
u/fotosjopjes_ig Dec 09 '23 edited Dec 10 '23
[Language: JS]
Only 15 lines of "readable" JavaScript for both parts.Surprised myself that I could do it with a single while loop and without storing all the sequences.(Gave myself a little pad on the shoulder for this one 😉 )
https://github.com/mark-boots/Advent-of-Code/blob/main/2023/day9/index.js
1
u/MarcoDelmastro Dec 09 '23 edited Dec 10 '23
[LANGUAGE: Python]
https://github.com/marcodelmastro/AdventOfCode2023/blob/main/Day09.ipynb
np.diffto process the lists in a loop,np.anyto stop it, some simple manipulation to get the results.1
u/veydar_ Dec 09 '23 edited Dec 09 '23
[LANGUAGE: janet]
Janet
5 lines of code.
I apologize for violating the rules by 2 lines, but here's the code for both parts:I apologize for violating the rules by 4 chars, but here's the code for both parts:
I tried to somehow fit it into 5 lines but couldn't. If someone has any ideas as to how to make this more concise without obfuscating it, let me know please.1
u/Exact_Apple_9826 Dec 09 '23
[Language: PHP]
https://github.com/mikedodd/AdventOfCode2023/tree/main/day_9
1
u/Loop_Dreams Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Clojure]
https://github.com/loopdreams/advent_of_code/blob/main/advent_of_code_2023/day09-mirage-maintenance/src/day09_mirage_maintenance/core.clj
1
u/Samuuuh Dec 09 '23 edited Dec 09 '23
[Language: Python]
Simple implementation in Python, good for beginners to understand what is being done.
The only difference between part 1 and part 2 is the step at which we add the value, where I just changed from `append(value)` to `insert(0, value)`to insert the element at the start rather than the end.
https://github.com/samyuh/advent-of-code/blob/main/2023/day_9.py
I will keep solving all AoC days from now on, ensuring that they are easily understood by everyone.
1
u/BrownCarter Dec 09 '23 edited Dec 10 '23
[2023 Day 9 (Part 2)] [LANGUAGE rust]
1
u/Goresao Dec 09 '23 edited Dec 09 '23
[LANGUAGE: C#]
Recursive functions are very helpful here.
Optimized code:
1
u/Galzzly Dec 09 '23 edited Dec 10 '23
[LANGUAGE: Go]
Quite liked this one, a short amount of code required for a pretty quick run time.
https://topaz.github.io/paste/#XQAAAQCaBQAAAAAAAAA4GEiZzRd1JAgz+whYQ+8kDmxlXTskAdhKyEdfRxacU4qkLCi/765VnERDfvTh9kanaWjNGsA+LDo1t5VEzrTYVlFbsW2Xfokup6qjsV/xlFXTqwXbk9+pjgHnYjRLCjTeq8e5y0oK9KHVrfNrqj64KCKpHCd4rcvAF7OZYouSM7lWVvno/DT6z09tpo+XKLbnThzuaxUJTuANozPjbvkvz3Tl0NPMY9MfOmN7L3N8KzH33QcqkGO3iq7zeLFLJfedIQ6U1LOSIRtsPEgAs0JDEFn4RipG3oGmaUOPBcQrdfak09M8rzw2NAjRIsaQ21bAoVG+6zFWHhjoYA2a38MzbGIo7PCToNlCTKNaspepZztf05mjHaZflL6nuX/uA7C+V8TZlhBxZ/wGVnspuHDLvm0qfoQEo+wqg5LoUBrwhaOP/mTD0JykLubnl3U41fGWk6EL3dYHC/SQbhWG+qFp7G1Xg+cYudT6LdK8pB5gsafEa01askSRfNsEpZcRt1MPjdXNU6COnvgxFlx65QOI416LjgyhRBZgyqGAPPCmjrV22WU39D2pm7/P0kWPWgG4HNR9YLjUeRYljLJlKjz/s9t2Y5j3MX4YFjt2CIXx+MhJBh6rlARed0+zkHQ5wC/BkAUiKRtI5EoE6cuPo1Dv7lwLlhivjYLXljpCI6UxDNg4YzX24ULwahwDbUBg16cxame+nwoHPv754m+O
3
u/morgoth1145 Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python 3] 154/98 Raw solution
Not too much to say about this problem. I really should have approached it with recursion instead of a manual stack but oh well, it worked well enough. (And part 2 is easy with a reversed list, of course!)
(Time to go refactor into a proper recursive solution :))
Edit: Refactored code
3
u/LtHummus Dec 09 '23 edited Dec 09 '23
[Language: Rust]
Genuinely happy with this one. I'm doing AoC this year to learn Rust, and I'm really happy with my code on this one. I thought doing this recursively would lead to borrow-checker hell, but it didn't and I'm happy.
edit: now that I'm thinking more, I guess i shouldn't be surprised the borrow checker isn't upset with recursion since it's all immutable borrows (oh god please let there be no mutable borrows for recursion...). I still don't have a great feel for the borrow checker, but I'm guessing that's just a matter of building intuition as I write more.
code is here
3
u/jovani_lukino Dec 09 '23
[LANGUAGE: Wolfram Mathematica]
3
u/swing-quick Dec 09 '23
[LANGUAGE: Javascript]
Pretty straightforward today. Runs in browser dev console.
3
u/globalreset Dec 09 '23
[LANGUAGE: Ruby] 1736/1266
Very simple recursion for both parts. Felt like I went really fast on this, but not enough to crack the top 1000. If only I would've skipped the test data... First day my solution worked on first try.
3
u/SopyG Dec 09 '23
[LANGUAGE: Rust]
Part1/Part2/Combined
3
u/Prudent_Candle Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python3]
For checking pairs:
pairwise(fromitertoolslibrary). For checking zeros,all(build in). I could write a nice way for taking values from bottom, but I just flip the whole stack of lines upside down (also build-in lists' method).Note: the iterative approach (not-recursive).
A little of ugly code
3
u/_voxed Dec 09 '23
[LANGUAGE: Python]
3
u/axr123 Dec 09 '23
[LANGUAGE: Python with NumPy]
3
u/homme_chauve_souris Dec 09 '23
[LANGUAGE: Python]
Somehow I expected something harder to start off the weekend. But it was fun nonetheless.
3
u/pred Dec 09 '23 edited Dec 09 '23
[LANGUAGE: Python] GitHub
Solving the problem as stated; using
np.diffto get n'th discrete differences is helpful:3
u/_livetalk Dec 09 '23
[LANGUAGE: Python]
Simple recursive solution that runs in ~8 Seconds.
Part 1+2
3
u/soleluke Dec 09 '23
[LANGUAGE: C#] 3597/3600
Solution
Like everyone else, apparently this one was deceptively simple. I did some tasks to parallelize it after the behemoths of the brute-force solutions the past few days, but it really was just some collection LINQ expressions and some recursion.
3
u/Accomplished-Ad-2762 Dec 09 '23
[LANGUAGE: Kotlin]
Really liked this solution so I thought it's worth sharing
3
u/dhruvasagar Dec 09 '23
[LANGUAGE: Haskell]
3
u/yfilipov Dec 09 '23
[Language: C#]
3
u/Ancient_Stranger_704 Dec 09 '23
[LANGUAGE: Elixir]
3
u/Goldeelux Dec 09 '23
[LANGUAGE: Javascript]
Nice puzzle and relatively straightforward. I still dont know why readfile() gives me a empty string at the end of my data inputs and it has been quite annoying
Part 1 & 2
3
u/solarpool Dec 09 '23
[LANGUAGE: R] 2394/1723
Recursion took a bit, reversing the input did not 😅
3
u/simpleauthority Dec 09 '23
[LANGUAGE: Java]
Eric, my brain thanks you. Still cooling down from Day 8. Haha.
Link to GitHub
3
u/MarvelousShade Dec 09 '23
[Language: C#]
I think that today was one of the easiest exercises of 2023. Especially the part 2.
My code is on: https://github.com/messcheg/advent-of-code/blob/main/AdventOfCode2023/Day09/Program.cs
3
u/michaelgallagher Dec 09 '23
[Language: Python]
:D
3
u/vkasra Dec 09 '23
[language: rust]
With no_std
https://github.com/dhconnelly/advent-of-code-2023/blob/master/src/day9.rs
3
u/Polaric_Spiral Dec 09 '23
[LANGUAGE: TypeScript]
Advent of Node, Day 9
Short and sweet. If not for the recursion, I would have smushed the
extrapolate()function into the middle of the function chain.3
u/SleepingInsomniac Dec 09 '23
[LANGUAGE: Ruby]
Part 1
Part 2
3
u/knl_ Dec 09 '23
[LANGUAGE: Hy]
Day 9
Didn't even think of using recursion, just iterated with a while loop.
3
u/radulfr2 Dec 09 '23
[LANGUAGE: Python3]
Took me some time to write but I liked this one. Using list and insert was fast enough, so I didn't bother thinking of a better way. Paste.
3
u/rafal_althoff Dec 09 '23
[Language: PHP]
Part 1
Part 2
3
u/vss2sn Dec 09 '23
[LANGUAGE: C++]
Part 1
Part 2
(Each file is a self-contained solution)
3
u/Knudson95 Dec 09 '23
[LANGAUGE: C#]
Paste Link
For a weird change of pace part 1 was easier than part 2. Nice easy Saturday challenge.
3
u/trevdak2 Dec 09 '23 edited Dec 09 '23
[Language: Javascript Golf]
Kudos to /u/Polaric_Spiral for some ideas that helped me shave off a few characters.
Part 1, 130 chars:
Part 2, 120 chars:
3
u/Scarecroweman Dec 09 '23
[Language: c#]
Github
3
u/mrvoid15 Dec 09 '23
[Language: Golang]
My approach is really simple, for a input line. Take the first and last value of each generation until zero. For example: for first input lastVals = [[0 15] [3 3] [0 0]].
With this now i just ran a loop, to predict the left and right value with difference and addition.
https://github.com/akashdeepnandi/advent-of-code/blob/main/day9/main.go
3
u/Vesk123 Dec 09 '23
LANGUAGE: Rust
Quite an easy one today. If only I hadn't forgotten that my regex for parsing all the numbers in a line (really useful btw) didn't include minus signs... Anyway, I'm starting to enjoy Rust a bit, no real issues today. I really liked how I could change a
Vec<i32>into a&[i32]without having to change anything else in the code.3
u/JustinHuPrime Dec 09 '23
[LANGUAGE: x86_64 assembly with Linux syscalls]
Part 1 involved a direct solution - for each line, I read it into a dynamically allocated array, then allocated a new array and calculated the differences between the previous array's elements, and so on until I got an array that's all zeroes. I then proceeded to extrapolate - I found the end of the list, and added the end of the previous list to this list to get the new element to add to the end of the list - I actually didn't need to save this value in new space, I could have just overwritten the old end of the list.
Part 2 was a surprisingly small change, and in fact, required fewer lines of assembly code! I just tweaked my extrapolation code to care about the start of the list (which was an easier pointer to get a hold of than the end of the list), and instead of using new space, I overwrote the initial values in the list, since I only cared about the previous list's new first value and the current list's actual first value to calculate the current list's new first value.
Part 1 and part 2 run in 5 milliseconds - gosh, is dynamic allocation ever expensive, time-wise! Part 1 is 8616 bytes long and part 2 is 8456 bytes long (since I could delete the "find the end of the list" code from part 2). Part 1 and part 2 use ~5 MiB of heap space. (Alas, my
allocfunction is really inefficient - Linux, I believe, returns whole pages when asked for more heap viabrk, so since I apparently make 1159 allocations, I use 1159 pages of heap.)3
u/izahariev96 Dec 09 '23
[Language: Kotlin]
https://github.com/ivzb/playground/blob/master/src/main/kotlin/advent_of_code/_2023/Task09.kt
3
u/TankLivsMatr Dec 09 '23
[LANGUAGE: Go]
Ended up geting home late from a bad snow storm, and still was able to get the answer in, in a relatively decent time (1.5 hours after opening).
It's pretty simple really, I just did exactly what the question said to do with the numbers, but with a two dimensional slice in Go. Specifically, I used Go's extremely powerful append feature to add elements as needed to the slice.
Day 9
3
u/solarshado Dec 09 '23
[Language: TypeScript]
6813 / 6592
github
Reversing the lists for part two didn't occur to me, but I'd already over-thought part one and thought I needed
reduceRight, so it was the obvious choice.Didn't consider recursion for building the triangle: after the scale of some of the previous days' solutions, I'm a little wary of possible stack overflows.
3
u/rukke Dec 09 '23
[LANGUAGE: JavaScript]
Recursively diff until last row is zeros, then extrapolate next value by
reduceRightfor first part by addition, for second part by taking the diff of the previous value.gist
3
u/Szeweq Dec 09 '23
[LANGUAGE: Rust]
No recursion, with a lot of purely functional iterators. Used:
sum,rfold,windowsand many others. The code is short and runs in less than 1 ms.Code: https://github.com/szeweq/aoc2023/blob/master/src/bin/09.rs
3
u/4HbQ Dec 09 '23
[LANGUAGE: Python] Code (7 lines)
Here's my implementation of Lagrange's interpolating polynomial:
3
u/Fyvaproldje Dec 09 '23
[LANGUAGE: Raku] [Allez Cuisine!]
Have trouble with a drying OASIS?
Click here for a solution which will predict the humidity and other values, in all directions you can think of! You'll be able to use the water from the oasis, and predict when exactly it'll be salty enough for your taste!
An accidental visualization included!
Limited offer: if you order today, you'll receive a copy of all the previous
solutirecipes for free!3
u/Ill_Ad7155 Dec 09 '23
[LANGUAGE: Python]
Part 1: Just sum every last element from the list while computing the differences.
Part 2: Reverse the list first.
3
u/toastedstapler Dec 09 '23 edited Dec 09 '23
[language: rust]
71us on my 10850k, sitting around a 700-800us solve overall so far!
https://github.com/jchevertonwynne/advent-of-code-2023/blob/main/src/days/day09.rs
3
u/k0ns3rv Dec 09 '23
[LANGUAGE: Rust]
Code
Quite pleased with the solve today. I forgot to reverse the list of final numbers, but that surprisingly worked for both part 1 and the test input for part 2.
3
u/sergiosgc Dec 09 '23
[LANGUAGE: Rust]
Code
This was surely my quickest part 2 of all time. Just reverse the input sequences... Other than that, unremarkable problem and solution. It's very approachable recursively.
3
u/aarnens Dec 09 '23
[LANGUAGE: Rust] Both parts run in ~0.8ms.
What was nice about this challenge was realizing that there is no need to keep track of all the lists present at each step, you just need to remember the first and last digits.
Here is my implementation for calculating the next "row" and keeping track of said digits:
Full code here: https://pastebin.com/uWwZRZYh
Feedback is appreciated, as always!
3
u/troru Dec 09 '23
[LANGUAGE: Java]
3
u/rumkuhgel Dec 09 '23
[LANGUAGE: Golang]
Okay, today was pretty easy compared to previous days.
https://github.com/rumkugel13/AdventOfCode2023/blob/main/day09.go
3
u/Lvl999Noob Dec 09 '23
[Language: Rust]
Today was certainly quite easier than the last few days. It took me some time because I accidentally wrote an infinite loop at first. But otherwise, it was quite easy.
3
u/Psychological-Pay806 Dec 09 '23
[LANGUAGE: RUST]
Happy days :)
github
3
u/Kintelligence Dec 09 '23
[Language: rust]
https://github.com/Kintelligence/advent-of-code-2023/blob/master/day-09/src/lib.rs
A lot simpler than yesterday, but still a pretty slow execution compared to earlier days. I wonder if there is a faster algorithm than the one described in the problem text.
Benchmarks Part 1 Part 2
3
u/Solidifor Dec 09 '23
[Language: Java]
https://github.com/dirk527/aoc2021/blob/main/src/aoc2023/Day09.java
118 lines (20 of those toString() for debugging), somewhat overengineered using a Node class with pointers. I thought I could optimize by not constructing the whole thing, but it does say "last row all zeros" so... no. Yeah, there's not really an advantage to this approach given what part 2 was.
This was curiously straightforward.
3
u/Small_Possibility173 Dec 09 '23
[LANGUAGE: Rust]
This was fairly simple using a bit of recursion.
https://gist.github.com/PrakharSrivastav/ef4bbe3786c9545272c14f44e048f202
3
u/odnoletkov Dec 09 '23 edited Dec 09 '23
[LANGUAGE: jq] github
3
u/[deleted] Dec 09 '23
[LANGUAGE: Python]
It's all about high order arithmetic progression. Almost instant output. Solution Here.
Just one formula. Thanks Newton 🫡
3
u/azzal07 Dec 09 '23
[LANGUAGE: Awk]
3
u/jcmoyer Dec 09 '23
[LANGUAGE: Ada]
https://github.com/jcmoyer/puzzles/blob/master/AdventOfCode2023/src/day09.adb
Really enjoyable puzzle today.
3
u/GillesArcas Dec 09 '23
[Language: Python]
Code it as you say it. A canonical tail recursive solution:
https://github.com/GillesArcas/Advent_of_Code/blob/main/2023/09.py
[418*]
3
u/sreyas_sreelal Dec 09 '23
[Language: Rust]
Day 9
Today's puzzle was easy and straightforward.
3
u/[deleted] Dec 09 '23
[Language: TypeScript]
Gotta love just having to add a
.reverse()to solve part 2.Part 1 - 283.41 µs
Part 2 - 287.27 µs
3
u/dylan_mojo Dec 09 '23
[LANGUAGE: Awk]
GitHub
3
u/win0err Dec 09 '23
[Language: Python]
https://github.com/win0err/solutions/blob/master/adventofcode/2023/09-mirage-maintenance/main.py
3
u/Mammoth_Spray_3451 Dec 09 '23
[LANGUAGE: Swift]
Fairly easy today and only a few (unreadable) lines.
My solution
3
u/chicagocode Dec 09 '23 edited Dec 09 '23
[LANGUAGE: kotlin]
Despite the 14 mentions of sequences in the puzzle description, I went with a recursive function in my solution today.
My code can be found on GitHub, I've written this up on my blog, and here are my 2023 Solutions so far.
3
u/axsk Dec 09 '23
[LANGUAGE: Julia]
3
u/Secure_Pirate9838 Dec 09 '23
[LANGUAGE: Rust]
Screencast: https://youtu.be/FIMdUs8IYuQ GitHub: https://github.com/samoylenkodmitry/AdventOfCode_2023/blob/main/src/day9.rs
Part 2 was the same as part 2, just do careful math before writing the code
3
u/comforttiger Dec 09 '23
[LANGUAGE: Ruby]
https://github.com/comforttiger/advent_of_code/blob/main/2023/ruby/day9.rb
im glad today was easier than yesterday. but im ready for tomorrow to be super hard
3
u/RelativeLead5 Dec 09 '23
[Language: Ruby]
To determine if the end condition was met, I first used "line.sum != 0" instead of "line.uniq != [0]". Took a long time to figure out because works fine on the test input and only two lines in the real input eventually reduce to an array that sums to 0 but isn't all zeros. Doh!! Don't do that.
3
u/hopperface Dec 09 '23
[LANGUAGE: Julia]
Five lines of code
3
u/SwampThingTom Dec 09 '23
[LANGUAGE: Rust]
Today's was very easy. I'm beginning to think the elves dropped all of the puzzles in the snow and are giving them to us in a completely random order. :-)
Github
3
u/CelestialDestroyer Dec 09 '23
[Language: Chicken Scheme]
Today was very simple indeed - I did get stuck on a stupid oversight though, but someone here helped me out. Thanks!
3
u/jitwit Dec 09 '23 edited Dec 09 '23
[Language: J]
Uses Vandermonde matrix to do polynomial interpolation.
https://github.com/jitwit/aoc/blob/a/J/23/09.ijs
3
u/OilAppropriate2827 Dec 09 '23 edited Dec 09 '23
[LANGUAGE: python]
I wanted to try Lagrange polynomial extrapolation. It is quite efficient as all the list have the same length.
I did the extrapolation for X=-1 (reverted the list to extrapolate the end). I am using all the points. The result is the dot product of the list to extrapolate and a vector [comb(n,i) for i in range(1,n+1)]
https://github.com/hlabs-dev/aoc/blob/main/2023/9_lagrange.py
3
u/c4irns Dec 09 '23
[LANGUAGE: Go]
https://github.com/cdillond/aoc/blob/main/2023/9/main.go
3
u/fullmetalalch Dec 09 '23
[Language: Python] Gitlab
I read the prompt and assumed part 2 would require solving for the underlying polynomial, so I spent an hour revisiting long-forgotten math. Finally decided to try the naive approach and was pleasantly surprised when part 2 was revealed.
Came up with a somewhat functional and very readable solution. Also my first time ever using
itertools.pairwisewhich was neat.One hiccup I encountered was not parsing negatives in my
intsutility function. Hopefully the change I added didn't break logic from previous days!3
u/pem4224 Dec 09 '23
[Language: go]
https://github.com/pemoreau/advent-of-code/blob/main/go/2023/09/day09.go
Very simple approach with some optimizations (avoiding memory allocation) which make the code run in 170 μs
3
u/ywgdana Dec 09 '23
[LANGUAGE: C#]
Just implemented the reduction pretty much as described in the problem as a recursive function, which makes my solution slow but fairly succinct in terms of LoC! Part 2 I just reversed all the arrays and did it again.
Nice and fast for a weekend problem! Maybe I'll have time to finish Day 5 Part 2 later on today...
Solution on github
3
u/marcja Dec 09 '23
[LANGUAGE: Python]
Using NumPy, it almost feels like cheating. ;)
3
u/FerenIsles Dec 09 '23
[Language: Javascript]
Today was a lot of fun. Part 2 almost identical to part 1 with some changes to the reducer function for calculating the total.
https://github.com/les-friesen/AoC2023/tree/main/aoc/day9
3
u/apolloisggg Dec 09 '23
[LANGUAGE: Scala]
github
3
u/arthurno1 Dec 09 '23
[LANGUAGE: EmacsLisp]
3
u/onrustigescheikundig Dec 09 '23 edited Dec 09 '23
[LANGUAGE: OCaml]
github
For those who don't quite recognize it from their algebra/calculus classes, the algorithm presented in today's challenge is Newton's difference method for polynomial interpolation, which gives exact interpolated values from n points evaluated on a polynomial up to degree n - 1. Because I expected Part 2 to require evaluation of this polynomial at some arbitrary point in time, I eschewed the difference method entirely in favor of constructing the polynomial in an anonymous function using Lagrange interpolation. This kind of interpolation can experience some serious numerical cancellation issues, so I used OCaml's Num library for arbitrary-precision rational numbers so I didn't have to think about them :). This does result in a performance hit, though; I have a ~120 ms runtime combined for both parts. I also implemented a version using floating-point arithmetic for comparison, and the results for Parts 1 and 2 differ from integer solutions in the fourth decimal place.
3
u/Ily3s_ Dec 09 '23
[LANGUAGE: C] C standalone
3
u/loquian Dec 09 '23
[LANGUAGE: C++]
github, 1225 microseconds (both parts)
3
u/mendelmunkis Dec 10 '23
[LANGUAGE: C]
Where's the twist?
1.59/1.602 ms
3
u/micod Dec 10 '23
[LANGUAGE: Common Lisp]
GitLab Day 9
3
u/sikief Dec 10 '23
[LANGUAGE: C++]
[PLATFORM: Nintendo DS (Lite)]
Solution - Part 1 and 2
3
u/e_blake Dec 12 '23
[LANGUAGE: m4 (golfed)] [Allez Cuisine!]
Ladies and Gentleman, step right up, and get your VERY OWN copy of this punchcard of m4 goodness! Are you tired of reading programming languages that are bogged down by boilerplate keywords? Do you ever feel like you are typing the same words over and over again? Do you ever get lost when reading spaghetti code that has more than one function? Do the symbolic characters on your keyboard need to be pressed more often? Then this is what you've been waiting for - LIVING PROOF that REAL programmers can write an entire program using just one immutable macro and one conditional statement, all in the space smaller than a punchcard, and with no letters besides the few needed to access language builtin operators. After all, if your input file has no letters, why should your program need any? No need for fancy variable names - everything you need is available through recursion. If you name your input file "I" (or if you cheat and use
m4 -DI=file day09.golfm4), you too can experience the amazing power of this 2-second solution for both parts of today's problem, and satisfy all your late-night line-noise cravings!But wait, there's more! If you want to understand what this code is doing, I'll even direct you to my earlier post showing a more legible version of the same algorithm, or an alternative algorithm that computes the same answers in a fraction of the time. And if you think 387 bytes is too expensive, then ask about my discount solution with 10 fewer bytes [*]).
And if you like this offer, I'll even chip in a similar solution for day 1, at no extra charge (other than shipping and handling)
[* to be read at 2x speed] Offer not valid for customers that only have one punchcard on hand, as the 377-byte solution requires 6 lines instead of 5. And since my repo uses a GPL license, you are reminded that THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
3
u/nicuveo Dec 13 '23
[LANGUAGE: Haskell]
Not gonna post my haskell solutions every day, but this is really a day where the language could shine.
VOD: https://www.twitch.tv/videos/2002544400
3
u/ianMihura Dec 13 '23 edited Dec 13 '23
[LANGUAGE: GO]
Part 1 The trick for me was realizing that, once I had the sequence down to all zeroes, I could add-up the last column, and that would yield the result.
Part 2 was similar but with the 0th column, with an alternating sum/sub
https://github.com/ianmihura/advent23/blob/master/day_9/day_9.go
3
u/ramrunner0xff Dec 16 '23
[LANGUAGE: C]
so around 6 hours ago i decided that i really wanted to do this on scheme, you know.. have a mapcar for the subtractions and a fold for testing if they are all 0's? you guys feel me right? well 6 hours later and almost 300 lines of the hackiest static memory only C here is one of the most over the top complex solutions to day 9.
3
u/agent3bood Dec 18 '23
[LANGUAGE: Rust]
https://github.com/agent3bood/aoc23/tree/master/src/nine
3
u/835246 Dec 20 '23
[LANGUAGE: C]
Part 1: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day9oasispt1.c
Part 2: https://github.com/efox4335/advent_of_code/blob/main/advent_of_code_2023/day9oasispt2.c