-
Notifications
You must be signed in to change notification settings - Fork 1
/
05-1.rb
executable file
·75 lines (63 loc) · 1.41 KB
/
05-1.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
#!/usr/bin/env ruby
class Map
attr_accessor :from, :to, :map
def initialize(from, to)
@from = from
@to = to
@map = []
end
def add_mapping(source, destination, length)
@map << {source: source, destination: destination, length: length}
end
def covers?(source, length, test)
test >= source and test < source + length
end
def [](input)
@map.each do |mapping|
if covers?(mapping[:source], mapping[:length], input)
return mapping[:destination] - mapping[:source] + input
end
end
input
end
def to_s
s = "<#{self.class} #{@from}-#{to}: "
@map.each do |m|
s += "#{m[:source]} #{m[:length]} #{m[:destination]} "
end
s += ">"
s
end
end
input = File.read('05.input').lines.map(&:strip)
seeds = nil
maps = []
map = nil
input.each do |line|
if line.start_with? 'seeds: '
seeds = line.split(':')[1].split.map(&:to_i)
elsif line.empty?
maps << map if map
map = nil
elsif line.end_with? 'map:'
names = line.split[0].split('-')
map = Map.new(names[0], names[2])
else
mapping = line.split.map(&:to_i)
map.add_mapping(mapping[1], mapping[0], mapping[2])
end
end
maps << map
locations = seeds.map do |seed|
from = 'seed'
pos = seed
loop do
map = maps.find { |m| m.from == from }
to = map.to
pos = map[pos]
break if to == 'location'
from = to
end
pos
end
print locations.min, "\n"