summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-12-04 16:10:18 -0500
committerJesse Luehrs <doy@tozt.net>2020-12-04 16:10:18 -0500
commit7543903709a1c7e56be6260befb117baea833c0a (patch)
treef27178335b2e7cebc33f7d7c3da383e55e02f233
parent29b1679b024f4f56d43b1646d6fa5d301994f6f4 (diff)
downloadadvent-of-code-7543903709a1c7e56be6260befb117baea833c0a.tar.gz
advent-of-code-7543903709a1c7e56be6260befb117baea833c0a.zip
refactor
-rw-r--r--src/2020/1/mod.rs10
-rw-r--r--src/2020/2/mod.rs11
-rw-r--r--src/2020/3/mod.rs23
-rw-r--r--src/2020/4/mod.rs10
-rw-r--r--src/2020/mod.rs2
-rw-r--r--src/main.rs4
-rw-r--r--src/util.rs4
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<i64> {
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<i64> {
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<i64> {
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<i64> {
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<Vec<Line>> {
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<usize> {
+ ) -> anyhow::Result<i64> {
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<i64> {
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<i64> {
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<Map> {
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<i64> {
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<i64> {
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<i64> {
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<Vec<i32>> {
+pub fn read_ints(filename: &str) -> anyhow::Result<Vec<i64>> {
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<Vec<i32>> = f
+ let ints: anyhow::Result<Vec<_>> = f
.lines()
.map(|l| {
l.context("failed to read a line")?