summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-16 17:56:27 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-16 17:56:27 -0500
commit2fb401764b5eed391be2006263bba126b76f0499 (patch)
treef5cdf8d2f9d4003d20602d9b5d406ab70522bcf6
parent016954ce5dc4a52d6f138477d82597e14ff7c4b1 (diff)
downloadadvent-of-code-2fb401764b5eed391be2006263bba126b76f0499.tar.gz
advent-of-code-2fb401764b5eed391be2006263bba126b76f0499.zip
simplify data utils a bit
-rw-r--r--src/2020/1/mod.rs4
-rw-r--r--src/2020/3/mod.rs4
-rw-r--r--src/2020/9/mod.rs4
-rw-r--r--src/2021/1/mod.rs2
-rw-r--r--src/2021/10/mod.rs2
-rw-r--r--src/2021/11/mod.rs4
-rw-r--r--src/2021/12/mod.rs2
-rw-r--r--src/2021/13/mod.rs2
-rw-r--r--src/2021/14/mod.rs3
-rw-r--r--src/2021/15/mod.rs8
-rw-r--r--src/2021/16/mod.rs4
-rw-r--r--src/2021/2/mod.rs2
-rw-r--r--src/2021/3/mod.rs12
-rw-r--r--src/2021/5/mod.rs2
-rw-r--r--src/2021/6/mod.rs14
-rw-r--r--src/2021/7/mod.rs16
-rw-r--r--src/2021/8/mod.rs2
-rw-r--r--src/2021/9/mod.rs2
-rw-r--r--src/util.rs89
19 files changed, 63 insertions, 115 deletions
diff --git a/src/2020/1/mod.rs b/src/2020/1/mod.rs
index 40c9c0f..8ac5e14 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 = data_ints!()?;
+ let ints: Vec<_> = data_ints!()?.collect();
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 = data_ints!()?;
+ let ints: Vec<_> = data_ints!()?.collect();
for i in &ints {
for j in &ints {
for k in &ints {
diff --git a/src/2020/3/mod.rs b/src/2020/3/mod.rs
index f80bd43..4676a45 100644
--- a/src/2020/3/mod.rs
+++ b/src/2020/3/mod.rs
@@ -5,7 +5,7 @@ struct Map {
}
impl Map {
- fn parse(s: &[u8]) -> anyhow::Result<Self> {
+ fn parse(s: impl Iterator<Item = u8>) -> anyhow::Result<Self> {
let mut grid = vec![];
let mut current_row = vec![];
for c in s {
@@ -79,7 +79,7 @@ pub fn part2() -> anyhow::Result<i64> {
}
fn read_map() -> anyhow::Result<Map> {
- Map::parse(&data_bytes!()?)
+ Map::parse(data_bytes!()?)
}
#[test]
diff --git a/src/2020/9/mod.rs b/src/2020/9/mod.rs
index 3b4dd36..9d1bc9f 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 = data_ints!()?;
+ let list: Vec<_> = data_ints!()?.collect();
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 = data_ints!()?;
+ let list: Vec<_> = data_ints!()?.collect();
let mut invalid = None;
for i in 0..(list.len() - WINDOW) {
let set = &list[i..i + WINDOW];
diff --git a/src/2021/1/mod.rs b/src/2021/1/mod.rs
index 2d9acad..be03275 100644
--- a/src/2021/1/mod.rs
+++ b/src/2021/1/mod.rs
@@ -1,5 +1,6 @@
pub fn part1() -> anyhow::Result<i64> {
Ok(data_ints!()?
+ .collect::<Vec<_>>()
.windows(2)
.map(|a| a[1] - a[0])
.filter(|x| *x > 0)
@@ -9,6 +10,7 @@ pub fn part1() -> anyhow::Result<i64> {
pub fn part2() -> anyhow::Result<i64> {
Ok(data_ints!()?
+ .collect::<Vec<_>>()
.windows(3)
.map(|a| a[0] + a[1] + a[2])
.collect::<Vec<_>>()
diff --git a/src/2021/10/mod.rs b/src/2021/10/mod.rs
index 552e79c..29cced2 100644
--- a/src/2021/10/mod.rs
+++ b/src/2021/10/mod.rs
@@ -1,7 +1,6 @@
pub fn part1() -> anyhow::Result<i64> {
let mut total = 0;
for line in data_lines!()? {
- let line = line?;
let mut open = vec![];
for c in line.chars() {
match c {
@@ -42,7 +41,6 @@ pub fn part1() -> anyhow::Result<i64> {
pub fn part2() -> anyhow::Result<i64> {
let mut scores = vec![];
for line in data_lines!()? {
- let line = line?;
let mut open = vec![];
let mut skip = false;
for c in line.chars() {
diff --git a/src/2021/11/mod.rs b/src/2021/11/mod.rs
index 4a3e0d0..2411181 100644
--- a/src/2021/11/mod.rs
+++ b/src/2021/11/mod.rs
@@ -78,8 +78,6 @@ fn iterate(map: &mut Vec<Vec<(i64, bool)>>) -> i64 {
pub fn part1() -> anyhow::Result<i64> {
let mut map = data_lines!()?
- .collect::<Result<Vec<String>, _>>()?
- .iter()
.map(|line| {
line.bytes()
.map(|b| ((b - b'0') as i64, false))
@@ -96,8 +94,6 @@ pub fn part1() -> anyhow::Result<i64> {
pub fn part2() -> anyhow::Result<i64> {
let mut map = data_lines!()?
- .collect::<Result<Vec<String>, _>>()?
- .iter()
.map(|line| {
line.bytes()
.map(|b| ((b - b'0') as i64, false))
diff --git a/src/2021/12/mod.rs b/src/2021/12/mod.rs
index 0935171..022a8b8 100644
--- a/src/2021/12/mod.rs
+++ b/src/2021/12/mod.rs
@@ -71,7 +71,6 @@ fn paths_from2<'a>(
pub fn part1() -> anyhow::Result<i64> {
let mut graph = std::collections::HashMap::new();
for line in data_lines!()? {
- let line = line?;
let nodes: Vec<String> =
line.split('-').map(|s| s.to_string()).collect();
let edges = graph
@@ -89,7 +88,6 @@ pub fn part1() -> anyhow::Result<i64> {
pub fn part2() -> anyhow::Result<i64> {
let mut graph = std::collections::HashMap::new();
for line in data_lines!()? {
- let line = line?;
let nodes: Vec<String> =
line.split('-').map(|s| s.to_string()).collect();
let edges = graph
diff --git a/src/2021/13/mod.rs b/src/2021/13/mod.rs
index 33130e3..56b51a9 100644
--- a/src/2021/13/mod.rs
+++ b/src/2021/13/mod.rs
@@ -108,7 +108,6 @@ pub fn part1() -> anyhow::Result<i64> {
let mut map = Map::new();
let mut folds = vec![];
for line in data_lines!()? {
- let line = line?;
if line.is_empty() {
continue;
}
@@ -133,7 +132,6 @@ pub fn part2() -> anyhow::Result<i64> {
let mut map = Map::new();
let mut folds = vec![];
for line in data_lines!()? {
- let line = line?;
if line.is_empty() {
continue;
}
diff --git a/src/2021/14/mod.rs b/src/2021/14/mod.rs
index 6f0fa76..fb36468 100644
--- a/src/2021/14/mod.rs
+++ b/src/2021/14/mod.rs
@@ -2,12 +2,11 @@
fn parse() -> anyhow::Result<(Vec<u8>, std::collections::HashMap<Vec<u8>, u8>)>
{
let mut lines = data_lines!()?;
- let polymer = lines.next().unwrap()?;
+ let polymer = lines.next().unwrap();
lines.next();
let mut rules = std::collections::HashMap::new();
for line in lines {
- let line = line?;
let rule: Vec<_> = line.split(" -> ").collect();
rules.insert(rule[0].as_bytes().to_vec(), rule[1].as_bytes()[0]);
}
diff --git a/src/2021/15/mod.rs b/src/2021/15/mod.rs
index cd54380..8cb380e 100644
--- a/src/2021/15/mod.rs
+++ b/src/2021/15/mod.rs
@@ -61,15 +61,15 @@ fn dijkstra(map: &[Vec<u8>]) -> i64 {
pub fn part1() -> anyhow::Result<i64> {
let map: Vec<Vec<_>> = data_lines!()?
- .map(|line| line.map(|line| line.bytes().map(|b| b - b'0').collect()))
- .collect::<Result<_, _>>()?;
+ .map(|line| line.bytes().map(|b| b - b'0').collect())
+ .collect();
Ok(dijkstra(&map))
}
pub fn part2() -> anyhow::Result<i64> {
let map: Vec<Vec<_>> = data_lines!()?
- .map(|line| line.map(|line| line.bytes().map(|b| b - b'0').collect()))
- .collect::<Result<_, _>>()?;
+ .map(|line| line.bytes().map(|b| b - b'0').collect())
+ .collect();
let mut large_map = vec![vec![0; map.len() * 5]; map[0].len() * 5];
for li in 0..5 {
for lj in 0..5 {
diff --git a/src/2021/16/mod.rs b/src/2021/16/mod.rs
index 2b245a6..fd57965 100644
--- a/src/2021/16/mod.rs
+++ b/src/2021/16/mod.rs
@@ -194,7 +194,7 @@ impl<'a> Iterator for Subpackets<'a> {
}
pub fn part1() -> anyhow::Result<i64> {
- let line = data_lines!()?.next().unwrap()?;
+ let line = data_lines!()?.next().unwrap();
let mut bits = bits(line.as_bytes().chunks(2).map(|bs| {
u8::from_str_radix(std::str::from_utf8(bs).unwrap(), 16).unwrap()
}));
@@ -206,7 +206,7 @@ pub fn part1() -> anyhow::Result<i64> {
}
pub fn part2() -> anyhow::Result<i64> {
- let line = data_lines!()?.next().unwrap()?;
+ let line = data_lines!()?.next().unwrap();
let mut bits = bits(line.as_bytes().chunks(2).map(|bs| {
u8::from_str_radix(std::str::from_utf8(bs).unwrap(), 16).unwrap()
}));
diff --git a/src/2021/2/mod.rs b/src/2021/2/mod.rs
index 140d026..e175ab9 100644
--- a/src/2021/2/mod.rs
+++ b/src/2021/2/mod.rs
@@ -2,7 +2,6 @@ pub fn part1() -> anyhow::Result<i64> {
let mut horizontal = 0;
let mut vertical = 0;
for line in data_lines!()? {
- let line = line?;
if let Some(n) = line.strip_prefix("forward ") {
horizontal += n.parse::<i64>()?;
} else if let Some(n) = line.strip_prefix("down ") {
@@ -19,7 +18,6 @@ pub fn part2() -> anyhow::Result<i64> {
let mut horizontal = 0;
let mut vertical = 0;
for line in data_lines!()? {
- let line = line?;
if let Some(n) = line.strip_prefix("forward ") {
let x = n.parse::<i64>()?;
horizontal += x;
diff --git a/src/2021/3/mod.rs b/src/2021/3/mod.rs
index e8f34f5..a0fc64e 100644
--- a/src/2021/3/mod.rs
+++ b/src/2021/3/mod.rs
@@ -12,13 +12,12 @@ pub fn part1() -> anyhow::Result<i64> {
}
pub fn part2() -> anyhow::Result<i64> {
- let mut oxygen: Vec<_> = data_lines!()?.collect::<Result<_, _>>()?;
+ let mut oxygen: Vec<_> = data_lines!()?.collect();
for i in 0..oxygen[0].len() {
if oxygen.len() == 1 {
break;
}
- let (total_lines, by_pos) =
- pos_counts(oxygen.iter().cloned().map(Ok))?;
+ let (total_lines, by_pos) = pos_counts(oxygen.iter().cloned())?;
let keep = if by_pos[i] * 2 >= total_lines {
'1'
} else {
@@ -32,12 +31,12 @@ pub fn part2() -> anyhow::Result<i64> {
oxygen = new_oxygen;
}
- let mut co2: Vec<_> = data_lines!()?.collect::<Result<_, _>>()?;
+ let mut co2: Vec<_> = data_lines!()?.collect();
for i in 0..co2[0].len() {
if co2.len() == 1 {
break;
}
- let (total_lines, by_pos) = pos_counts(co2.iter().cloned().map(Ok))?;
+ let (total_lines, by_pos) = pos_counts(co2.iter().cloned())?;
let keep = if by_pos[i] * 2 >= total_lines {
'0'
} else {
@@ -55,12 +54,11 @@ pub fn part2() -> anyhow::Result<i64> {
}
fn pos_counts(
- lines: impl std::iter::Iterator<Item = std::io::Result<String>>,
+ lines: impl std::iter::Iterator<Item = String>,
) -> anyhow::Result<(i64, Vec<i64>)> {
let mut by_pos = vec![];
let mut total_lines = 0;
for line in lines {
- let line = line?;
total_lines += 1;
if by_pos.is_empty() {
by_pos.resize(line.len(), 0);
diff --git a/src/2021/5/mod.rs b/src/2021/5/mod.rs
index 0ce7f6b..7f8de45 100644
--- a/src/2021/5/mod.rs
+++ b/src/2021/5/mod.rs
@@ -102,7 +102,6 @@ pub fn part1() -> anyhow::Result<i64> {
let rx = regex::Regex::new("^(\\d+),(\\d+) -> (\\d+),(\\d+)$").unwrap();
let mut map = Map::new();
for line in data_lines!()? {
- let line = line?;
let nums: Vec<usize> = rx
.captures(&line)
.unwrap()
@@ -121,7 +120,6 @@ pub fn part2() -> anyhow::Result<i64> {
let rx = regex::Regex::new("^(\\d+),(\\d+) -> (\\d+),(\\d+)$").unwrap();
let mut map = Map::new();
for line in data_lines!()? {
- let line = line?;
let nums: Vec<usize> = rx
.captures(&line)
.unwrap()
diff --git a/src/2021/6/mod.rs b/src/2021/6/mod.rs
index 3948886..821e777 100644
--- a/src/2021/6/mod.rs
+++ b/src/2021/6/mod.rs
@@ -1,10 +1,5 @@
pub fn part1() -> anyhow::Result<i64> {
- let mut fishes: Vec<u8> = data_lines!()?
- .next()
- .unwrap()?
- .split(',')
- .map(|s| s.parse())
- .collect::<Result<_, _>>()?;
+ let mut fishes: Vec<_> = data_ints!(b',')?.collect();
for _ in 0..80 {
let mut new = 0;
for fish in fishes.iter_mut() {
@@ -21,12 +16,7 @@ pub fn part1() -> anyhow::Result<i64> {
}
pub fn part2() -> anyhow::Result<i64> {
- let fishes: Vec<u8> = data_lines!()?
- .next()
- .unwrap()?
- .split(',')
- .map(|s| s.parse())
- .collect::<Result<_, _>>()?;
+ let fishes: Vec<_> = data_ints!(b',')?.collect();
let mut by_age = std::collections::VecDeque::new();
by_age.resize(9, 0);
for fish in fishes {
diff --git a/src/2021/7/mod.rs b/src/2021/7/mod.rs
index c389f01..1d8320a 100644
--- a/src/2021/7/mod.rs
+++ b/src/2021/7/mod.rs
@@ -1,11 +1,5 @@
pub fn part1() -> anyhow::Result<i64> {
- let crabs: Vec<i64> = data_lines!()?
- .next()
- .unwrap()?
- .split(',')
- .map(|s| s.parse())
- .collect::<Result<_, _>>()?;
-
+ let crabs: Vec<_> = data_ints!(b',')?.collect();
Ok((0..=crabs.iter().copied().max().unwrap())
.map(|start| {
crabs.iter().copied().map(|crab| (crab - start).abs()).sum()
@@ -15,13 +9,7 @@ pub fn part1() -> anyhow::Result<i64> {
}
pub fn part2() -> anyhow::Result<i64> {
- let crabs: Vec<i64> = data_lines!()?
- .next()
- .unwrap()?
- .split(',')
- .map(|s| s.parse())
- .collect::<Result<_, _>>()?;
-
+ let crabs: Vec<_> = data_ints!(b',')?.collect();
Ok((0..=crabs.iter().copied().max().unwrap())
.map(|start| {
crabs
diff --git a/src/2021/8/mod.rs b/src/2021/8/mod.rs
index 2614ebe..7244524 100644
--- a/src/2021/8/mod.rs
+++ b/src/2021/8/mod.rs
@@ -1,7 +1,6 @@
pub fn part1() -> anyhow::Result<i64> {
let mut count = 0i64;
for line in data_lines!()? {
- let line = line?;
let mut parts = line.split(" | ");
let output = parts.nth(1).unwrap();
let line_count: i64 = output
@@ -25,7 +24,6 @@ pub fn part1() -> anyhow::Result<i64> {
pub fn part2() -> anyhow::Result<i64> {
let mut total = 0;
for line in data_lines!()? {
- let line = line?;
let mut parts = line.split(" | ");
let numbers = parts.next().unwrap();
let numbers: Vec<_> = numbers.split(' ').collect();
diff --git a/src/2021/9/mod.rs b/src/2021/9/mod.rs
index 939df15..1046c6c 100644
--- a/src/2021/9/mod.rs
+++ b/src/2021/9/mod.rs
@@ -1,7 +1,6 @@
pub fn part1() -> anyhow::Result<i64> {
let mut map = vec![];
for line in data_lines!()? {
- let line = line?;
let mut row = vec![];
for c in line.bytes() {
row.push(c - b'0');
@@ -39,7 +38,6 @@ pub fn part1() -> anyhow::Result<i64> {
pub fn part2() -> anyhow::Result<i64> {
let mut map = vec![];
for line in data_lines!()? {
- let line = line?;
let mut row = vec![];
for c in line.bytes() {
row.push(c - b'0');
diff --git a/src/util.rs b/src/util.rs
index d5df6ea..423b18e 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,4 +1,3 @@
-use anyhow::Context as _;
use std::io::{BufRead as _, Read as _};
macro_rules! data {
@@ -10,36 +9,31 @@ macro_rules! data {
}};
}
-macro_rules! data_ints {
+macro_rules! data_lines {
() => {
- crate::util::read_ints(&crate::util::src_file_to_data_file(
- &std::file!(),
- ))
+ data!().map(|fh| crate::util::lines(fh))
};
}
-macro_rules! data_bytes {
+macro_rules! data_ints {
() => {
- crate::util::read_file(&crate::util::src_file_to_data_file(
- &std::file!(),
- ))
+ data!().map(|fh| crate::util::ints_by_line(fh))
+ };
+ ($sep:expr) => {
+ data!().map(|fh| crate::util::ints_by_split(fh, $sep))
};
}
-macro_rules! data_str {
+macro_rules! data_bytes {
() => {
- crate::util::read_file_str(&crate::util::src_file_to_data_file(
- &std::file!(),
- ))
+ data!().map(|fh| crate::util::bytes(fh))
};
}
-macro_rules! data_lines {
- () => {
- crate::util::read_file_lines(&crate::util::src_file_to_data_file(
- &std::file!(),
- ))
- };
+macro_rules! data_str {
+ () => {{
+ data!().map(|fh| crate::util::string(fh))
+ }};
}
pub fn src_file_to_data_file(file: &str) -> String {
@@ -51,41 +45,36 @@ pub fn src_file_to_data_file(file: &str) -> String {
)
}
-pub fn read_ints(filename: &str) -> anyhow::Result<Vec<i64>> {
- let ints: anyhow::Result<Vec<_>> = read_file_lines(filename)?
- .map(|l| {
- l.context("failed to read a line")?
- .parse()
- .context("failed to parse line into an integer")
- })
- .collect();
- ints
+pub fn lines(fh: std::fs::File) -> impl Iterator<Item = String> {
+ let fh = std::io::BufReader::new(fh);
+ fh.lines().map(|res| res.unwrap())
+}
+
+pub fn ints_by_line(fh: std::fs::File) -> impl Iterator<Item = i64> {
+ lines(fh).map(|l| l.parse().unwrap())
}
-pub fn read_file(filename: &str) -> anyhow::Result<Vec<u8>> {
- let mut f = std::fs::File::open(filename)
- .with_context(|| format!("couldn't find data file {}", filename))?;
- let mut s = vec![];
- f.read_to_end(&mut s)
- .context("failed to read map contents")?;
- Ok(s)
+pub fn ints_by_split(
+ fh: std::fs::File,
+ sep: u8,
+) -> impl Iterator<Item = i64> {
+ let fh = std::io::BufReader::new(fh);
+ fh.split(sep).filter_map(|res| {
+ let res = res.unwrap();
+ let s = std::str::from_utf8(&res).unwrap().trim();
+ if s.is_empty() {
+ None
+ } else {
+ Some(s.parse().unwrap())
+ }
+ })
}
-pub fn read_file_str(filename: &str) -> anyhow::Result<String> {
- let mut f = std::fs::File::open(filename)
- .with_context(|| format!("couldn't find data file {}", filename))?;
- let mut s = String::new();
- f.read_to_string(&mut s)
- .context("failed to read map contents")?;
- Ok(s)
+pub fn bytes(fh: std::fs::File) -> impl Iterator<Item = u8> {
+ fh.bytes().map(|res| res.unwrap())
}
-pub fn read_file_lines(
- filename: &str,
-) -> anyhow::Result<impl std::iter::Iterator<Item = std::io::Result<String>>>
-{
- let f = std::fs::File::open(filename)
- .with_context(|| format!("couldn't find data file {}", filename))?;
- let f = std::io::BufReader::new(f);
- Ok(f.lines())
+pub fn string(fh: std::fs::File) -> String {
+ let bytes: Vec<_> = bytes(fh).collect();
+ std::string::String::from_utf8(bytes).unwrap()
}