mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 05:10:32 +01:00
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:
63
2016/src/common.rs
Normal file
63
2016/src/common.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user