Skip to content

Commit

Permalink
Improve overall structure, add testsupport and build solver as lib
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Lundgren <[email protected]>
  • Loading branch information
panord committed Dec 4, 2021
1 parent efb12cf commit 0505191
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 54 deletions.
5 changes: 5 additions & 0 deletions 2021/aoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ edition = "2021"
clap = "2.34.0"
anyhow = "1.0.51"
either = "1.6.1"


[lib]
name = "aoc"
path = "src/lib.rs"
58 changes: 58 additions & 0 deletions 2021/aoc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::io::{BufRead, BufReader};

pub fn from_lines<X, Y, Z>(input: X) -> Z
where
X: std::io::Read,
Y: std::str::FromStr,
<Y as std::str::FromStr>::Err: std::fmt::Debug,
Z: std::iter::FromIterator<Y>,
{
BufReader::new(input)
.lines()
.into_iter()
.map(|r| r.and_then(|r| Ok(r.parse::<Y>().unwrap())).unwrap())
.collect()
}

pub fn do_sonar_sweep(vals: &mut Vec<i64>, n: usize) -> usize {
let mut last: i64 = std::i64::MAX;
let mut cnt: usize = 0;

for i in 0..vals.len() - (n - 1) {
for j in 1..n {
vals[i] += vals[i + j];
}
}

for v in 0..vals.len() - (n - 1) {
if vals[v] > last {
cnt += 1;
}
last = vals[v];
}
return cnt;
}

pub fn sonar_sweep<X, Y>(input: X, out: &mut Y, n: usize)
where
X: std::io::Read,
Y: std::io::Write,
{
let mut vals: Vec<i64> = from_lines(input);
let cnt = do_sonar_sweep(&mut vals, n);

out.write(format!("Growing: {}\n", cnt).as_ref())
.expect("Failed to write result");
}

pub fn do_solve<X, Y>(assign: u8, input: X, output: &mut Y)
where
X: std::io::Read,
Y: std::io::Write,
{
match assign {
1 => sonar_sweep(input, output, 1),
2 => sonar_sweep(input, output, 3),
_ => println!("Unsupported assignment {}", assign),
};
}
62 changes: 8 additions & 54 deletions 2021/aoc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,14 @@
use anyhow::{Context, Result};
use anyhow::Context;
use clap::{value_t, App, Arg, ArgMatches};
use either::{Left, Right};
use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader};
use std::io::{stdin, stdout};
use String;

fn from_lines<X, Y, Z>(input: X) -> Z
where
X: std::io::Read,
Y: std::str::FromStr,
<Y as std::str::FromStr>::Err: std::fmt::Debug,
Z: std::iter::FromIterator<Y>,
{
BufReader::new(input)
.lines()
.into_iter()
.map(|r| r.and_then(|r| Ok(r.parse::<Y>().unwrap())).unwrap())
.collect()
}

fn sonar_sweep<X, Y>(input: X, out: &mut Y, n: usize)
where
X: std::io::Read,
Y: std::io::Write,
{
let mut last: i64 = std::i64::MAX;
let mut cnt: u64 = 0;
let mut vals: Vec<i64> = from_lines(input);

for i in 0..vals.len() - (n - 1) {
for j in 1..n {
vals[i] += vals[i + j];
}
}

for v in 0..vals.len() - (n - 1) {
if vals[v] > last {
cnt += 1;
} else {
println!("{}: {} < {}", v, vals[v], last);
}
last = vals[v];
}

out.write(format!("Growing: {}\n", cnt).as_ref())
.expect("Failed to write result");
}

fn solve(args: &ArgMatches) -> Result<()> {
let a =
value_t!(args.value_of("assignment"), u32).context("Could not parse assignment number")?;
pub fn solve(args: &ArgMatches) {
let a = value_t!(args.value_of("assignment"), u8)
.context("Could not parse assignment number")
.unwrap();

// crate for fileopen or stdout/stdin
let input = value_t!(args.value_of("file"), String)
Expand All @@ -64,12 +23,7 @@ fn solve(args: &ArgMatches) -> Result<()> {
.and_then(|f| Ok(Right(std::io::BufWriter::new(f))))
.unwrap_or(Left(stdout()));

match a {
1 => sonar_sweep(input, &mut output, 1),
2 => sonar_sweep(input, &mut output, 3),
_ => println!("Unsupported assignment {}", a),
};
Ok(())
aoc::do_solve(a, input, &mut output);
}

fn main() {
Expand Down Expand Up @@ -105,5 +59,5 @@ fn main() {
.context("Failed to parse args")
.unwrap();

solve(&matches).unwrap();
solve(&matches);
}
7 changes: 7 additions & 0 deletions 2021/aoc/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[test]
fn sonar_sweep() {
let mut t = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];

assert_eq!(aoc::do_sonar_sweep(&mut t, 1), 7);
assert_eq!(aoc::do_sonar_sweep(&mut t, 3), 5);
}

0 comments on commit 0505191

Please sign in to comment.