[2017][rust][15.x] extract, clean up code

sorbet
Alpha Chen 7 years ago
parent fd07f77699
commit d1924f5742

@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::collections::hash_map::Entry;
use failure::*;
#[allow(dead_code)]
pub fn solve(input: &str) -> Result<String, Error> {
let mut jumps: HashMap<isize, isize> = input
.trim()

@ -0,0 +1,28 @@
use failure::*;
pub fn solve(_: &str) -> Result<String, Error> {
let a = Generator{value: 679, factor: 16807};
let b = Generator{value: 771, factor: 48271};
let answer = a.filter(|a| a % 4 == 0).zip(b.filter(|b| b % 8 == 0))
.take(5_000_000)
.filter(|&(a,b)| a & 0b1111111111111111 == b & 0b1111111111111111)
.count();
Ok(answer.to_string())
}
struct Generator {
value: usize,
factor: usize,
}
impl Iterator for Generator {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
self.value *= self.factor;
self.value %= 2147483647;
Some(self.value)
}
}

@ -13,47 +13,19 @@ mod day_02;
mod day_03;
mod day_04;
mod day_05;
struct Generator {
value: usize,
factor: usize,
}
impl Iterator for Generator {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
self.value *= self.factor;
self.value %= 2147483647;
Some(self.value)
}
}
mod day_15;
fn main() {
let mut a = Generator{value: 679, factor: 16807};
let mut b = Generator{value: 771, factor: 48271};
let answer = a.filter(|a| a % 4 == 0).zip(b.filter(|b| b % 8 == 0))
.take(5_000_000)
.filter(|&(a,b)| a & 0b1111111111111111 == b & 0b1111111111111111)
.count();
println!("{}", answer);
// p a.zip(b).take(40_000_000).with_index.count {|(a,b),i|
// p i if i % 1_000_000 == 0
// a.to_s(2)[-16,16] == b.to_s(2)[-16,16]
// }
// if let Err(e) = run() {
// eprintln!("{}", e);
// }
if let Err(e) = run() {
eprintln!("{}", e);
}
}
fn run() -> Result<(), Error> {
let mut input = String::new();
io::stdin().read_to_string(&mut input)?;
let solution = day_05::solve(&input)?;
let solution = day_15::solve(&input)?;
println!("{}", solution);
Ok(())

Loading…
Cancel
Save