r/adventofcode • • Dec 15 '16

SOLUTION MEGATHREAD --- 2016 Day 15 Solutions ---

--- Day 15: Timing is Everything ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


ZAMENHOFA TAGO ESTAS DEVIGA [?]

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!

5 Upvotes

121 comments sorted by

View all comments

Show parent comment

2

u/mperham Dec 15 '16

I created the input manually rather than regexp'ing it. Enumerable#all? came in super-handy:

discs = [
  [0, 5, 2],
  [1, 13, 7],
  [2, 17, 10],
  [3, 3, 2],
  [4, 19, 9],
  [5, 7, 0],
  [6, 11, 0],
]

count = 0
loop do
  (puts count; break) if discs.all? do |disc|
    (disc[2] + count + (disc[0] + 1)) % disc[1] == 0
  end
  count += 1
end

0

u/[deleted] Dec 15 '16

[deleted] — view deleted comment

1

u/mperham Dec 15 '16

Whoa, that first line is amazing. Kudos.

Side note, I always use #detect because #find clashes with ActiveRecord::Base#find.

1

u/eregontp Dec 15 '16

Mine is p (0..Float::INFINITY).find { |t| disks.all? { |dt, n, pos| (t + pos + dt) % n == 0 } }

Nice trick with 0.step