From bd49344d801c78d374e52953cbd5b17f8b9454b2 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Thu, 7 Dec 2017 15:48:55 +1000 Subject: [PATCH] Add day 2 part 2 solution --- 2017/day/2/problem.txt | 23 +++++++++++++++++++++++ 2017/day/2/src/main.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/2017/day/2/problem.txt b/2017/day/2/problem.txt index d1a8055..b49a820 100644 --- a/2017/day/2/problem.txt +++ b/2017/day/2/problem.txt @@ -17,3 +17,26 @@ For example, given the following spreadsheet: In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18. What is the checksum for the spreadsheet in your puzzle input? + +--- Part Two --- + +"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: + +5 9 2 8 +9 4 7 3 +3 8 6 5 + + 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. + +What is the sum of each row's result in your puzzle input? + diff --git a/2017/day/2/src/main.rs b/2017/day/2/src/main.rs index 122810b..2a3e28e 100644 --- a/2017/day/2/src/main.rs +++ b/2017/day/2/src/main.rs @@ -19,6 +19,7 @@ fn main() { .collect::>(); println!("{}", checksum(&spreadsheet)); + println!("{}", evenly_divisible(&spreadsheet)); } fn checksum(spreadsheet: &Spreadsheet) -> i32 { @@ -28,6 +29,26 @@ fn checksum(spreadsheet: &Spreadsheet) -> i32 { .sum() } +fn evenly_divisible_row(row: &[i32]) -> Option { + for i in 0..row.len() { + for j in 0..row.len() { + if i == j { continue }; + + if row[i] % row[j] == 0 { + return Some(row[i] / row[j]) + } + } + } + + None +} + +fn evenly_divisible(spreadsheet: &Spreadsheet) -> i32 { + spreadsheet.iter() + .flat_map(|row| evenly_divisible_row(row)) + .sum() +} + #[test] fn test_checksum() { let input = vec![ @@ -39,3 +60,15 @@ fn test_checksum() { // In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18. assert_eq!(checksum(&input), 18); } + +#[test] +fn test_evenly_divisible() { + let input = vec![ + vec![5, 9, 2, 8], + vec![9, 4, 7, 3], + vec![3, 8, 6, 5], + ]; + + // In this example, the sum of the results would be 4 + 3 + 2 = 9. + assert_eq!(evenly_divisible(&input), 9); +}