Code for the Advent of Code event
aoc advent-of-code
at rust 13 lines 401 B view raw
1# frozen_string_literal: true 2 3# Function courtesy of http://stackoverflow.com/a/3852809 4def invert(arr, values) 5 counts = values.each_with_object(Hash.new(0)) { |v, h| h[v] += 1 } 6 arr.reject { |e| counts[e] -= 1 unless counts[e].zero? } 7end 8 9def count_triangles(list) 10 list.reduce(0) do |a, e| 11 a + (e.permutation(2).all? { |perm| perm.inject(&:+) > invert(e, perm).first } ? 1 : 0) 12 end 13end