Skip to content

Instantly share code, notes, and snippets.

@Rydgel
Created December 2, 2017 00:04
Show Gist options
  • Save Rydgel/59f1945ed69981e9ef0e02ba60cd83d8 to your computer and use it in GitHub Desktop.
Save Rydgel/59f1945ed69981e9ef0e02ba60cd83d8 to your computer and use it in GitHub Desktop.
Day1 - Advent of Code
const INPUT: &str = include_str!("../input.txt");
fn convert_input(input: &str) -> Vec<u32> {
input
.to_owned()
.chars()
.filter_map(|s| s.to_digit(10))
.collect()
}
fn solver(digits: &[u32], step: usize) -> u32 {
digits.iter().enumerate().fold(0, |acc, (index, &current)| {
let next = digits[(index + step) % digits.len()];
if current == next { acc + current } else { acc }
})
}
fn main() {
let digits = convert_input(INPUT);
println!("{:?}", solver(&digits, 1));
println!("{:?}", solver(&digits, digits.len() / 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment