Use anyhow to make try operator feasible in parse and part#.
This commit is contained in:
parent
11c5dcaaaf
commit
11744d1fe9
@ -73,36 +73,34 @@
|
|||||||
//!
|
//!
|
||||||
//! Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?
|
//! Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
|
||||||
/// Reads text file containing one integer per line, and parses them into `Vec<u32>`. Any
|
/// Reads text file containing one integer per line, and parses them into `Vec<u32>`.
|
||||||
/// non-number will result in a panice.
|
|
||||||
#[aoc_generator(day1)]
|
#[aoc_generator(day1)]
|
||||||
fn parse(input: &str) -> Vec<u32> {
|
fn parse(input: &str) -> Result<Vec<u32>> {
|
||||||
input
|
input.split("\n").map(|s| Ok(s.parse()?)).collect()
|
||||||
.split("\n")
|
|
||||||
.map(|s| s.parse().expect("not a number"))
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day1, part1)]
|
#[aoc(day1, part1)]
|
||||||
fn part1(depths: &[u32]) -> u32 {
|
fn part1(depths: &[u32]) -> Result<u32> {
|
||||||
depths
|
Ok(depths
|
||||||
.windows(2)
|
.windows(2)
|
||||||
.map(|s| if s[0] < s[1] { 1 } else { 0 })
|
.map(|s| if s[0] < s[1] { 1 } else { 0 })
|
||||||
.sum()
|
.sum())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day1, part2)]
|
#[aoc(day1, part2)]
|
||||||
fn part2(depths: &[u32]) -> u32 {
|
fn part2(depths: &[u32]) -> Result<u32> {
|
||||||
let sums: Vec<u32> = depths.windows(3).map(|s| s.iter().sum()).collect();
|
let sums: Vec<u32> = depths.windows(3).map(|s| s.iter().sum()).collect();
|
||||||
sums.windows(2)
|
Ok(sums
|
||||||
|
.windows(2)
|
||||||
.map(|s| if s[0] < s[1] { 1 } else { 0 })
|
.map(|s| if s[0] < s[1] { 1 } else { 0 })
|
||||||
.sum()
|
.sum())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part1() {
|
fn test_part1() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
part1(&parse(
|
part1(&parse(
|
||||||
r#"199
|
r#"199
|
||||||
@ -115,13 +113,14 @@ fn test_part1() {
|
|||||||
269
|
269
|
||||||
260
|
260
|
||||||
263"#
|
263"#
|
||||||
)),
|
)?)?,
|
||||||
7
|
7
|
||||||
);
|
);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part2() {
|
fn test_part2() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
part2(&parse(
|
part2(&parse(
|
||||||
r#"199
|
r#"199
|
||||||
@ -134,7 +133,8 @@ fn test_part2() {
|
|||||||
269
|
269
|
||||||
260
|
260
|
||||||
263"#
|
263"#
|
||||||
)),
|
)?)?,
|
||||||
5
|
5
|
||||||
);
|
);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,36 +1,42 @@
|
|||||||
|
use anyhow::Result;
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
|
||||||
#[aoc_generator(dayX)]
|
#[aoc_generator(dayX)]
|
||||||
fn parse(input: &str) -> Vec<u32> {
|
fn parse(input: &str) -> Result<Vec<u32>> {
|
||||||
todo!("parse")
|
todo!("parse");
|
||||||
|
Ok(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(dayX, part1)]
|
#[aoc(dayX, part1)]
|
||||||
fn part1(depths: &[u32]) -> u32 {
|
fn part1(depths: &[u32]) -> Result<u32> {
|
||||||
todo!("part1")
|
todo!("part1");
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#[aoc(dayX, part2)]
|
#[aoc(dayX, part2)]
|
||||||
fn part2(depths: &[u32]) -> u32 {
|
fn part2(depths: &[u32]) -> Result<u32> {
|
||||||
todo!("part2")
|
todo!("part2")
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part1() {
|
fn test_part1() -> Result<()> {
|
||||||
let input = r#"
|
let input = r#"
|
||||||
"#
|
"#
|
||||||
.trim();
|
.trim();
|
||||||
assert_eq!(part1(&parse(input)), TODO);
|
assert_eq!(part1(&parse(input)?)?, TODO);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part2() {
|
fn test_part2()->Result<()> {
|
||||||
let input = r#"
|
let input = r#"
|
||||||
"#
|
"#
|
||||||
.trim();
|
.trim();
|
||||||
assert_eq!(part2(&parse(input)), TODO);
|
assert_eq!(part2(&parse(input)?)?, TODO);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user