advent_of_code/2024_1.rb
Code:
# Advent of Code 2024, Day 1: Historian Hysteria
# https://adventofcode.com/2024/day/1
require_relative './helpers.rb'
def part_one(a, b)
a.zip(b).sum { |c, d| (c - d).abs }
end
def part_two(a, b)
a.sum { |c| c * b.count(c) }
end
def solve(data)
d = data.map { |l| l.split(' ').map(&:to_i) }
a, b = d.map(&:first).sort, d.map(&:last).sort
[part_one(a, b), part_two(a, b)]
end
puts solve(get_dataset(year: 2024, day: 1, type: 'example'))
puts solve(get_dataset(year: 2024, day: 1, type: 'input'))
Output:
11
31
2378066
18934359