From b941cf4ec7d4d87f134cf1bac1b1ec6524cd4d88 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sat, 2 Dec 2017 13:24:48 +0100 Subject: [PATCH] Slightly prettier --- README.md | 5 +++++ checksum.rb | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b6ebafc..87ebf3d 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,8 @@ Some of the code will be somewhat quick and dirty when I first upload it, since For the first challenge, part one, one had to review a sequence of digits and find the `sum` of all the digits, which match the next digit in the list. This list is circular, thus the last digit will be able to match with the first digit, such that $`1221`$ will have a `sum` of $`3`$. For the second part, one now had to, instead of considering the next digit in the list, consider the digit *halfway around* the circular list, so $`1212`$ would produce a `sum` of $`6`$. + +## The 2nd of December +For the second challenge, part one, the objective was to look through a bunch of lists, find the maximum and minimum value and then subtract these for each list. In the end, the results of the different subtractions had to be summed up and this would be the result. + +For the second part, the calculation of the checksum had changed to now being calculated from finding the only pair of evenly divisible numbers from each list and then sum up the results of the divisions. diff --git a/checksum.rb b/checksum.rb index 61c72df..28c2a5f 100644 --- a/checksum.rb +++ b/checksum.rb @@ -1,3 +1,9 @@ +class Integer + def is_lcm?(other) + self.lcm(other) == self and other != self + end +end + a = "515 912 619 2043 96 93 2242 1385 2110 860 2255 621 1480 118 1230 99 161 6142 142 1742 237 6969 211 4314 5410 4413 3216 6330 261 3929 5552 109 1956 4470 3577 619 105 3996 128 1666 720 4052 108 132 2652 306 1892 1869 @@ -17,11 +23,11 @@ a = "515 912 619 2043 96 93 2242 1385 2110 860 2255 621 1480 118 1230 99 splitted = a.split("\n").map! {|i| i.split("\t").map(&:to_i)} puts splitted.reduce(0) {|sum, row| sum += row.max-row.min} - + result = 0 splitted.each do |row| row.permutation(2).to_a.find do |ele| - if (ele[0].lcm(ele[1]) == ele[0]) then + if ele[0].is_lcm?(ele[1]) then result += ele[0] / ele[1] end end @@ -30,3 +36,4 @@ end puts result +