-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rb
executable file
·144 lines (105 loc) · 2.71 KB
/
day12.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
# frozen_string_literal: true
input = File.read('day12.txt').lines.map(&:strip)
Position = Struct.new(:x, :y, :z)
Velocity = Struct.new(:x, :y, :z)
class Moon
attr_accessor :pos, :vel
def initialize(pos, vel)
@pos, @vel = pos, vel
end
def apply_gravity(other)
@vel.x += other.pos.x <=> @pos.x
other.vel.x += @pos.x <=> other.pos.x
@vel.y += other.pos.y <=> @pos.y
other.vel.y += @pos.y <=> other.pos.y
@vel.z += other.pos.z <=> @pos.z
other.vel.z += @pos.z <=> other.pos.z
end
def apply_velocity
@pos.x += @vel.x
@pos.y += @vel.y
@pos.z += @vel.z
end
def potential_energy
@pos.x.abs + @pos.y.abs + @pos.z.abs
end
def kinetic_energy
@vel.x.abs + @vel.y.abs + @vel.z.abs
end
def total_energy
potential_energy * kinetic_energy
end
def ==(other)
other.class == self.class && other.pos == @pos && other.vel == @vel
end
end
class System
attr_accessor :moons
def initialize(moons)
@moons = moons
end
def step
apply_gravity
apply_velocity
end
def apply_gravity
moons.combination(2).each do |a, b|
a.apply_gravity(b)
end
end
def apply_velocity
moons.each { |moon| moon.apply_velocity }
end
def potential_energy
moons.map(&:potential_energy).sum
end
def kinetic_energy
moons.map(&:kinetic_energy).sum
end
def total_energy
moons.map(&:total_energy).sum
end
def ==(other)
other.class == self.class && other.moons == @moons
end
end
# Part 1
positions = input.map do |line|
line.match(/^<x=([-]?\d+), y=([-]?\d+), z=([-+]?\d+)>$/) { |m| Position.new(*m.captures.map(&:to_i)) }
end
moons = positions.map do |pos|
Moon.new(pos, Velocity.new(0, 0, 0))
end
system = System.new(moons)
1000.times { system.step }
puts system.total_energy
# Part 2
positions = input.map do |line|
line.match(/^<x=([-]?\d+), y=([-]?\d+), z=([-+]?\d+)>$/) { |m| Position.new(*m.captures.map(&:to_i)) }
end
orig_moons = positions.map do |pos|
Moon.new(pos.dup, Velocity.new(0, 0, 0))
end
moons = positions.map do |pos|
Moon.new(pos.dup, Velocity.new(0, 0, 0))
end
orig_system = System.new(orig_moons)
system = System.new(moons)
period_x, period_y, period_z = -1, -1, -1
i = 0
loop do
system.step
i += 1
if system.moons.zip(orig_system.moons).all? { |a, b| a.pos.x == b.pos.x && a.vel.x == b.vel.x }
period_x = i
end
if system.moons.zip(orig_system.moons).all? { |a, b| a.pos.y == b.pos.y && a.vel.y == b.vel.y }
period_y = i
end
if system.moons.zip(orig_system.moons).all? { |a, b| a.pos.z == b.pos.z && a.vel.z == b.vel.z }
period_z = i
end
break if [period_x, period_y, period_z].all?(&:positive?)
end
puts [period_x, period_y, period_z].reduce(&:lcm)