Code for the Advent of Code event
aoc advent-of-code
at rust 30 lines 667 B view raw
1#!/usr/bin/env ruby 2# frozen_string_literal: true 3 4equations = ARGF.map { _1.scan(/\d+/).map(&:to_i) } 5 6class Integer 7 def c(other) 8 "#{self}#{other}".to_i 9 end 10end 11 12def validate(t, values, ops, head) 13 return ops.any? { t == head.send(_1, values[0]) } if values.size == 1 14 15 ops.any? { validate t, values[1..], ops, head.send(_1, values[0]) } 16end 17 18PART1_OPS = %i[+ *].freeze 19PART2_OPS = %i[+ * c].freeze 20 21failed = [] 22 23part1 = equations.filter { 24 result = validate(_1[0], _1[2..], PART1_OPS, _1[1]) 25 next result if result 26 failed << _1 27 false 28}.sum { _1[0] } 29puts part1 30puts failed.filter { validate(_1[0], _1[2..], PART2_OPS, _1[1]) }.sum { _1[0] } + part1