clean up project
- modularized code into individual day modules - moved utilities into utils module - added unit testing
This commit is contained in:
3 files changed
+49
-29
No files matched your search
@@ -0,0 +1,33 @@
|
||||
use crate::utils;
|
||||
use anyhow::Result;
|
||||
|
||||
fn count_incrementing_windows(window: usize, v: &[i64]) -> usize {
|
||||
v[window..].iter().zip(v).filter(|(b, a)| b > a).count()
|
||||
}
|
||||
|
||||
pub(crate) fn run() -> Result<(usize, usize)> {
|
||||
let data = utils::read_i64s("data/day01.txt")?;
|
||||
Ok((
|
||||
count_incrementing_windows(1, &data),
|
||||
count_incrementing_windows(3, &data),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_example() {
|
||||
let data = utils::read_i64s("data/day01-test.txt").unwrap();
|
||||
assert_eq!(count_incrementing_windows(1, &data), 7);
|
||||
assert_eq!(count_incrementing_windows(3, &data), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_my_data() {
|
||||
let data = utils::read_i64s("data/day01.txt").unwrap();
|
||||
assert_eq!(count_incrementing_windows(1, &data), 1298);
|
||||
assert_eq!(count_incrementing_windows(3, &data), 1248);
|
||||
}
|
||||
}
|
||||
+3
-29
@@ -1,35 +1,9 @@
|
||||
use anyhow::Result;
|
||||
use std::fs;
|
||||
|
||||
fn read_i64s(filename: &str) -> Result<Vec<i64>> {
|
||||
Ok(read_file(filename)?
|
||||
.lines()
|
||||
.map(|s| s.parse::<i64>().expect("i64 parse error"))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn read_file(filename: &str) -> Result<String> {
|
||||
Ok(fs::read_to_string(filename)?)
|
||||
}
|
||||
|
||||
fn count_incrementing_windows(window: usize, v: &[i64]) -> usize {
|
||||
v[window..].iter().zip(v).filter(|(b, a)| b > a).count()
|
||||
}
|
||||
|
||||
fn day01() -> Result<(usize, usize)> {
|
||||
let data = read_i64s("data/day01-test.txt")?;
|
||||
assert_eq!(count_incrementing_windows(1, &data), 7);
|
||||
assert_eq!(count_incrementing_windows(3, &data), 5);
|
||||
|
||||
let data = read_i64s("data/day01.txt")?;
|
||||
let part1 = count_incrementing_windows(1, &data);
|
||||
let part2 = count_incrementing_windows(3, &data);
|
||||
assert_eq!(part1, 1298);
|
||||
assert_eq!(part2, 1248);
|
||||
Ok((part1, part2))
|
||||
}
|
||||
mod day01;
|
||||
mod utils;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("day01: {:?}", day01()?);
|
||||
println!("day01: {:?}", day01::run()?);
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
use anyhow::Result;
|
||||
use std::fs;
|
||||
|
||||
pub fn read_i64s(filename: &str) -> Result<Vec<i64>> {
|
||||
Ok(read_file(filename)?
|
||||
.lines()
|
||||
.map(|s| s.parse::<i64>().expect("i64 parse error"))
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn read_file(filename: &str) -> Result<String> {
|
||||
Ok(fs::read_to_string(filename)?)
|
||||
}
|
||||
Reference in new issue
Block a user