Created
January 17, 2026 22:50
-
-
Save icub3d/ad62d7fdec8c685a19d45767fcd7ff2c to your computer and use it in GitHub Desktop.
Solution for Advent of Code 2017 Day 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::time::Instant; | |
| const INPUT: &str = include_str!("inputs/day01.txt"); | |
| fn parse(input: &str) -> impl Iterator<Item = u32> { | |
| input.trim().chars().map(|c| c.to_digit(10).unwrap()) | |
| } | |
| fn p1(input: &str) -> u32 { | |
| let input = parse(input).collect::<Vec<_>>(); | |
| input | |
| .iter() | |
| .zip(input.iter().cycle().skip(1)) | |
| .filter(|(l, r)| l == r) | |
| .map(|(l, _)| l) | |
| .sum() | |
| } | |
| fn p2(input: &str) -> u32 { | |
| let input = parse(input).collect::<Vec<_>>(); | |
| input | |
| .iter() | |
| .zip(input.iter().cycle().skip(input.len() / 2)) | |
| .filter(|(l, r)| l == r) | |
| .map(|(l, _)| l) | |
| .sum() | |
| } | |
| fn main() { | |
| let now = Instant::now(); | |
| let solution = p1(INPUT); | |
| println!("p1 {:?} {}", now.elapsed(), solution); | |
| let now = Instant::now(); | |
| let solution = p2(INPUT); | |
| println!("p2 {:?} {}", now.elapsed(), solution); | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| #[test] | |
| fn test_p1() { | |
| let input = "1111"; | |
| assert_eq!(p1(input), 4); | |
| } | |
| #[test] | |
| fn test_p2() { | |
| let input = "12131415"; | |
| assert_eq!(p2(input), 4); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment