Created
December 1, 2017 21:52
-
-
Save archer884/b2ba487df628d418d5993aac5a903ad9 to your computer and use it in GitHub Desktop.
aoc-1 (Rust)
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
pub(crate) struct CaptchaIter<T> { | |
state: IterState, | |
source: T, | |
} | |
impl<T: Iterator<Item = u8>> CaptchaIter<T> { | |
pub fn new<S>(source: S) -> Self | |
where | |
S: IntoIterator<Item = u8, IntoIter = T>, | |
{ | |
Self { | |
state: IterState::New, | |
source: source.into_iter(), | |
} | |
} | |
} | |
enum IterState { | |
New, | |
Running { first: u8, current: u8 }, | |
Complete, | |
} | |
impl<T: Iterator<Item = u8>> Iterator for CaptchaIter<T> { | |
type Item = (u8, u8); | |
fn next(&mut self) -> Option<Self::Item> { | |
match self.state { | |
IterState::New => { | |
let first = self.source.next()?; | |
let current = self.source.next()?; | |
self.state = IterState::Running { first, current }; | |
Some((first, current)) | |
} | |
IterState::Running { first, current } => { | |
match self.source.next() { | |
None => { | |
self.state = IterState::Complete; | |
Some((first, current)) | |
} | |
Some(n) => { | |
self.state = IterState::Running { first, current: n }; | |
Some((current, n)) | |
} | |
} | |
} | |
IterState::Complete => None, | |
} | |
} | |
} |
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
#![feature(test)] | |
extern crate test; | |
mod iter; | |
use iter::CaptchaIter; | |
static INPUT: &str = include_str!("../input.txt"); | |
fn main() { | |
println!("{}", sum_captcha(INPUT)); | |
} | |
fn sum_captcha(s: &str) -> u32 { | |
fn eval(u: u8) -> u8 { | |
u - b'0' | |
} | |
CaptchaIter::new(s.bytes().map(eval)) | |
.filter(|pair| pair.0 == pair.1) | |
.map(|pair| pair.0 as u32) | |
.sum() | |
} | |
#[cfg(test)] | |
mod benchmarks { | |
use super::*; | |
#[bench] | |
fn captcha_iter(b: &mut test::Bencher) { | |
b.iter(|| test::black_box(sum_captcha(INPUT))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment