mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-09-19 21:28:26 +00:00
Add solution to part 2 of day 1
This commit is contained in:
parent
a89cdfc860
commit
b35039cb2f
2 changed files with 65 additions and 4 deletions
|
@ -7,3 +7,17 @@ For example:
|
||||||
1234 produces 0 because no digit matches the next.
|
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.
|
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.
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ fn main() {
|
||||||
file.read_to_end(&mut input).expect("error reading input");
|
file.read_to_end(&mut input).expect("error reading input");
|
||||||
|
|
||||||
println!("{}", captcha(&input));
|
println!("{}", captcha(&input));
|
||||||
|
println!("{}", captcha2(&input));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn captcha(input: &[u8]) -> u64 {
|
fn captcha(input: &[u8]) -> u64 {
|
||||||
|
@ -24,27 +25,73 @@ fn captcha(input: &[u8]) -> u64 {
|
||||||
sum
|
sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn captcha2(input: &[u8]) -> u64 {
|
||||||
|
let digits = input.iter()
|
||||||
|
.take_while(|&&chr| chr >= 0x30 && chr <= 0x39)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
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
|
// 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.
|
// third digit (2) matches the fourth digit.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example1() {
|
fn test_captcha_example1() {
|
||||||
assert_eq!(captcha(b"1122"), 3);
|
assert_eq!(captcha(b"1122"), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1111 produces 4 because each digit (all 1) matches the next.
|
// 1111 produces 4 because each digit (all 1) matches the next.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example2() {
|
fn test_captcha_example2() {
|
||||||
assert_eq!(captcha(b"1111"), 4);
|
assert_eq!(captcha(b"1111"), 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1234 produces 0 because no digit matches the next.
|
// 1234 produces 0 because no digit matches the next.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example3() {
|
fn test_captcha_example3() {
|
||||||
assert_eq!(captcha(b"1234"), 0);
|
assert_eq!(captcha(b"1234"), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
|
// 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example4() {
|
fn test_captcha_example4() {
|
||||||
assert_eq!(captcha(b"91212129"), 9);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue