diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-21 04:18:01 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-21 04:18:01 -0500 |
commit | 3a5398399221c7986a69c6cc90f7bb70669f39a8 (patch) | |
tree | ec4b06df956dbc2aab7c4a2d25a47a65fd4ac81e /src/2020/2/mod.rs | |
parent | 5d64e313a46b002205ecfe854555d1e8c94572e0 (diff) | |
download | advent-of-code-3a5398399221c7986a69c6cc90f7bb70669f39a8.tar.gz advent-of-code-3a5398399221c7986a69c6cc90f7bb70669f39a8.zip |
prelude to reduce some typing
Diffstat (limited to 'src/2020/2/mod.rs')
-rw-r--r-- | src/2020/2/mod.rs | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/src/2020/2/mod.rs b/src/2020/2/mod.rs index 31dfeb5..75cc4f1 100644 --- a/src/2020/2/mod.rs +++ b/src/2020/2/mod.rs @@ -1,5 +1,4 @@ -use anyhow::Context as _; -use std::convert::TryInto as _; +use crate::prelude::*; pub struct Line { c: char, @@ -9,8 +8,8 @@ pub struct Line { } impl Line { - fn parse(line: &str) -> anyhow::Result<Self> { - let rx = regex::Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap(); + fn parse(line: &str) -> Result<Self> { + let rx = Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap(); let captures = rx.captures(line).context("line failed to match regex")?; let c = captures @@ -51,18 +50,16 @@ impl Line { } } -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result<impl Iterator<Item = Line>> { - Ok(crate::util::parse::lines(fh).map(|line| Line::parse(&line).unwrap())) +pub fn parse(fh: File) -> Result<impl Iterator<Item = Line>> { + Ok(parse::lines(fh).map(|line| Line::parse(&line).unwrap())) } -pub fn part1(lines: impl Iterator<Item = Line>) -> anyhow::Result<i64> { +pub fn part1(lines: impl Iterator<Item = Line>) -> Result<i64> { let count = lines.filter(|l| l.valid_part_1()).count(); Ok(count.try_into()?) } -pub fn part2(lines: impl Iterator<Item = Line>) -> anyhow::Result<i64> { +pub fn part2(lines: impl Iterator<Item = Line>) -> Result<i64> { let count = lines.filter(|l| l.valid_part_2()).count(); Ok(count.try_into()?) } @@ -70,11 +67,11 @@ pub fn part2(lines: impl Iterator<Item = Line>) -> anyhow::Result<i64> { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 2).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 2).unwrap()).unwrap()).unwrap(), 638 ); assert_eq!( - part2(parse(crate::util::data(2020, 2).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 2).unwrap()).unwrap()).unwrap(), 699 ); } |