summaryrefslogtreecommitdiffstats
path: root/src/2020/6/mod.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-18 13:21:42 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-18 13:21:42 -0500
commitd16795e44aeac17bee08363bd08c1a9672edf3d4 (patch)
tree3f70ddaf74b10db3f0e38c8a81838607f4e26a2c /src/2020/6/mod.rs
parentd1cacab50a8cab269da867ae900e903648b42cff (diff)
downloadadvent-of-code-d16795e44aeac17bee08363bd08c1a9672edf3d4.tar.gz
advent-of-code-d16795e44aeac17bee08363bd08c1a9672edf3d4.zip
factor out parsing
Diffstat (limited to 'src/2020/6/mod.rs')
-rw-r--r--src/2020/6/mod.rs26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/2020/6/mod.rs b/src/2020/6/mod.rs
index 5e918d7..21279af 100644
--- a/src/2020/6/mod.rs
+++ b/src/2020/6/mod.rs
@@ -1,8 +1,13 @@
-pub fn part1() -> anyhow::Result<i64> {
- let input = data_str!()?;
+pub fn parse(
+ fh: std::fs::File,
+) -> anyhow::Result<impl Iterator<Item = String>> {
+ Ok(crate::util::parse::lines(fh))
+}
+
+pub fn part1(lines: impl Iterator<Item = String>) -> anyhow::Result<i64> {
let mut yes = std::collections::HashSet::new();
let mut total = 0;
- for line in input.lines() {
+ for line in lines {
if line.is_empty() {
total += yes.len() as i64;
yes = std::collections::HashSet::new();
@@ -16,14 +21,13 @@ pub fn part1() -> anyhow::Result<i64> {
Ok(total)
}
-pub fn part2() -> anyhow::Result<i64> {
- let input = data_str!()?;
+pub fn part2(lines: impl Iterator<Item = String>) -> anyhow::Result<i64> {
let mut yes = std::collections::HashSet::new();
for c in 'a'..='z' {
yes.insert(c);
}
let mut total = 0;
- for line in input.lines() {
+ for line in lines {
if line.is_empty() {
total += yes.len() as i64;
yes = std::collections::HashSet::new();
@@ -44,6 +48,12 @@ pub fn part2() -> anyhow::Result<i64> {
#[test]
fn test() {
- assert_eq!(part1().unwrap(), 6930);
- assert_eq!(part2().unwrap(), 3585);
+ assert_eq!(
+ part1(parse(crate::util::data(2020, 6).unwrap()).unwrap()).unwrap(),
+ 6930
+ );
+ assert_eq!(
+ part2(parse(crate::util::data(2020, 6).unwrap()).unwrap()).unwrap(),
+ 3585
+ );
}