Skip to content

Latest commit

 

History

History
101 lines (74 loc) · 4.54 KB

day2.org

File metadata and controls

101 lines (74 loc) · 4.54 KB

Day 2: Corruption Checksum

As you walk through the door, a glowing humanoid shape yells in your direction. “You there! Your state appears to be idle. Come help us repair the corruption in this spreadsheet - if we take another millisecond, we’ll have to display an hourglass cursor!”

The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet’s checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.

For example, given the following spreadsheet:

5195
753
2468
  • The first row’s largest and smallest values are 9 and 1, and their difference is 8.
  • The second row’s largest and smallest values are 7 and 3, and their difference is 4.
  • The third row’s difference is 6.
  • In this example, the spreadsheet’s checksum would be 8 + 4 + 6 = 18.

What is the checksum for the spreadsheet in your puzzle input?

Input

34583471163129917042002425167363640014162115285913040754269
277727121202569253030351818324918721139225264771381360
23163516817414041437263118631127640174517123912587214193
197201355116611212062031742289843732211736011939992088
392533892181134220171197234839193706494357733202391202508
239947102920247332422171781290421561500310049724983312211
18838063901261235373337473721267379438143995300491540623400
9186328542799178176103748720615722122539281625019273147
186194307672208351243180619749590745671707334224
18543180134534214782141981944942556424692425248578652604127
37802880236330322712523540218213458201408324024919682066
118869624157151609199765107897611941772386588601228
9036121887661969006286989212322657940168165103
71037848320872582394197141228591173880411102369143664104
317821925312973661155282486782457042260581735043182818117
837809528165282210281295101114088452854446491247

Part 1

diffs = 0
  input.each do |row|
    # Each row is an array
    sorted = row.sort
    highest = sorted.last
    lowest = sorted.first
    difference = highest - lowest

    diffs += difference
  end

  puts diffs

Part 2

“Great work; looks like we’re on the right track after all. Here’s a star for your effort.” However, the program seems a little worried. Can programs be worried?

“Based on what we’re seeing, it looks like all the User wanted is some information about the evenly divisible values in the spreadsheet. Unfortunately, none of us are equipped for that kind of calculation - most of us specialize in bitwise operations.”

It sounds like the goal is to find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line’s result.

For example, given the following spreadsheet:

5928
9473
3865
  • In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4.
  • In the second row, the two numbers are 9 and 3; the result is 3.
  • In the third row, the result is 2.
  • In this example, the sum of the results would be =4 + 3 + 2 = 9.
summs = 0

input.each do |row|
  # input is array of arrays
  row.each do |num|
    row.each do |num2|
      next if num == num2
      if num.modulo(num2) == 0
        summs += num / num2
      end
    end
  end
end

puts summs