-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5part2.rb
140 lines (133 loc) · 3.53 KB
/
day5part2.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
file = File.open "day5in.txt"
seeds = []
seeds_to_soil = {}
soil_to_fert = {}
fert_to_water = {}
water_to_light = {}
light_to_temp = {}
temp_to_humidity = {}
humidity_to_location = {}
order = [seeds_to_soil, soil_to_fert, fert_to_water, water_to_light, light_to_temp, temp_to_humidity, humidity_to_location]
order_index = 0
file.each_line { |raw_line| raw_line.chomp.then do |l|
next if l.empty? && seeds_to_soil.empty?
if seeds.length == 0
s = l.match /seeds: (.*)/
s.captures[0].split(" ").map { |it| it.to_i }.each_slice(2) { |seed_slice| seeds.push(seed_slice[0]..(seed_slice[0] + seed_slice[1] - 1)) }
elsif l.empty?
order_index += 1
else
next if l.include? ":"
(dest, source, dist) = l.split(" ").map { |it| it.to_i }
order[order_index][source..(source + dist - 1)] = { start: dest, finish: (dest + dist - 1), dist: dist }
end
end
}
file.close
min = nil
class Range
def overlaps?(other)
raise "exclude_end? not implemented" if self.exclude_end? || other.exclude_end?
other.begin <= self.end && self.begin <= other.end
end
end
def range_overlaps(from, to)
raise "no overlap" unless to.overlaps? from
if from.begin < to.begin
if from.end > to.end
# |--------| from
# |---| to
[
{ range: (from.begin..(to.begin - 1)), overlaps: false },
{ range: to, overlaps: true },
{ range: (to.end + 1)..from.end, overlaps: false }
]
elsif from.end == to.end
# |------| from
# |---| to
[
{ range: (from.begin..(to.begin - 1)), overlaps: false },
{ range: to, overlaps: true }
]
else
# |------| from
# |------| to
[
{ range: (from.begin..(to.begin - 1)), overlaps: false },
{ range: (to.begin..from.end), overlaps: true }
]
end
elsif from.begin == to.begin
if from.end > to.end
# |--------| from
# |---| to
[
{ range: to, overlaps: true },
{ range: (to.end + 1)..from.end, overlaps: false }
]
elsif from.end == to.end
# |------| from
# |------| to
[
{ range: to, overlaps: true }
]
else
# |------| from
# |---------| to
[
{ range: from, overlaps: true },
]
end
else
if from.end < to.end
# |---| from
# |--------| to
[
{ range: from, overlaps: true },
]
elsif from.end == to.end
# |-----| from
# |-------| to
[
{ range: from, overlaps: true }
]
else
# |------| from
# |------| to
[
{ range: (from.begin..to.end), overlaps: true },
{ range: (to.end + 1)..from.end, overlaps: false }
]
end
end
end
seeds.each do |seed|
ranges = [seed]
order.each do |mapping|
to_process = ranges
processed = []
while to_process.size > 0
cur = to_process.pop
anything_changed = false
mapping.each do |k, v|
next unless k.overlaps? cur
anything_changed = true
split_ranges = range_overlaps cur, k
split_ranges.each do |r|
if r[:overlaps]
range_diff = v[:start] - k.begin
new_begin = r[:range].begin + range_diff
new_end = r[:range].end + range_diff
processed.push(new_begin..new_end)
else
to_process.unshift r[:range]
end
end
end
processed << cur unless anything_changed
end
ranges = processed
end
ranges.each { |r| min = r.begin if min.nil? || r.begin < min }
end
puts min