summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-12-01 22:53:14 -0500
committerJesse Luehrs <doy@tozt.net>2020-12-01 22:53:14 -0500
commitb87a72594515f4a05aee7622db96269f2ee9c0af (patch)
tree02ad12632e4cafdbfd82f221f9a8350300ee4f43
parent2ce3804c08f8b49d8a921c07f02dba68df047e66 (diff)
downloadadvent-of-code-b87a72594515f4a05aee7622db96269f2ee9c0af.tar.gz
advent-of-code-b87a72594515f4a05aee7622db96269f2ee9c0af.zip
refactor
-rw-r--r--src/2020/1/mod.rs27
-rw-r--r--src/2020/mod.rs51
-rw-r--r--src/main.rs1
-rw-r--r--src/util.rs17
4 files changed, 49 insertions, 47 deletions
diff --git a/src/2020/1/mod.rs b/src/2020/1/mod.rs
new file mode 100644
index 0000000..8aa5290
--- /dev/null
+++ b/src/2020/1/mod.rs
@@ -0,0 +1,27 @@
+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(());
+ }
+ }
+ }
+ Err(anyhow::anyhow!("no numbers summing to 2020 found"))
+}
+
+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(());
+ }
+ }
+ }
+ }
+ Err(anyhow::anyhow!("no numbers summing to 2020 found"))
+}
diff --git a/src/2020/mod.rs b/src/2020/mod.rs
index 27e56fc..3800c30 100644
--- a/src/2020/mod.rs
+++ b/src/2020/mod.rs
@@ -1,53 +1,10 @@
-use anyhow::Context as _;
-use std::io::BufRead as _;
+#[path = "1/mod.rs"]
+mod day1;
pub fn run(day: u8, puzzle: u8) -> anyhow::Result<()> {
match (day, puzzle) {
- (1, 1) => report_repair(),
- (1, 2) => report_repair_2(),
+ (1, 1) => day1::part1(),
+ (1, 2) => day1::part2(),
_ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)),
}
}
-
-fn report_repair() -> anyhow::Result<()> {
- let ints = read_ints("data/1.txt")?;
- for i in &ints {
- for j in &ints {
- if i + j == 2020 {
- println!("{} * {} = {}", i, j, i * j);
- return Ok(());
- }
- }
- }
- Err(anyhow::anyhow!("no numbers summing to 2020 found"))
-}
-
-fn report_repair_2() -> anyhow::Result<()> {
- let ints = 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(());
- }
- }
- }
- }
- Err(anyhow::anyhow!("no numbers summing to 2020 found"))
-}
-
-fn read_ints(filename: &str) -> anyhow::Result<Vec<i32>> {
- 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
- .lines()
- .map(|l| {
- l.context("failed to read a line")?
- .parse()
- .context("failed to parse line into an integer")
- })
- .collect();
- ints
-}
diff --git a/src/main.rs b/src/main.rs
index b8677cb..e8619fe 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+mod util;
#[path = "2020/mod.rs"]
mod year2020;
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..fdc3bed
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,17 @@
+use anyhow::Context as _;
+use std::io::BufRead as _;
+
+pub fn read_ints(filename: &str) -> anyhow::Result<Vec<i32>> {
+ 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
+ .lines()
+ .map(|l| {
+ l.context("failed to read a line")?
+ .parse()
+ .context("failed to parse line into an integer")
+ })
+ .collect();
+ ints
+}