Skip to content

Instantly share code, notes, and snippets.

@icub3d
Created January 17, 2026 22:50
Show Gist options
  • Select an option

  • Save icub3d/ad62d7fdec8c685a19d45767fcd7ff2c to your computer and use it in GitHub Desktop.

Select an option

Save icub3d/ad62d7fdec8c685a19d45767fcd7ff2c to your computer and use it in GitHub Desktop.
Solution for Advent of Code 2017 Day 1
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