Rewrite old code into 2018 style.

Nice test for my 2018 repo set up. Which turns out needs some tweaking.
This commit is contained in:
2018-11-12 13:13:03 +01:00
parent 71eb13f52d
commit 9aff05d296
8 changed files with 249 additions and 82 deletions

63
2016/src/common.rs Normal file
View File

@@ -0,0 +1,63 @@
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: &mut io::Read) -> String;
/// Solve the second part of the day
fn part2(&mut self, input: &mut io::Read) -> String;
}
#[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);
}
}