Advent of Code: Day 2

For Day 2, the challenge is to calculate checksums based on either the largest difference between values or on values that are divisors of each other. I decided to try Ruby for it, here's my solution:

def line_checksum1(line)
    line = line.split(" ").map { |s| s.to_i }
    line.max - line.min

def line_checksum2(line)
    line = line.split(" ").map { |s| s.to_i }
    combination = line.combination(2).find { |p| p.max % p.min == 0}
    combination.max / combination.min

text ="input2.txt")
lines = text.split("\n")

puts "Part 1: " + { |n| line_checksum1(n)}.reduce(0, :+).to_s
puts "Part 2: " + { |n| line_checksum2(n)}.reduce(0, :+).to_s

I've never used Ruby before, so I'm confident there'll be some magic that I'm missing. But this solution doesn't seem too bad to me at first glance, at least for the scale of input that we're giving it.

Advent of Code runs every day up to Christmas, you should join in!.

Show Comments

Get the latest posts delivered right to your inbox.