Live data from Hacker News

Advent of Code 2024

adventofcode.com

451–460 of 580 posts

Re: Advent of Code 2024

#451
I'm excited about this! I'll be sticking to Python so I can practice writing maintainable code. I'm also looking forward to keeping up with my explanations of each puzzle, which helps me (and others!) learn a lot.

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.

Re: Advent of Code 2024

#452

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…

That seems rather slow for yours, and not very fast for an optimised one. It can speed up a lot from a cold start to a warm run, my tuned code can show 8 ms in powershell 7.4 after a few runs.

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.

Re: Advent of Code 2024

#453

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_...

Can't edit my comment, here's another claiming 12.7 μs and 7 μs:

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_...

Re: Advent of Code 2024

#454

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

I 'git clone' and 'cargo run' and it downloaded 16 crates and compiled all the crates and built a debug version in 29.7 seconds.

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.

Re: Advent of Code 2024

#455
post #146

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)))

this is the least amount of loc I can think of rn

    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)

Re: Advent of Code 2024

#456

I love looking at AoC solution megathreads on reddit. So many languages and so many different approaches are hard to find and observe anywhere.

I feel like those threads would make great research opportunities. We often hear people say that code should be for people to read, incidentally for machines to execute, just be amazed at how much the readability varies from answer to answer.

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.

Re: Advent of Code 2024

#457
post #111

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

Thanks, I like this.

Re: Advent of Code 2024

#458
Aiming to get all the stars this year to round it out with 500 total - all the years, all the problems.

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.

Re: Advent of Code 2024

#460
post #251

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.

Erlang lifted it as the comma, semicolon, dot convention. When I was writing a lot of Erlang I found myself wishing it was in other languages. After not writing Erlang for a long time, I wrote a few functions recently and it was jarring. Then again I usually prefer the conventions of whatever language I'm using most at the time, unless I really dislike the language (Javascript).
Post reply on HN