diff --git a/2017/day/1/src/main.rs b/2017/day/1/src/main.rs index 156ef03..730ca10 100644 --- a/2017/day/1/src/main.rs +++ b/2017/day/1/src/main.rs @@ -6,34 +6,19 @@ fn main() { let mut file = File::open("input").expect("unable to open input file"); file.read_to_end(&mut input).expect("error reading input"); - println!("{}", captcha(&input)); - println!("{}", captcha2(&input)); -} - -fn captcha(input: &[u8]) -> u64 { - let digits = input.iter() - .take_while(|&&chr| chr >= 0x30 && chr <= 0x39) + let digits = input.into_iter() + .take_while(|&chr| chr >= 0x30 && chr <= 0x39) .collect::>(); - let mut sum = 0; - for idx in 0..digits.len() { - if digits[idx] == digits[(idx + 1) % digits.len()] { - sum += *digits[idx] as u64 - 0x30; - } - } - - sum + println!("{}", captcha(digits.as_slice(), 1)); + println!("{}", captcha(digits.as_slice(), digits.len() / 2)); } -fn captcha2(input: &[u8]) -> u64 { - let digits = input.iter() - .take_while(|&&chr| chr >= 0x30 && chr <= 0x39) - .collect::>(); - +fn captcha(digits: &[u8], offset: usize) -> u64 { 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; + if digits[idx] == digits[(idx + offset) % digits.len()] { + sum += digits[idx] as u64 - 0x30; } } @@ -44,54 +29,54 @@ fn captcha2(input: &[u8]) -> u64 { // third digit (2) matches the fourth digit. #[test] fn test_captcha_example1() { - assert_eq!(captcha(b"1122"), 3); + assert_eq!(captcha(b"1122", 1), 3); } // 1111 produces 4 because each digit (all 1) matches the next. #[test] fn test_captcha_example2() { - assert_eq!(captcha(b"1111"), 4); + assert_eq!(captcha(b"1111", 1), 4); } // 1234 produces 0 because no digit matches the next. #[test] fn test_captcha_example3() { - assert_eq!(captcha(b"1234"), 0); + assert_eq!(captcha(b"1234", 1), 0); } // 91212129 produces 9 because the only digit that matches the next one is the last digit, 9. #[test] fn test_captcha_example4() { - assert_eq!(captcha(b"91212129"), 9); + assert_eq!(captcha(b"91212129", 1), 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); + assert_eq!(captcha(b"1212", 2), 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); + assert_eq!(captcha(b"1221", 2), 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); + assert_eq!(captcha(b"123425", 3), 4); } // 123123 produces 12. #[test] fn test_captcha2_example4() { - assert_eq!(captcha2(b"123123"), 12); + assert_eq!(captcha(b"123123", 3), 12); } // 12131415 produces 4. #[test] fn test_captcha2_example5() { - assert_eq!(captcha2(b"12131415"), 4); + assert_eq!(captcha(b"12131415", 4), 4); }