-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday25.rb
82 lines (61 loc) · 1.63 KB
/
day25.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
require_relative 'toolbox'
# plan:
# [X] test harness
# [ ] figure out how to solve
# [ ] production
def taxi_distance(c1, c2)
x1, y1, z1, a1 = *c1
x2, y2, z2, a2 = *c2
(x2 - x1).abs + (y2 - y1).abs + (z2 - z1).abs + (a2 - a1).abs
end
def star_mapper(stars)
constellations = []
cluster = nil
loop do
break if stars.empty?
star = stars.pop
# LOGGER.debug { "#{star.inspect} with #{stars.inspect}" }
cluster = constellation(star, stars)
# LOGGER.debug { "found cluster: #{cluster.inspect}" }
constellations << cluster
stars.reject! {|s| cluster.include?(s) }
end
constellations.size
end
def constellation(star, coordinates)
# LOGGER.debug { "checking #{star.inspect} with #{coordinates.inspect}" }
members = []
members << star
original = coordinates.dup
candidates = coordinates.dup
loop do
break if candidates.empty?
candidate = candidates.pop
next if members.include?(candidate)
members.each do |member|
if taxi_distance(member, candidate) <= 3
members << candidate
candidates = original.dup
break
end
end
end
members.uniq!
members
end
def run_tests
puts "testing:"
[2, 1, 4, 3, 8].each_with_index do |answer, i|
f = "data/day25_test_points_0#{i}.txt"
stars = raw_lines(f).map {|l| l.split(',').map(&:to_i) }
break unless test(answer, star_mapper(stars))
end
puts
end
def part1
stars = raw_lines('data/day25_production_points.txt').map {|l| l.split(',').map(&:to_i) }
star_mapper(stars)
end
run_tests
puts "How many constellations are formed by the fixed points in spacetime?"
puts "Answer: #{part1}"