diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-01 17:00:31 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-01 17:00:31 -0500 |
commit | f9573cf0b5de23e258791fbdc424fd3abbc4fdc5 (patch) | |
tree | 9a46aa790605dbe0a7d3d9de9eebe0a0ac1dc6fb | |
parent | a40f68453cf4b59f166d70e769ce8fd6ceeeb831 (diff) | |
download | advent-of-code-f9573cf0b5de23e258791fbdc424fd3abbc4fdc5.tar.gz advent-of-code-f9573cf0b5de23e258791fbdc424fd3abbc4fdc5.zip |
simplify getting data
-rw-r--r-- | src/2020/1/mod.rs | 4 | ||||
-rw-r--r-- | src/2020/2/mod.rs | 3 | ||||
-rw-r--r-- | src/2020/3/mod.rs | 3 | ||||
-rw-r--r-- | src/2020/4/mod.rs | 4 | ||||
-rw-r--r-- | src/2020/5/mod.rs | 4 | ||||
-rw-r--r-- | src/2020/6/mod.rs | 4 | ||||
-rw-r--r-- | src/2020/7/mod.rs | 4 | ||||
-rw-r--r-- | src/2020/8/mod.rs | 4 | ||||
-rw-r--r-- | src/2020/9/mod.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/util.rs | 41 |
11 files changed, 58 insertions, 18 deletions
diff --git a/src/2020/1/mod.rs b/src/2020/1/mod.rs index ec3e1f2..d9f3bf5 100644 --- a/src/2020/1/mod.rs +++ b/src/2020/1/mod.rs @@ -1,5 +1,5 @@ pub fn part1() -> anyhow::Result<i64> { - let ints = crate::util::read_ints("data/2020/1.txt")?; + let ints = data_ints!()?; for i in &ints { for j in &ints { if i + j == 2020 { @@ -11,7 +11,7 @@ pub fn part1() -> anyhow::Result<i64> { } pub fn part2() -> anyhow::Result<i64> { - let ints = crate::util::read_ints("data/2020/1.txt")?; + let ints = data_ints!()?; for i in &ints { for j in &ints { for k in &ints { diff --git a/src/2020/2/mod.rs b/src/2020/2/mod.rs index d2cf88d..0f9af6a 100644 --- a/src/2020/2/mod.rs +++ b/src/2020/2/mod.rs @@ -65,8 +65,7 @@ pub fn part2() -> anyhow::Result<i64> { } fn read_lines() -> anyhow::Result<Vec<Line>> { - let f = std::fs::File::open("data/2020/2.txt") - .context("couldn't find data file 2.txt")?; + let f = data!()?; let f = std::io::BufReader::new(f); f.lines() .map(|l| Line::parse(&l.context("failed to read a line")?)) diff --git a/src/2020/3/mod.rs b/src/2020/3/mod.rs index 6658b64..f101e7c 100644 --- a/src/2020/3/mod.rs +++ b/src/2020/3/mod.rs @@ -79,6 +79,5 @@ pub fn part2() -> anyhow::Result<i64> { } fn read_map() -> anyhow::Result<Map> { - let map_str = crate::util::read_file("data/2020/3.txt")?; - Map::parse(&map_str) + Map::parse(&data_bytes!()?) } diff --git a/src/2020/4/mod.rs b/src/2020/4/mod.rs index 827e648..6a9f00e 100644 --- a/src/2020/4/mod.rs +++ b/src/2020/4/mod.rs @@ -4,7 +4,7 @@ const REQUIRED_KEYS: &[&str] = &["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]; pub fn part1() -> anyhow::Result<i64> { - let batch = crate::util::read_file_str("data/2020/4.txt")?; + let batch = data_str!()?; let mut valid = 0; for passport in parse(&batch)? { let mut cur_valid = true; @@ -22,7 +22,7 @@ pub fn part1() -> anyhow::Result<i64> { } pub fn part2() -> anyhow::Result<i64> { - let batch = crate::util::read_file_str("data/2020/4.txt")?; + let batch = data_str!()?; let mut valid = 0; for passport in parse(&batch)? { let mut cur_valid = true; diff --git a/src/2020/5/mod.rs b/src/2020/5/mod.rs index e574a45..1fa1147 100644 --- a/src/2020/5/mod.rs +++ b/src/2020/5/mod.rs @@ -2,7 +2,7 @@ use anyhow::Context as _; use std::convert::TryInto as _; pub fn part1() -> anyhow::Result<i64> { - let input = crate::util::read_file_str("data/2020/5.txt")?; + let input = data_str!()?; let mut max = 0; for line in input.lines() { let id = seat_id(line)?; @@ -15,7 +15,7 @@ pub fn part1() -> anyhow::Result<i64> { pub fn part2() -> anyhow::Result<i64> { let mut seats = vec![false; 1024]; - let input = crate::util::read_file_str("data/2020/5.txt")?; + let input = data_str!()?; for line in input.lines() { let id = seat_id(line)?; seats[id as usize] = true; diff --git a/src/2020/6/mod.rs b/src/2020/6/mod.rs index 9d22667..bd79742 100644 --- a/src/2020/6/mod.rs +++ b/src/2020/6/mod.rs @@ -1,5 +1,5 @@ pub fn part1() -> anyhow::Result<i64> { - let input = crate::util::read_file_str("data/2020/6.txt")?; + let input = data_str!()?; let mut yes = std::collections::HashSet::new(); let mut total = 0; for line in input.lines() { @@ -17,7 +17,7 @@ pub fn part1() -> anyhow::Result<i64> { } pub fn part2() -> anyhow::Result<i64> { - let input = crate::util::read_file_str("data/2020/6.txt")?; + let input = data_str!()?; let mut yes = std::collections::HashSet::new(); for c in 'a'..='z' { yes.insert(c); diff --git a/src/2020/7/mod.rs b/src/2020/7/mod.rs index 26fa507..237fadf 100644 --- a/src/2020/7/mod.rs +++ b/src/2020/7/mod.rs @@ -3,7 +3,7 @@ use anyhow::Context as _; type Graph = std::collections::HashMap<String, Vec<(i64, String)>>; pub fn part1() -> anyhow::Result<i64> { - let input = crate::util::read_file_str("data/2020/7.txt")?; + let input = data_str!()?; let graph = parse(&input)?; let mut colors = 0; for color in graph.keys() { @@ -15,7 +15,7 @@ pub fn part1() -> anyhow::Result<i64> { } pub fn part2() -> anyhow::Result<i64> { - let input = crate::util::read_file_str("data/2020/7.txt")?; + let input = data_str!()?; let graph = parse(&input)?; // subtract 1 to not count the shiny gold bag itself count_bags(&graph, "shiny gold").map(|i| i - 1) diff --git a/src/2020/8/mod.rs b/src/2020/8/mod.rs index c5bf835..87a24e8 100644 --- a/src/2020/8/mod.rs +++ b/src/2020/8/mod.rs @@ -44,7 +44,7 @@ impl std::str::FromStr for Op { } pub fn part1() -> anyhow::Result<i64> { - let input = crate::util::read_file_str("data/2020/8.txt")?; + let input = data_str!()?; let opcodes = parse(&input)?; let (acc, success) = run(&opcodes)?; if success { @@ -54,7 +54,7 @@ pub fn part1() -> anyhow::Result<i64> { } pub fn part2() -> anyhow::Result<i64> { - let input = crate::util::read_file_str("data/2020/8.txt")?; + let input = data_str!()?; let opcodes = parse(&input)?; for i in 0..opcodes.len() { match opcodes[i].ty { diff --git a/src/2020/9/mod.rs b/src/2020/9/mod.rs index 9684099..aaf61a4 100644 --- a/src/2020/9/mod.rs +++ b/src/2020/9/mod.rs @@ -1,7 +1,7 @@ pub fn part1() -> anyhow::Result<i64> { const WINDOW: usize = 25; - let list = crate::util::read_ints("data/2020/9.txt")?; + let list = data_ints!()?; for i in 0..(list.len() - WINDOW) { let set = &list[i..i + WINDOW]; let n = list[i + WINDOW]; @@ -16,7 +16,7 @@ pub fn part1() -> anyhow::Result<i64> { pub fn part2() -> anyhow::Result<i64> { const WINDOW: usize = 25; - let list = crate::util::read_ints("data/2020/9.txt")?; + let list = data_ints!()?; let mut invalid = None; for i in 0..(list.len() - WINDOW) { let set = &list[i..i + WINDOW]; diff --git a/src/main.rs b/src/main.rs index 3905d9b..963ffe8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![allow(clippy::collapsible_if)] +#[macro_use] mod util; #[path = "2020/mod.rs"] mod year2020; diff --git a/src/util.rs b/src/util.rs index 6a62708..9cc3d6a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,47 @@ use anyhow::Context as _; use std::io::{BufRead as _, Read as _}; +macro_rules! data { + () => {{ + let file = crate::util::src_file_to_data_file(&std::file!()); + std::fs::File::open(file.clone()) + .with_context(|| format!("couldn't find data file {}", file)) + }}; +} + +macro_rules! data_ints { + () => { + crate::util::read_ints(&crate::util::src_file_to_data_file( + &std::file!(), + )) + }; +} + +macro_rules! data_bytes { + () => { + crate::util::read_file(&crate::util::src_file_to_data_file( + &std::file!(), + )) + }; +} + +macro_rules! data_str { + () => { + crate::util::read_file_str(&crate::util::src_file_to_data_file( + &std::file!(), + )) + }; +} + +pub fn src_file_to_data_file(file: &str) -> String { + let parts: Vec<_> = file.split('/').collect(); + format!( + "data/{}/{}.txt", + parts[parts.len() - 3], + parts[parts.len() - 2] + ) +} + 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))?; |