AoC2024/day05/solution1.rb

25 lines
602 B
Ruby
Raw Normal View History

2025-01-01 18:23:18 +00:00
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'pry'
# ifile = 'input.sample'
ifile = 'input'
input = File.readlines(ifile).map(&:strip).reject(&:empty?)
updates, rules = input.partition { _1.match?(',') }
rules.map! { _1.split('|').map(&:to_i) }
updates.map! { _1.split(',').map(&:to_i) }
updates.select! do |update|
update.combination(2).map do |pair|
rule = rules.select { _1.include?(pair[0]) and _1.include?(pair[1]) }.flatten
next if rule.empty?
rule == pair
end.all?
end
solution = updates.map { |update| update[update.size / 2] }.sum
# binding.pry
puts solution