Created
December 12, 2019 18:25
-
-
Save mgates/abe60ba85747256f87b57683b8d5fcc6 to your computer and use it in GitHub Desktop.
advent 2019 day 12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
class Blerg | |
# test | |
# x repeat is 2028 | |
# y repeat is 5898 | |
# z repeat is 4702 | |
# | |
# Actual | |
# x repeat is | |
# y repeat is 231614 | |
# z repeat is 102356 | |
def do_it(moons) | |
past = Set.new | |
moons = moons.lines.map(&:chomp).each_with_index.map do |line| | |
Moon.new *line.split(", ").map {|n| n.split("=").last.to_i } | |
end | |
n = 1 | |
moons.each {|m| m.xv, m.yv, m.zv = 0, 0, 0} | |
#while true | |
1_000_000.times do | |
past << moons.inspect | |
moons.each do |moon| | |
moon.xv += moons.count{|m| m.x > moon.x} | |
moon.xv -= moons.count{|m| m.x < moon.x} | |
#moon.yv += moons.count{|m| m.y > moon.y} | |
#moon.yv -= moons.count{|m| m.y < moon.y} | |
#moon.zv += moons.count{|m| m.z > moon.z} | |
#moon.zv -= moons.count{|m| m.z < moon.z} | |
end | |
moons.each do |moon| | |
moon.x += moon.xv | |
# moon.y += moon.yv | |
# moon.z += moon.zv | |
end | |
#print "#{n}..." if n % 1000 == 0 | |
if past.include? moons.inspect | |
past = Set.new | |
puts "done: #{n}" | |
end | |
n += 1 | |
end | |
end | |
Moon = Struct.new(:x, :y, :z, :xv, :yv, :zv) | |
end | |
Blerg.new.do_it(ARGF.read) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<x=-1, y=0, z=2> | |
<x=2, y=-10, z=-7> | |
<x=4, y=-8, z=8> | |
<x=3, y=5, z=-1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<x=-8, y=-10, z=0> | |
<x=5, y=5, z=10> | |
<x=2, y=-7, z=3> | |
<x=9, y=-8, z=-3> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<x=5, y=4, z=4> | |
<x=-11, y=-11, z=-3> | |
<x=0, y=7, z=0> | |
<x=-13, y=2, z=10> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment