#!/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