r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 9

Transcript:

Studies show that AoC programmers write better code after being exposed to ___.


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

edit: Leaderboard capped, thread unlocked at 00:29:13!

22 Upvotes

283 comments sorted by

View all comments

Show parent comment

1

u/tomthecool Dec 10 '18

You're right... But it's quite easy to create a basic implementation via a custom class:

class Node
  attr_accessor :val, :prev_node, :next_node
  def initialize(val, prev_node = self, next_node = self)
    @val = val
    @prev_node = prev_node
    @next_node = next_node
  end

  def replace_next(val)
    new_node = Node.new(val, self, @next_node)
    @next_node.prev_node = new_node
    @next_node = new_node
  end

  def delete
    @prev_node.next_node = @next_node
    @next_node.prev_node = @prev_node
    @val
  end
end

You can then use this to write some solution like:

(1..turns).each do |turn|
  if turn % 23 == 0
    7.times { current_node = current_node.prev_node }
    removed_marble = current_node.delete
    current_node = current_node.next_node
    scores[current_player-1] += (removed_marble + turn)
  else
    current_node = current_node.next_node
    current_node.replace_next(turn)
    current_node = current_node.next_node
  end

  current_player %= players
  current_player += 1
end