Skip to content

Instantly share code, notes, and snippets.

@BenConstable
Last active December 7, 2017 11:08
Show Gist options
  • Save BenConstable/3e3675962b3065a804b611324e725053 to your computer and use it in GitHub Desktop.
Save BenConstable/3e3675962b3065a804b611324e725053 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 1 (in Rust)
fn inverse_captcha(input: &str) -> u32 {
if input.len() <= 1 {
return 0u32;
}
let mut prev = 0;
let mut total: u32 = 0;
let mut numbers: Vec<u32> = input.chars().map(|c| c.to_digit(10).unwrap()).collect();
let firstNumber = numbers[0];
numbers.push(firstNumber);
for num in numbers.iter() {
if prev == *num {
total += *num;
}
prev = *num;
}
total
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_inverse_captcha_1122() {
assert!(inverse_captcha("1122") == 3);
}
#[test]
fn check_inverse_captcha_1111() {
assert!(inverse_captcha("1111") == 4);
}
#[test]
fn check_inverse_captcha_1234() {
assert!(inverse_captcha("1234") == 0);
}
#[test]
fn check_inverse_captcha_91212129() {
assert!(inverse_captcha("91212129") == 9);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment