summaryrefslogtreecommitdiffstats
path: root/src/2021/1/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/2021/1/mod.rs')
-rw-r--r--src/2021/1/mod.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/2021/1/mod.rs b/src/2021/1/mod.rs
index be03275..3ba61e2 100644
--- a/src/2021/1/mod.rs
+++ b/src/2021/1/mod.rs
@@ -1,6 +1,9 @@
-pub fn part1() -> anyhow::Result<i64> {
- Ok(data_ints!()?
- .collect::<Vec<_>>()
+pub fn parse(fh: std::fs::File) -> anyhow::Result<Vec<i64>> {
+ Ok(crate::util::parse::ints(crate::util::parse::lines(fh)).collect())
+}
+
+pub fn part1(ints: Vec<i64>) -> anyhow::Result<i64> {
+ Ok(ints
.windows(2)
.map(|a| a[1] - a[0])
.filter(|x| *x > 0)
@@ -8,9 +11,8 @@ pub fn part1() -> anyhow::Result<i64> {
.try_into()?)
}
-pub fn part2() -> anyhow::Result<i64> {
- Ok(data_ints!()?
- .collect::<Vec<_>>()
+pub fn part2(ints: Vec<i64>) -> anyhow::Result<i64> {
+ Ok(ints
.windows(3)
.map(|a| a[0] + a[1] + a[2])
.collect::<Vec<_>>()
@@ -23,6 +25,12 @@ pub fn part2() -> anyhow::Result<i64> {
#[test]
fn test() {
- assert_eq!(part1().unwrap(), 1602);
- assert_eq!(part2().unwrap(), 1633);
+ assert_eq!(
+ part1(parse(crate::util::data(2021, 1).unwrap()).unwrap()).unwrap(),
+ 1602
+ );
+ assert_eq!(
+ part2(parse(crate::util::data(2021, 1).unwrap()).unwrap()).unwrap(),
+ 1633
+ );
}