From 3c99f5cf039fe8df1fa511cf289502854e84b5c1 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Mon, 19 Dec 2016 23:20:29 -0500 Subject: [PATCH] [2016][ruby][19.1] --- 2016/ruby/day_19.rb | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/2016/ruby/day_19.rb b/2016/ruby/day_19.rb index 6ec925f..c6a7ee1 100644 --- a/2016/ruby/day_19.rb +++ b/2016/ruby/day_19.rb @@ -1,16 +1,35 @@ -Elf = Struct.new(:id, :presents) +def solve(n, debug: false) + elves = Array.new(n) {|i| i+1 } -n = 3017957 -elves = Array.new(n) {|i| Elf.new(i+1, 1) } + until elves.size == 1 + across = elves.size / 2 + elves.delete_at(across) + elves.rotate! + end -until elves.size == 1 - elf = elves.shift - next if elf.presents.zero? + elves.first +end - elf.presents += elves.first.presents - elves.first.presents = 0 +# This takes too long +# n = 3017957 +# puts solve(n) - elves << elf +# So instead use this to find the pattern! +(1..100).each do |i| + puts "#{i}: #{solve(i)}" end -p elves +# At 3**n, the counter resets +# At 2*3**n, the counter goes up by two instead of one +include Math +def formula(n) + x = log(n, 3).to_i + a = 3**x + b = 2*a + + (n == a) ? a : n - a + [(n - b), 0].max +end + +(1..100).each do |n| + puts "#{n}: #{solve(n)} #{formula(n)}" +end