2024-12-30 03:38:34 +00:00
|
|
|
#!/usr/bin/env ruby
|
2024-12-30 14:27:55 +00:00
|
|
|
# frozen_string_literal: true
|
2024-12-30 03:38:34 +00:00
|
|
|
|
|
|
|
require 'pry'
|
|
|
|
|
|
|
|
# Part 1
|
|
|
|
|
2024-12-30 14:27:55 +00:00
|
|
|
real_input = File.read('./input')
|
2024-12-30 03:38:34 +00:00
|
|
|
|
|
|
|
input = real_input
|
|
|
|
|
2024-12-30 14:27:55 +00:00
|
|
|
col1 = input.split("\n").map(&:split).map { |pair| pair[0].to_i }.sort
|
|
|
|
col2 = input.split("\n").map(&:split).map { |pair| pair[1].to_i }.sort
|
2024-12-30 03:38:34 +00:00
|
|
|
|
2024-12-30 14:27:55 +00:00
|
|
|
# binding.pry
|
2024-12-30 03:38:34 +00:00
|
|
|
|
2024-12-30 14:27:55 +00:00
|
|
|
solution = col1.zip(col2).map { |left, right| (right - left).abs }.sum
|
2024-12-30 03:38:34 +00:00
|
|
|
puts solution
|
|
|
|
|
|
|
|
# Part 2
|
|
|
|
|
2024-12-30 14:27:55 +00:00
|
|
|
p2 = col1.map { |value| value * col2.count(value) }.sum
|
2024-12-30 03:38:34 +00:00
|
|
|
puts p2
|