From 7543903709a1c7e56be6260befb117baea833c0a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 4 Dec 2020 16:10:18 -0500 Subject: refactor --- src/2020/1/mod.rs | 10 ++++------ src/2020/2/mod.rs | 11 +++++------ src/2020/3/mod.rs | 23 +++++++++-------------- src/2020/4/mod.rs | 10 ++++------ src/2020/mod.rs | 2 +- src/main.rs | 4 +++- src/util.rs | 4 ++-- 7 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/2020/1/mod.rs b/src/2020/1/mod.rs index 8aa5290..1c18cad 100644 --- a/src/2020/1/mod.rs +++ b/src/2020/1/mod.rs @@ -1,24 +1,22 @@ -pub fn part1() -> anyhow::Result<()> { +pub fn part1() -> anyhow::Result { let ints = crate::util::read_ints("data/1.txt")?; for i in &ints { for j in &ints { if i + j == 2020 { - println!("{} * {} = {}", i, j, i * j); - return Ok(()); + return Ok(i * j); } } } Err(anyhow::anyhow!("no numbers summing to 2020 found")) } -pub fn part2() -> anyhow::Result<()> { +pub fn part2() -> anyhow::Result { let ints = crate::util::read_ints("data/1.txt")?; for i in &ints { for j in &ints { for k in &ints { if i + j + k == 2020 { - println!("{} * {} * {} = {}", i, j, k, i * j * k); - return Ok(()); + return Ok(i * j * k); } } } diff --git a/src/2020/2/mod.rs b/src/2020/2/mod.rs index a81665b..1026b03 100644 --- a/src/2020/2/mod.rs +++ b/src/2020/2/mod.rs @@ -1,4 +1,5 @@ use anyhow::Context as _; +use std::convert::TryInto as _; use std::io::BufRead as _; struct Line { @@ -51,18 +52,16 @@ impl Line { } } -pub fn part1() -> anyhow::Result<()> { +pub fn part1() -> anyhow::Result { let lines = read_lines()?; let count = lines.iter().filter(|l| l.valid_part_1()).count(); - println!("{}", count); - Ok(()) + Ok(count.try_into()?) } -pub fn part2() -> anyhow::Result<()> { +pub fn part2() -> anyhow::Result { let lines = read_lines()?; let count = lines.iter().filter(|l| l.valid_part_2()).count(); - println!("{}", count); - Ok(()) + Ok(count.try_into()?) } fn read_lines() -> anyhow::Result> { diff --git a/src/2020/3/mod.rs b/src/2020/3/mod.rs index 825e31c..81d095a 100644 --- a/src/2020/3/mod.rs +++ b/src/2020/3/mod.rs @@ -51,7 +51,7 @@ impl Map { &self, x_incr: usize, y_incr: usize, - ) -> anyhow::Result { + ) -> anyhow::Result { let mut trees = 0; for r in 0..self.rows() / y_incr { let x = r * x_incr; @@ -64,23 +64,18 @@ impl Map { } } -pub fn part1() -> anyhow::Result<()> { +pub fn part1() -> anyhow::Result { let map = read_map()?; - println!("{}", map.trees_for_slope(3, 1)?); - Ok(()) + Ok(map.trees_for_slope(3, 1)?) } -pub fn part2() -> anyhow::Result<()> { +pub fn part2() -> anyhow::Result { let map = read_map()?; - println!( - "{}", - map.trees_for_slope(1, 1)? - * map.trees_for_slope(3, 1)? - * map.trees_for_slope(5, 1)? - * map.trees_for_slope(7, 1)? - * map.trees_for_slope(1, 2)? - ); - Ok(()) + Ok(map.trees_for_slope(1, 1)? + * map.trees_for_slope(3, 1)? + * map.trees_for_slope(5, 1)? + * map.trees_for_slope(7, 1)? + * map.trees_for_slope(1, 2)?) } fn read_map() -> anyhow::Result { diff --git a/src/2020/4/mod.rs b/src/2020/4/mod.rs index 4b51b29..076a524 100644 --- a/src/2020/4/mod.rs +++ b/src/2020/4/mod.rs @@ -3,7 +3,7 @@ use anyhow::Context as _; const REQUIRED_KEYS: &[&str] = &["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]; -pub fn part1() -> anyhow::Result<()> { +pub fn part1() -> anyhow::Result { let batch = crate::util::read_file_str("data/4.txt")?; let mut valid = 0; for passport in parse(&batch)? { @@ -18,11 +18,10 @@ pub fn part1() -> anyhow::Result<()> { valid += 1; } } - println!("{}", valid); - Ok(()) + Ok(valid) } -pub fn part2() -> anyhow::Result<()> { +pub fn part2() -> anyhow::Result { let batch = crate::util::read_file_str("data/4.txt")?; let mut valid = 0; for passport in parse(&batch)? { @@ -45,8 +44,7 @@ pub fn part2() -> anyhow::Result<()> { valid += 1; } } - println!("{}", valid); - Ok(()) + Ok(valid) } fn parse( diff --git a/src/2020/mod.rs b/src/2020/mod.rs index 812a379..8ef019e 100644 --- a/src/2020/mod.rs +++ b/src/2020/mod.rs @@ -7,7 +7,7 @@ mod day3; #[path = "4/mod.rs"] mod day4; -pub fn run(day: u8, puzzle: u8) -> anyhow::Result<()> { +pub fn run(day: u8, puzzle: u8) -> anyhow::Result { match (day, puzzle) { (1, 1) => day1::part1(), (1, 2) => day1::part2(), diff --git a/src/main.rs b/src/main.rs index e8619fe..af137fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,9 @@ fn main(opt: Opt) { Opt::Year2020 { day, puzzle } => crate::year2020::run(day, puzzle), }; match res { - Ok(()) => {} + Ok(answer) => { + println!("{}", answer); + } Err(e) => { eprintln!("{}", e); std::process::exit(1); diff --git a/src/util.rs b/src/util.rs index 6078b5d..6a62708 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,11 @@ use anyhow::Context as _; use std::io::{BufRead as _, Read as _}; -pub fn read_ints(filename: &str) -> anyhow::Result> { +pub fn read_ints(filename: &str) -> anyhow::Result> { let f = std::fs::File::open(filename) .with_context(|| format!("couldn't find data file {}", filename))?; let f = std::io::BufReader::new(f); - let ints: anyhow::Result> = f + let ints: anyhow::Result> = f .lines() .map(|l| { l.context("failed to read a line")? -- cgit v1.2.3