Day 01 - Inverse Captcha
Language: Rust
Problem https://adventofcode.com/2017/day/1
Part 1: A string of digits treated as a circular list. Sum all digits that match the next one, with the last digit wrapping back to the first.
I zip the string with itself offset by one to compare consecutive pairs, then handle the wrap manually at the end:
for (c1, c2) in input.chars().zip(input.chars().skip(1)) {
if c1 == c2 {
sum += c1.to_digit(10).unwrap();
}
}
if let (Some(first), Some(last)) = (input.chars().next(), input.chars().last()) {
if first == last {
sum += first.to_digit(10).unwrap();
}
}
Part 2: Now compare each digit to the one halfway around the list. Since both halves are symmetric, every match contributes twice. I zip the first half with the second half and multiply each match by 2:
let ans: u32 = input.chars()
.take(input.len() / 2)
.zip(input.chars().skip(input.len() / 2))
.filter(|(a, b)| a == b)
.map(|(c, _)| 2 * c.to_digit(10).unwrap())
.sum();
Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2017/day01.rs
No C writeup yet.