Refactoring program to better multiple puzzles
Moved the logic for computing sonar depth increases out of the main function into its own module. Made the window size adjustable, with a default size of 1, so the same program can answer both questions from the Day 1 puzzle.master
parent
c378851aed
commit
40f77c94ac
|
@ -6,11 +6,91 @@ version = 3
|
|||
name = "aoc2021"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"argh",
|
||||
"bounded-vec-deque",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f023c76cd7975f9969f8e29f0e461decbdc7f51048ce43427107a3d192f1c9bf"
|
||||
dependencies = [
|
||||
"argh_derive",
|
||||
"argh_shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh_derive"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48ad219abc0c06ca788aface2e3a1970587e3413ab70acd20e54b6ec524c1f8f"
|
||||
dependencies = [
|
||||
"argh_shared",
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "argh_shared"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38de00daab4eac7d753e97697066238d67ce9d7e2d823ab4f72fe14af29f3f33"
|
||||
|
||||
[[package]]
|
||||
name = "bounded-vec-deque"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2225b558afc76c596898f5f1b3fc35cfce0eb1b13635cbd7d1b2a7177dc10ccd"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
|
|
@ -7,3 +7,4 @@ edition = "2018"
|
|||
|
||||
[dependencies]
|
||||
bounded-vec-deque = "0.1.1"
|
||||
argh = "^0.1.6"
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
max_width = 79
|
41
src/main.rs
41
src/main.rs
|
@ -1,33 +1,24 @@
|
|||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
|
||||
use bounded_vec_deque::BoundedVecDeque;
|
||||
use argh::FromArgs;
|
||||
|
||||
mod sonar;
|
||||
|
||||
#[derive(FromArgs)]
|
||||
/// Elvish Submarine Sonar Tracker
|
||||
struct Arguments {
|
||||
/// path to input data file
|
||||
#[argh(positional)]
|
||||
input: String,
|
||||
|
||||
/// sliding window size
|
||||
#[argh(option, default = "1")]
|
||||
window: usize,
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let f = File::open(&args[1])?;
|
||||
let buf = io::BufReader::new(f);
|
||||
let mut deque: BoundedVecDeque<i32> = BoundedVecDeque::with_capacity(3, 3);
|
||||
let mut last = -1;
|
||||
let mut count_incr = 0;
|
||||
for line in buf.lines() {
|
||||
if let Ok(l) = line {
|
||||
if let Ok(value) = i32::from_str_radix(&l, 10) {
|
||||
deque.push_back(value);
|
||||
}
|
||||
if deque.is_full() {
|
||||
eprintln!("{:?}", deque);
|
||||
let sum = deque.iter().sum();
|
||||
if last > -1 && sum > last {
|
||||
count_incr += 1;
|
||||
}
|
||||
last = sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
let args: Arguments = argh::from_env();
|
||||
let count_incr = sonar::count_increases(&args.input, args.window)?;
|
||||
println!("number of increases: {}", count_incr);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
|
||||
use bounded_vec_deque::BoundedVecDeque;
|
||||
|
||||
pub fn count_increases(filename: &str, window: usize) -> io::Result<i32> {
|
||||
let f = File::open(filename)?;
|
||||
let buf = io::BufReader::new(f);
|
||||
let mut deque: BoundedVecDeque<i32> =
|
||||
BoundedVecDeque::with_capacity(window, window);
|
||||
let mut last = -1;
|
||||
let mut count_incr = 0;
|
||||
for line in buf.lines() {
|
||||
if let Ok(l) = line {
|
||||
if let Ok(value) = i32::from_str_radix(&l, 10) {
|
||||
deque.push_back(value);
|
||||
}
|
||||
if deque.is_full() {
|
||||
let sum = deque.iter().sum();
|
||||
if last > -1 && sum > last {
|
||||
count_incr += 1;
|
||||
}
|
||||
last = sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(count_incr)
|
||||
}
|
Reference in New Issue