Files
adventofcode/2018/src/common.rs
2018-11-09 16:11:08 +01:00

64 lines
1.3 KiB
Rust

use std::io;
/// Apply Erathostenes's sieve to the supplied array
///
/// # Arguments
///
/// * `dest` - the destination slice to fill with the sieve. This is
/// assumed to be filled with "true" before being handed to this
/// method.
pub fn prime_sieve(dest: &mut[bool]) {
if dest.len() >= 1 {
dest[0] = false;
}
if dest.len() >= 2 {
dest[1] = false;
}
let limit = (dest.len() as f64).sqrt() as usize;
for i in 1..(limit + 1) {
if !dest[i] {
continue
}
for j in ((i * i)..(dest.len())).step_by(i) {
dest[j] = false;
}
}
}
/// Solution trait
///
/// Every day's solution should implement this function so that it can
/// be easily run from the main program.
pub trait Solution {
/// Solve the first part of the day
fn part1(&mut self, input: Box<io::Read>);
/// Solve the second part of the day
fn part2(&mut self, input: Box<io::Read>);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_prime_sieve() {
let mut input = [true; 10];
prime_sieve(&mut input);
let output = [
false, false,
true, true,
false, true,
false, true,
false, false
];
assert_eq!(output, input);
}
}