mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Add day 2 part 2 solution
This commit is contained in:
parent
a8e0515900
commit
bd49344d80
2 changed files with 56 additions and 0 deletions
|
@ -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?
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ fn main() {
|
|||
.collect::<Vec<_>>();
|
||||
|
||||
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<i32> {
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue