-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve overall structure, add testsupport and build solver as lib
Signed-off-by: Patrik Lundgren <[email protected]>
- Loading branch information
Showing
4 changed files
with
78 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |