From b35039cb2f37348582d07c95727b6d47e162eb59 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Sun, 3 Dec 2017 09:02:11 +1000 Subject: [PATCH] Add solution to part 2 of day 1 --- 2017/day/1/problem.txt | 14 +++++++++++ 2017/day/1/src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/2017/day/1/problem.txt b/2017/day/1/problem.txt index 7fd9dc1..5d03857 100644 --- a/2017/day/1/problem.txt +++ b/2017/day/1/problem.txt @@ -7,3 +7,17 @@ For example: 1234 produces 0 because no digit matches the next. 91212129 produces 9 because the only digit that matches the next one is the last digit, 9. +--- Part Two --- + +You notice a progress bar that jumps to 50% completion. Apparently, the door isn't yet satisfied, but it did emit a star as encouragement. The instructions change: + +Now, instead of considering the next digit, it wants you to consider the digit halfway around the circular list. That is, if your list contains 10 items, only include a digit in your sum if the digit 10/2 = 5 steps forward matches it. Fortunately, your list has an even number of elements. + +For example: + + 1212 produces 6: the list contains 4 items, and all four digits match the digit 2 items ahead. + 1221 produces 0, because every comparison is between a 1 and a 2. + 123425 produces 4, because both 2s match each other, but no other digit has a match. + 123123 produces 12. + 12131415 produces 4. + diff --git a/2017/day/1/src/main.rs b/2017/day/1/src/main.rs index 7ae335b..156ef03 100644 --- a/2017/day/1/src/main.rs +++ b/2017/day/1/src/main.rs @@ -7,6 +7,7 @@ fn main() { file.read_to_end(&mut input).expect("error reading input"); println!("{}", captcha(&input)); + println!("{}", captcha2(&input)); } fn captcha(input: &[u8]) -> u64 { @@ -24,27 +25,73 @@ fn captcha(input: &[u8]) -> u64 { sum } +fn captcha2(input: &[u8]) -> u64 { + let digits = input.iter() + .take_while(|&&chr| chr >= 0x30 && chr <= 0x39) + .collect::>(); + + let mut sum = 0; + for idx in 0..digits.len() { + if digits[idx] == digits[(idx + (digits.len() / 2)) % digits.len()] { + sum += *digits[idx] as u64 - 0x30; + } + } + + sum +} + // 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the // third digit (2) matches the fourth digit. #[test] -fn test_example1() { +fn test_captcha_example1() { assert_eq!(captcha(b"1122"), 3); } // 1111 produces 4 because each digit (all 1) matches the next. #[test] -fn test_example2() { +fn test_captcha_example2() { assert_eq!(captcha(b"1111"), 4); } // 1234 produces 0 because no digit matches the next. #[test] -fn test_example3() { +fn test_captcha_example3() { assert_eq!(captcha(b"1234"), 0); } // 91212129 produces 9 because the only digit that matches the next one is the last digit, 9. #[test] -fn test_example4() { +fn test_captcha_example4() { assert_eq!(captcha(b"91212129"), 9); } + +// 1212 produces 6: the list contains 4 items, and all four digits match the digit 2 items ahead. +#[test] +fn test_captcha2_example1() { + assert_eq!(captcha2(b"1212"), 6); +} + +// 1221 produces 0, because every comparison is between a 1 and a 2. +#[test] +fn test_captcha2_example2() { + assert_eq!(captcha2(b"1221"), 0); +} + +// 123425 produces 4, because both 2s match each other, but no other digit has a match. +#[test] +fn test_captcha2_example3() { + assert_eq!(captcha2(b"123425"), 4); +} + +// 123123 produces 12. +#[test] +fn test_captcha2_example4() { + assert_eq!(captcha2(b"123123"), 12); +} + +// 12131415 produces 4. +#[test] +fn test_captcha2_example5() { + assert_eq!(captcha2(b"12131415"), 4); +} +