summaryrefslogtreecommitdiffstats
path: root/src/2020
diff options
context:
space:
mode:
Diffstat (limited to 'src/2020')
-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
5 files changed, 23 insertions, 33 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(),