Everything is here: https://advent-of-code.xavd.id/
I'm unlikely to finish it all in December (the puzzles get hard and I get busy) but I _do_ love the event.
451–460 of 580 posts
Everything is here: https://advent-of-code.xavd.id/
I'm unlikely to finish it all in December (the puzzles get hard and I get busy) but I _do_ love the event.
I'm doing the challenges in PowerShell to see how it goes. I want to use it as a test to see how human programming can be improved by an AI, so I wrote the solution for day 1, got the right answer, and then gave my code to ChatGPT 4o to ask it to make the code faster. My version ran in ~3500 ms ChatGPT's version ran in 140 ms both worked A great example of how a common DevOps language program can be improved on by Ch…
My hack-it-out code: https://pastebin.com/PDQhxDc9
Faster code: https://pastebin.com/6xwaVkwq
The hacky code uses slower techniques like:
- Get-Content which adds metadata to every line.
- @() arrays with += which copies the array in memory to a new one-larger memory location for every addition.
- Pipeline overhead e.g. ForEach-Object and Measure-Object.
- Filtering the whole second column for each number in the first column, repeated wasted work.
and it's still in the region of your ChatGPT one.
The faster one addresses these with:
- ReadAllLines() .NET method to get plain strings.
- [system.collections.generic.list[int]]::new() which don't box integers and can grow more quickly.
- plain adding numbers into sum variables.
- Building a hashtable [system.collections.generic.dictionary[int, int]]::new() to count each number in the second column.
- Swapping -split for string split() which may have a tiny bit less overhead.
- no pipelines.
The code isn't completely different, it's the same blocks doing the same things, leaning more on .NET lower levels, and years of experience of the basic PowerShell performance hits.
Earlier quoted context omitted.
Same. I am doing rust + clojure this year. Very interested in performance hax, esp around SIMD. I know absolutely nothing at all about rust, this is my first time working with it. My day 1 rust solution: cargo solve 1 -- release Finished `release` profile [optimized] target(s) in 0.05s Running `target/release/01` Part 1: 1189304 (95.8µs) Part 2: 24349736 (120.4µs) Day 1 clojure solution: lein run 1 running all tasks…
There's a Rust solution posted in the Reddit Day 1 answers mega thread which claims 22 microseconds part 1 and 10 microseconds part 2. (I haven't tried to verify): https://old.reddit.com/r/adventofcode/comments/1h3vp6n/2024_...
https://old.reddit.com/r/adventofcode/comments/1h3vp6n/2024_...
Another claiming 30μs and 32μs:
https://old.reddit.com/r/adventofcode/comments/1h3vp6n/2024_...
Earlier quoted context omitted.
There's a Rust solution posted in the Reddit Day 1 answers mega thread which claims 22 microseconds part 1 and 10 microseconds part 2. (I haven't tried to verify): https://old.reddit.com/r/adventofcode/comments/1h3vp6n/2024_...
Sure but how long did it take to compile
Then 'cargo build --release' took 23.3 seconds.
Commenting out one of the sort_unstable lines and rebuilding gave me a warning, recompiled in 1.8 seconds.
Earlier quoted context omitted.
Python looks excruciatingly slow to me. If you want fast I believe you need to think and write in vector languages like kdb+/q. I am not a kdb+ expert by any means and my code can probably use more q primitives, but here was my solution in ~2 minutes: i1:("I I";" ")0: `:1.txt; sum {abs last deltas x }each flip asc each i1 / answer 1 sum {x * sum x = i1[1]}each i1[0] / answer 2
Everything autocompletes in Python. from collections import * xys = list(map(int, open(0).read().split())) xs = xys[::2] ys = xys[1::2] xs.sort() ys.sort() print(sum(abs(x-y) for x,y in zip(xs,ys))) yc = Counter(ys) print(sum(((yc[x])*x for x in xs)))
data = { i+1 : sorted([ x for x in list(map(int, open('input').read().split()))[i::2]]) for i in range(2) }
total_distance = sum(list(map(lambda x: abs(x[0]-x[1]), zip(data[1], data[2]))))
print("part 1:", total_distance)
similarity_score = sum(list(map(lambda x: (x*data[2].count(x))*data[1].count(x), set(data[1]).intersection(data[2]))))
print("part 2:", similarity_score)I love looking at AoC solution megathreads on reddit. So many languages and so many different approaches are hard to find and observe anywhere.
Skimming some, the core of Part 1 after people have parsed and sorted:
Python:
sum([abs(x-y) for x,y in zip(left,right))
TypeScript: list1.reduce((acc, cur, i) => {
return acc += Math.abs(cur - list2[i])
}, 0)
Common Lisp: (reduce #'+ (mapcar (lambda (l r) (abs (- l r))) sorted-left sorted-right))))
Julia: sum(abs.(list1 .- list2))
Rust: Ok(zip(left, right).map(|(l, r)| l.abs_diff(r)).sum()
F#: Seq.map2 (fun x y -> abs (x - y)) xs ys |> Seq.sum
APL: +/|-⌿
Haskell: map abs $ zipWith (-) column2 column1
and then all the submissions which don't do anything like this, and have manual loops and indexing and clunky data representations or performance-optimized data representations, etc. etc.Last year I got stuck on Day 12 for a full week, and thinking about how to solve it consumed my every waking moment. I think this year, I'm going to be kind to myself and not participate so I can really enjoy the winter break from work.
A friend recently shared this with me. I think you'll like it. https://eli.li/december-adventure
As of last week there were something around 1024 people who had all 450 stars.
Only started on like day 6 of 2022, but became hooked and had some time early in 2023 to go through the previous years. Once you have a few algorithms canned, it's not too difficult and some themes repeat across years.
It's fun to brush up on stuff you don't touch all the time - actual algorithms and stuff.
Hats off to the volunteers and Eric - I aim to donate every year now - it's a great event.
Ah here it is again, 25 days of writing increasingly complex input parsers.
I completed last year's in Scryer Prolog and it was a joy. Some problems were almost impossible due to the lack of mutation (Karger's algorithm comes to mind), but file parsing was a breeze and I find Prolog programs generally beautiful. My favourite syntactical feature is the full stop at the end of clauses.