m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/16/a.rb
blob: dd3eea402d308cbeaea6150decb297b876ab03e9 (plain)
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
sections = File.read('input.txt').split "\n\n"

fields = sections[0].split "\n"
tickets = sections[2].split "\n"

valid_values = Hash.new false

fields.each do |field|
  ranges = field.split(/.*: | or /).slice(1, 2)
  ranges.each do |range|
    from, to = range.split('-').map {|n| n.to_i}
    (from..to).each do |n|
      valid_values[n] = true
    end
  end
end

invalid = 0
tickets.each do |ticket|
  values = ticket.split(',').map {|n| n.to_i}
  values.each do |n|
    if !valid_values[n]
      invalid += n
    end
  end
end

puts invalid