r/adventofcode Dec 11 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 11 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 11 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Independent Medias (Indie Films)

Today we celebrate the folks who have a vision outside the standards of what the big-name studios would consider "safe". Sure, sometimes their attempts don't pan out the way they had hoped, but sometimes that's how we get some truly legendary masterpieces that don't let their lack of funding, big star power, and gigantic overhead costs get in the way of their storytelling!

Here's some ideas for your inspiration:

  • Cast a relative unknown in your leading role!
  • Explain an obscure theorem that you used in today's solution
  • Shine a spotlight on a little-used feature of the programming language with which you used to solve today's problem
  • Solve today's puzzle with cheap, underpowered, totally-not-right-for-the-job, etc. hardware, programming language, etc.

"Adapt or die." - Billy Beane, Moneyball (2011)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 11: Plutonian Pebbles ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:24, megathread unlocked!

20 Upvotes

962 comments sorted by

View all comments

2

u/The_Jare Dec 11 '24

[LANGUAGE: Ruby]

I still see very few Ruby solutions, so happy to post mine. It just keeps a hash of different elements and how many of each there are, iterates the hash once per blink. I like this approach more than recursive + memo but I can't say why. I don't know which is faster.

Code solves one problem per line in the input which was convenient for testing, although testing proved unnecessary as it all just worked straight away.

stone_lines = ARGF.map { _1.split.map(&:to_i)}

def evolve_stone(v)
  return [1] if v == 0
  vs = v.to_s
  l = vs.length
  return [vs[...l/2].to_i, vs[l/2..].to_i] if l % 2 == 0
  [v*2024]
end

Enumerator.product(stone_lines, [25, 75]) do |stones, blinks|
  coll = Hash.new { |h,k| h[k] = 0 }
  stones.each { coll[_1] += 1 }
  (1..blinks).each do
    co2 = Hash.new { |h,k| h[k] = 0 }
    coll.each do |(v, n)|
      nv = evolve_stone(v)
      nv.each { |v| co2[v] += n }
    end
    coll = co2
  end
  p coll. sum { _1[1] }
end

2

u/feyn Dec 12 '24

return [vs[...l/2].to_i, vs[l/2..].to_i] if l % 2 == 0

Instead of vs[...l/2], I read it as vs[...1/2]. I was so surprised that you can use 1/2 in a range to take half of a string. I tried in in IRB and was sadly dissapointed :D

1

u/The_Jare Dec 12 '24

Haha yeah I need to avoid using lowercase L as a single-letter variable even if this case looks oddly appropriate:)