From 3a5398399221c7986a69c6cc90f7bb70669f39a8 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 21 Dec 2021 04:18:01 -0500 Subject: prelude to reduce some typing --- src/2020/1/mod.rs | 18 ++++++----- src/2020/2/mod.rs | 21 ++++++------- src/2020/3/mod.rs | 22 ++++++------- src/2020/4/mod.rs | 28 +++++++---------- src/2020/5/mod.rs | 29 +++++++++--------- src/2020/6/mod.rs | 24 +++++++-------- src/2020/7/mod.rs | 34 +++++++++------------ src/2020/8/mod.rs | 34 ++++++++++----------- src/2020/9/mod.rs | 22 ++++++------- src/2020/mod.rs | 42 +++++++++++++------------ src/2021/1/mod.rs | 14 +++++---- src/2021/10/mod.rs | 16 +++++----- src/2021/11/mod.rs | 22 ++++++------- src/2021/12/mod.rs | 52 ++++++++++--------------------- src/2021/13/mod.rs | 23 +++++--------- src/2021/14/mod.rs | 35 ++++++++++----------- src/2021/15/mod.rs | 16 +++++----- src/2021/16/mod.rs | 18 ++++++----- src/2021/17/mod.rs | 30 +++++++++--------- src/2021/18/mod.rs | 16 +++++----- src/2021/19/mod.rs | 27 ++++++++-------- src/2021/2/mod.rs | 16 +++++----- src/2021/20/mod.rs | 16 +++++----- src/2021/21/mod.rs | 14 +++++---- src/2021/3/mod.rs | 20 ++++++------ src/2021/4/mod.rs | 18 +++++------ src/2021/5/mod.rs | 22 +++++-------- src/2021/6/mod.rs | 19 ++++++------ src/2021/7/mod.rs | 17 +++++------ src/2021/8/mod.rs | 30 +++++++++--------- src/2021/9/mod.rs | 16 +++++----- src/2021/mod.rs | 90 ++++++++++++++++++++++++++++-------------------------- src/main.rs | 1 + src/prelude.rs | 10 ++++++ src/util.rs | 5 --- src/util/parse.rs | 20 ++++++------ 36 files changed, 403 insertions(+), 454 deletions(-) create mode 100644 src/prelude.rs diff --git a/src/2020/1/mod.rs b/src/2020/1/mod.rs index 31c4c5d..1ed3824 100644 --- a/src/2020/1/mod.rs +++ b/src/2020/1/mod.rs @@ -1,8 +1,10 @@ -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok(crate::util::parse::ints(crate::util::parse::lines(fh)).collect()) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result> { + Ok(parse::ints(parse::lines(fh)).collect()) } -pub fn part1(ints: Vec) -> anyhow::Result { +pub fn part1(ints: Vec) -> Result { for i in &ints { for j in &ints { if i + j == 2020 { @@ -10,10 +12,10 @@ pub fn part1(ints: Vec) -> anyhow::Result { } } } - Err(anyhow::anyhow!("no numbers summing to 2020 found")) + Err(anyhow!("no numbers summing to 2020 found")) } -pub fn part2(ints: Vec) -> anyhow::Result { +pub fn part2(ints: Vec) -> Result { for i in &ints { for j in &ints { for k in &ints { @@ -23,17 +25,17 @@ pub fn part2(ints: Vec) -> anyhow::Result { } } } - Err(anyhow::anyhow!("no numbers summing to 2020 found")) + Err(anyhow!("no numbers summing to 2020 found")) } #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 1).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 1).unwrap()).unwrap()).unwrap(), 445536 ); assert_eq!( - part2(parse(crate::util::data(2020, 1).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 1).unwrap()).unwrap()).unwrap(), 138688160 ); } diff --git a/src/2020/2/mod.rs b/src/2020/2/mod.rs index 31dfeb5..75cc4f1 100644 --- a/src/2020/2/mod.rs +++ b/src/2020/2/mod.rs @@ -1,5 +1,4 @@ -use anyhow::Context as _; -use std::convert::TryInto as _; +use crate::prelude::*; pub struct Line { c: char, @@ -9,8 +8,8 @@ pub struct Line { } impl Line { - fn parse(line: &str) -> anyhow::Result { - let rx = regex::Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap(); + fn parse(line: &str) -> Result { + let rx = Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap(); let captures = rx.captures(line).context("line failed to match regex")?; let c = captures @@ -51,18 +50,16 @@ impl Line { } } -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result> { - Ok(crate::util::parse::lines(fh).map(|line| Line::parse(&line).unwrap())) +pub fn parse(fh: File) -> Result> { + Ok(parse::lines(fh).map(|line| Line::parse(&line).unwrap())) } -pub fn part1(lines: impl Iterator) -> anyhow::Result { +pub fn part1(lines: impl Iterator) -> Result { let count = lines.filter(|l| l.valid_part_1()).count(); Ok(count.try_into()?) } -pub fn part2(lines: impl Iterator) -> anyhow::Result { +pub fn part2(lines: impl Iterator) -> Result { let count = lines.filter(|l| l.valid_part_2()).count(); Ok(count.try_into()?) } @@ -70,11 +67,11 @@ pub fn part2(lines: impl Iterator) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 2).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 2).unwrap()).unwrap()).unwrap(), 638 ); assert_eq!( - part2(parse(crate::util::data(2020, 2).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 2).unwrap()).unwrap()).unwrap(), 699 ); } diff --git a/src/2020/3/mod.rs b/src/2020/3/mod.rs index 03ddfce..29e20d2 100644 --- a/src/2020/3/mod.rs +++ b/src/2020/3/mod.rs @@ -1,4 +1,4 @@ -use crate::util::grid::*; +use crate::prelude::*; pub struct Map { grid: Grid, @@ -13,7 +13,7 @@ impl Map { self.grid.rows().0 } - fn tree_at(&self, row: Row, col: Col) -> anyhow::Result { + fn tree_at(&self, row: Row, col: Col) -> Result { // unwrap safe because cycle().nth() can never fail Ok(*self.grid[row].iter().cycle().nth(col.0).unwrap()) } @@ -22,7 +22,7 @@ impl Map { &self, row_incr: usize, col_incr: usize, - ) -> anyhow::Result { + ) -> Result { let mut trees = 0; for r in 0..self.rows() / row_incr { let row = r * row_incr; @@ -35,19 +35,15 @@ impl Map { } } -pub fn parse(fh: std::fs::File) -> anyhow::Result { - Ok(Map::new(crate::util::parse::bool_grid( - crate::util::parse::lines(fh), - b'#', - b'.', - ))) +pub fn parse(fh: File) -> Result { + Ok(Map::new(parse::bool_grid(parse::lines(fh), b'#', b'.'))) } -pub fn part1(map: Map) -> anyhow::Result { +pub fn part1(map: Map) -> Result { map.trees_for_slope(1, 3) } -pub fn part2(map: Map) -> anyhow::Result { +pub fn part2(map: Map) -> Result { Ok(map.trees_for_slope(1, 1)? * map.trees_for_slope(1, 3)? * map.trees_for_slope(1, 5)? @@ -58,11 +54,11 @@ pub fn part2(map: Map) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 3).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 3).unwrap()).unwrap()).unwrap(), 292 ); assert_eq!( - part2(parse(crate::util::data(2020, 3).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 3).unwrap()).unwrap()).unwrap(), 9354744432 ); } diff --git a/src/2020/4/mod.rs b/src/2020/4/mod.rs index f89993e..98a35fb 100644 --- a/src/2020/4/mod.rs +++ b/src/2020/4/mod.rs @@ -1,17 +1,15 @@ -use anyhow::Context as _; +use crate::prelude::*; const REQUIRED_KEYS: &[&str] = &["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]; -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result>> { +pub fn parse(fh: File) -> Result>> { let mut res = vec![]; - let mut cur = std::collections::HashMap::new(); - for line in crate::util::parse::lines(fh) { + let mut cur = HashMap::new(); + for line in parse::lines(fh) { if line.is_empty() { res.push(cur); - cur = std::collections::HashMap::new(); + cur = HashMap::new(); continue; } @@ -32,9 +30,7 @@ pub fn parse( Ok(res) } -pub fn part1( - passports: Vec>, -) -> anyhow::Result { +pub fn part1(passports: Vec>) -> Result { let mut valid = 0; for passport in passports { let mut cur_valid = true; @@ -51,9 +47,7 @@ pub fn part1( Ok(valid) } -pub fn part2( - passports: Vec>, -) -> anyhow::Result { +pub fn part2(passports: Vec>) -> Result { let mut valid = 0; for passport in passports { let mut cur_valid = true; @@ -78,7 +72,7 @@ pub fn part2( Ok(valid) } -fn validate(key: &str, val: &str) -> anyhow::Result { +fn validate(key: &str, val: &str) -> Result { match key { "byr" => match val.parse::() { Ok(year) => Ok((1920..=2002).contains(&year)), @@ -127,18 +121,18 @@ fn validate(key: &str, val: &str) -> anyhow::Result { == val .matches(|c: char| c.is_ascii_digit()) .collect::()), - _ => Err(anyhow::anyhow!("invalid key found: {}", key)), + _ => Err(anyhow!("invalid key found: {}", key)), } } #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 4).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 4).unwrap()).unwrap()).unwrap(), 247 ); assert_eq!( - part2(parse(crate::util::data(2020, 4).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 4).unwrap()).unwrap()).unwrap(), 145 ); } diff --git a/src/2020/5/mod.rs b/src/2020/5/mod.rs index b824e60..1f57f4f 100644 --- a/src/2020/5/mod.rs +++ b/src/2020/5/mod.rs @@ -1,11 +1,10 @@ -use anyhow::Context as _; -use std::convert::TryInto as _; +use crate::prelude::*; -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok(crate::util::parse::lines(fh).map(|line| seat_id(&line).unwrap())) +pub fn parse(fh: File) -> Result> { + Ok(parse::lines(fh).map(|line| seat_id(&line).unwrap())) } -pub fn part1(ids: impl Iterator) -> anyhow::Result { +pub fn part1(ids: impl Iterator) -> Result { let mut max = 0; for id in ids { if id > max { @@ -15,7 +14,7 @@ pub fn part1(ids: impl Iterator) -> anyhow::Result { Ok(max) } -pub fn part2(ids: impl Iterator) -> anyhow::Result { +pub fn part2(ids: impl Iterator) -> Result { let mut seats = vec![false; 1024]; for id in ids { seats[id as usize] = true; @@ -31,14 +30,14 @@ pub fn part2(ids: impl Iterator) -> anyhow::Result { .context("failed to find free seat")? + first; if !seats[seat - 1] || seats[seat] || !seats[seat + 1] { - return Err(anyhow::anyhow!("invalid seat found")); + return Err(anyhow!("invalid seat found")); } Ok(seat.try_into()?) } -fn seat_id(desc: &str) -> anyhow::Result { +fn seat_id(desc: &str) -> Result { if desc.len() != 10 { - return Err(anyhow::anyhow!("invalid desc {}", desc)); + return Err(anyhow!("invalid desc {}", desc)); } let row_desc = &desc[0..7]; let col_desc = &desc[7..10]; @@ -54,11 +53,11 @@ fn seat_id(desc: &str) -> anyhow::Result { 'B' => { min_row = mid + 1; } - _ => return Err(anyhow::anyhow!("invalid desc {}", desc)), + _ => return Err(anyhow!("invalid desc {}", desc)), } } if min_row != max_row { - return Err(anyhow::anyhow!("bug")); + return Err(anyhow!("bug")); } let row = min_row; @@ -73,11 +72,11 @@ fn seat_id(desc: &str) -> anyhow::Result { 'R' => { min_col = mid + 1; } - _ => return Err(anyhow::anyhow!("invalid desc {}", desc)), + _ => return Err(anyhow!("invalid desc {}", desc)), } } if min_col != max_col { - return Err(anyhow::anyhow!("bug")); + return Err(anyhow!("bug")); } let col = min_col; @@ -87,11 +86,11 @@ fn seat_id(desc: &str) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 5).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 5).unwrap()).unwrap()).unwrap(), 978 ); assert_eq!( - part2(parse(crate::util::data(2020, 5).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 5).unwrap()).unwrap()).unwrap(), 727 ); } diff --git a/src/2020/6/mod.rs b/src/2020/6/mod.rs index 21279af..0f61621 100644 --- a/src/2020/6/mod.rs +++ b/src/2020/6/mod.rs @@ -1,16 +1,16 @@ -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result> { - Ok(crate::util::parse::lines(fh)) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result> { + Ok(parse::lines(fh)) } -pub fn part1(lines: impl Iterator) -> anyhow::Result { - let mut yes = std::collections::HashSet::new(); +pub fn part1(lines: impl Iterator) -> Result { + let mut yes = HashSet::new(); let mut total = 0; for line in lines { if line.is_empty() { total += yes.len() as i64; - yes = std::collections::HashSet::new(); + yes = HashSet::new(); } else { for c in line.chars() { yes.insert(c); @@ -21,8 +21,8 @@ pub fn part1(lines: impl Iterator) -> anyhow::Result { Ok(total) } -pub fn part2(lines: impl Iterator) -> anyhow::Result { - let mut yes = std::collections::HashSet::new(); +pub fn part2(lines: impl Iterator) -> Result { + let mut yes = HashSet::new(); for c in 'a'..='z' { yes.insert(c); } @@ -30,7 +30,7 @@ pub fn part2(lines: impl Iterator) -> anyhow::Result { for line in lines { if line.is_empty() { total += yes.len() as i64; - yes = std::collections::HashSet::new(); + yes = HashSet::new(); for c in 'a'..='z' { yes.insert(c); } @@ -49,11 +49,11 @@ pub fn part2(lines: impl Iterator) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 6).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 6).unwrap()).unwrap()).unwrap(), 6930 ); assert_eq!( - part2(parse(crate::util::data(2020, 6).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 6).unwrap()).unwrap()).unwrap(), 3585 ); } diff --git a/src/2020/7/mod.rs b/src/2020/7/mod.rs index fbf2c43..6199ad0 100644 --- a/src/2020/7/mod.rs +++ b/src/2020/7/mod.rs @@ -1,9 +1,9 @@ -use anyhow::Context as _; +use crate::prelude::*; -type Graph = std::collections::HashMap>; +type Graph = HashMap>; -pub fn parse(fh: std::fs::File) -> anyhow::Result { - let input = crate::util::parse::string(fh); +pub fn parse(fh: File) -> Result { + let input = parse::string(fh); let mut graph = Graph::new(); for line in input.lines() { let (k, v) = parse_line(line)?; @@ -12,7 +12,7 @@ pub fn parse(fh: std::fs::File) -> anyhow::Result { Ok(graph) } -pub fn part1(graph: Graph) -> anyhow::Result { +pub fn part1(graph: Graph) -> Result { let mut colors = 0; for color in graph.keys() { if bag_contains(&graph, color, "shiny gold")? { @@ -22,14 +22,14 @@ pub fn part1(graph: Graph) -> anyhow::Result { Ok(colors) } -pub fn part2(graph: Graph) -> anyhow::Result { +pub fn part2(graph: Graph) -> Result { // subtract 1 to not count the shiny gold bag itself count_bags(&graph, "shiny gold").map(|i| i - 1) } -fn parse_line(line: &str) -> anyhow::Result<(String, Vec<(i64, String)>)> { - let main_rx = regex::Regex::new(r"^(.*) bags contain (.*)\.$").unwrap(); - let contents_rx = regex::Regex::new(r"^([0-9]+) (.*) bags?").unwrap(); +fn parse_line(line: &str) -> Result<(String, Vec<(i64, String)>)> { + let main_rx = Regex::new(r"^(.*) bags contain (.*)\.$").unwrap(); + let contents_rx = Regex::new(r"^([0-9]+) (.*) bags?").unwrap(); let captures = main_rx .captures(line) @@ -57,16 +57,12 @@ fn parse_line(line: &str) -> anyhow::Result<(String, Vec<(i64, String)>)> { captures.get(2).unwrap().as_str().to_string(), )) }) - .collect::>()?, + .collect::>()?, )) } } -fn bag_contains( - graph: &Graph, - start: &str, - target: &str, -) -> anyhow::Result { +fn bag_contains(graph: &Graph, start: &str, target: &str) -> Result { let mut to_check = graph .get(&start.to_string()) .context("failed to find starting color in graph")? @@ -86,13 +82,13 @@ fn bag_contains( Ok(false) } -fn count_bags(graph: &Graph, color: &str) -> anyhow::Result { +fn count_bags(graph: &Graph, color: &str) -> Result { Ok(1 + graph .get(&color.to_string()) .context("failed to find starting color in graph")? .iter() .map(|(count, child)| Ok(count * count_bags(graph, child)?)) - .collect::>>()? + .collect::>>()? .iter() .sum::()) } @@ -100,11 +96,11 @@ fn count_bags(graph: &Graph, color: &str) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 7).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 7).unwrap()).unwrap()).unwrap(), 169 ); assert_eq!( - part2(parse(crate::util::data(2020, 7).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 7).unwrap()).unwrap()).unwrap(), 82372 ); } diff --git a/src/2020/8/mod.rs b/src/2020/8/mod.rs index 052538e..d1908cd 100644 --- a/src/2020/8/mod.rs +++ b/src/2020/8/mod.rs @@ -1,4 +1,4 @@ -use anyhow::Context as _; +use crate::prelude::*; #[derive(Clone, Copy)] enum OpType { @@ -8,14 +8,14 @@ enum OpType { } impl std::str::FromStr for OpType { - type Err = anyhow::Error; + type Err = Error; fn from_str(s: &str) -> std::result::Result { Ok(match s { "nop" => Self::Nop, "acc" => Self::Acc, "jmp" => Self::Jmp, - _ => return Err(anyhow::anyhow!("invalid optype {}", s)), + _ => return Err(anyhow!("invalid optype {}", s)), }) } } @@ -27,10 +27,10 @@ pub struct Op { } impl std::str::FromStr for Op { - type Err = anyhow::Error; + type Err = Error; fn from_str(s: &str) -> std::result::Result { - let rx = regex::Regex::new(r"^([^ ]*) ((?:-|\+)[0-9]+)$").unwrap(); + let rx = Regex::new(r"^([^ ]*) ((?:-|\+)[0-9]+)$").unwrap(); let captures = rx.captures(s).context("failed to parse line")?; let ty = captures.get(1).unwrap().as_str().parse()?; let arg = captures @@ -43,21 +43,19 @@ impl std::str::FromStr for Op { } } -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - crate::util::parse::lines(fh) - .map(|line| line.parse()) - .collect() +pub fn parse(fh: File) -> Result> { + parse::lines(fh).map(|line| line.parse()).collect() } -pub fn part1(opcodes: Vec) -> anyhow::Result { +pub fn part1(opcodes: Vec) -> Result { let (acc, success) = run(&opcodes)?; if success { - return Err(anyhow::anyhow!("unexpectedly succeeded")); + return Err(anyhow!("unexpectedly succeeded")); } Ok(acc) } -pub fn part2(opcodes: Vec) -> anyhow::Result { +pub fn part2(opcodes: Vec) -> Result { for i in 0..opcodes.len() { match opcodes[i].ty { OpType::Nop => { @@ -79,10 +77,10 @@ pub fn part2(opcodes: Vec) -> anyhow::Result { } } } - Err(anyhow::anyhow!("failed to find corrupted opcode")) + Err(anyhow!("failed to find corrupted opcode")) } -fn run(opcodes: &[Op]) -> anyhow::Result<(i64, bool)> { +fn run(opcodes: &[Op]) -> Result<(i64, bool)> { let mut seen = vec![false; opcodes.len()]; let mut pc = 0; let mut acc = 0; @@ -108,12 +106,12 @@ fn run(opcodes: &[Op]) -> anyhow::Result<(i64, bool)> { if arg as usize > opcodes.len() || pc > opcodes.len() - arg as usize { - return Err(anyhow::anyhow!("invalid jmp")); + return Err(anyhow!("invalid jmp")); } pc += arg as usize; } else { if pc < (-arg as usize) { - return Err(anyhow::anyhow!("invalid jmp")); + return Err(anyhow!("invalid jmp")); } pc -= -arg as usize; } @@ -125,11 +123,11 @@ fn run(opcodes: &[Op]) -> anyhow::Result<(i64, bool)> { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 8).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 8).unwrap()).unwrap()).unwrap(), 1928 ); assert_eq!( - part2(parse(crate::util::data(2020, 8).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 8).unwrap()).unwrap()).unwrap(), 1319 ); } diff --git a/src/2020/9/mod.rs b/src/2020/9/mod.rs index c067ca0..0d2dd6f 100644 --- a/src/2020/9/mod.rs +++ b/src/2020/9/mod.rs @@ -1,10 +1,12 @@ +use crate::prelude::*; + const WINDOW: usize = 25; -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok(crate::util::parse::ints(crate::util::parse::lines(fh)).collect()) +pub fn parse(fh: File) -> Result> { + Ok(parse::ints(parse::lines(fh)).collect()) } -pub fn part1(list: Vec) -> anyhow::Result { +pub fn part1(list: Vec) -> Result { for i in 0..(list.len() - WINDOW) { let set = &list[i..i + WINDOW]; let n = list[i + WINDOW]; @@ -13,10 +15,10 @@ pub fn part1(list: Vec) -> anyhow::Result { } } - Err(anyhow::anyhow!("failed to find invalid number")) + Err(anyhow!("failed to find invalid number")) } -pub fn part2(list: Vec) -> anyhow::Result { +pub fn part2(list: Vec) -> Result { let mut invalid = None; for i in 0..(list.len() - WINDOW) { let set = &list[i..i + WINDOW]; @@ -26,7 +28,7 @@ pub fn part2(list: Vec) -> anyhow::Result { } } if invalid.is_none() { - return Err(anyhow::anyhow!("failed to find invalid number")); + return Err(anyhow!("failed to find invalid number")); } let invalid = invalid.unwrap(); @@ -40,9 +42,7 @@ pub fn part2(list: Vec) -> anyhow::Result { } } - Err(anyhow::anyhow!( - "failed to find sequence summing to invalid number" - )) + Err(anyhow!("failed to find sequence summing to invalid number")) } fn valid(set: &[i64], n: i64) -> bool { @@ -64,11 +64,11 @@ fn valid(set: &[i64], n: i64) -> bool { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2020, 9).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2020, 9).unwrap()).unwrap()).unwrap(), 373803594 ); assert_eq!( - part2(parse(crate::util::data(2020, 9).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2020, 9).unwrap()).unwrap()).unwrap(), 51152360 ); } diff --git a/src/2020/mod.rs b/src/2020/mod.rs index b4ea179..c8d45a0 100644 --- a/src/2020/mod.rs +++ b/src/2020/mod.rs @@ -1,3 +1,5 @@ +use crate::prelude::*; + #[path = "1/mod.rs"] mod day1; #[path = "2/mod.rs"] @@ -18,27 +20,27 @@ mod day8; mod day9; // NEXT MOD -pub fn run(day: u8, puzzle: u8) -> anyhow::Result { +pub fn run(day: u8, puzzle: u8) -> Result { match (day, puzzle) { - (1, 1) => day1::part1(day1::parse(crate::util::data(2020, 1)?)?), - (1, 2) => day1::part2(day1::parse(crate::util::data(2020, 1)?)?), - (2, 1) => day2::part1(day2::parse(crate::util::data(2020, 2)?)?), - (2, 2) => day2::part2(day2::parse(crate::util::data(2020, 2)?)?), - (3, 1) => day3::part1(day3::parse(crate::util::data(2020, 3)?)?), - (3, 2) => day3::part2(day3::parse(crate::util::data(2020, 3)?)?), - (4, 1) => day4::part1(day4::parse(crate::util::data(2020, 4)?)?), - (4, 2) => day4::part2(day4::parse(crate::util::data(2020, 4)?)?), - (5, 1) => day5::part1(day5::parse(crate::util::data(2020, 5)?)?), - (5, 2) => day5::part2(day5::parse(crate::util::data(2020, 5)?)?), - (6, 1) => day6::part1(day6::parse(crate::util::data(2020, 6)?)?), - (6, 2) => day6::part2(day6::parse(crate::util::data(2020, 6)?)?), - (7, 1) => day7::part1(day7::parse(crate::util::data(2020, 7)?)?), - (7, 2) => day7::part2(day7::parse(crate::util::data(2020, 7)?)?), - (8, 1) => day8::part1(day8::parse(crate::util::data(2020, 8)?)?), - (8, 2) => day8::part2(day8::parse(crate::util::data(2020, 8)?)?), - (9, 1) => day9::part1(day9::parse(crate::util::data(2020, 9)?)?), - (9, 2) => day9::part2(day9::parse(crate::util::data(2020, 9)?)?), + (1, 1) => day1::part1(day1::parse(parse::data(2020, 1)?)?), + (1, 2) => day1::part2(day1::parse(parse::data(2020, 1)?)?), + (2, 1) => day2::part1(day2::parse(parse::data(2020, 2)?)?), + (2, 2) => day2::part2(day2::parse(parse::data(2020, 2)?)?), + (3, 1) => day3::part1(day3::parse(parse::data(2020, 3)?)?), + (3, 2) => day3::part2(day3::parse(parse::data(2020, 3)?)?), + (4, 1) => day4::part1(day4::parse(parse::data(2020, 4)?)?), + (4, 2) => day4::part2(day4::parse(parse::data(2020, 4)?)?), + (5, 1) => day5::part1(day5::parse(parse::data(2020, 5)?)?), + (5, 2) => day5::part2(day5::parse(parse::data(2020, 5)?)?), + (6, 1) => day6::part1(day6::parse(parse::data(2020, 6)?)?), + (6, 2) => day6::part2(day6::parse(parse::data(2020, 6)?)?), + (7, 1) => day7::part1(day7::parse(parse::data(2020, 7)?)?), + (7, 2) => day7::part2(day7::parse(parse::data(2020, 7)?)?), + (8, 1) => day8::part1(day8::parse(parse::data(2020, 8)?)?), + (8, 2) => day8::part2(day8::parse(parse::data(2020, 8)?)?), + (9, 1) => day9::part1(day9::parse(parse::data(2020, 9)?)?), + (9, 2) => day9::part2(day9::parse(parse::data(2020, 9)?)?), // NEXT PART - _ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)), + _ => Err(anyhow!("unknown puzzle {}-{}", day, puzzle)), } } diff --git a/src/2021/1/mod.rs b/src/2021/1/mod.rs index 3ba61e2..c01714a 100644 --- a/src/2021/1/mod.rs +++ b/src/2021/1/mod.rs @@ -1,8 +1,10 @@ -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok(crate::util::parse::ints(crate::util::parse::lines(fh)).collect()) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result> { + Ok(parse::ints(parse::lines(fh)).collect()) } -pub fn part1(ints: Vec) -> anyhow::Result { +pub fn part1(ints: Vec) -> Result { Ok(ints .windows(2) .map(|a| a[1] - a[0]) @@ -11,7 +13,7 @@ pub fn part1(ints: Vec) -> anyhow::Result { .try_into()?) } -pub fn part2(ints: Vec) -> anyhow::Result { +pub fn part2(ints: Vec) -> Result { Ok(ints .windows(3) .map(|a| a[0] + a[1] + a[2]) @@ -26,11 +28,11 @@ pub fn part2(ints: Vec) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 1).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 1).unwrap()).unwrap()).unwrap(), 1602 ); assert_eq!( - part2(parse(crate::util::data(2021, 1).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 1).unwrap()).unwrap()).unwrap(), 1633 ); } diff --git a/src/2021/10/mod.rs b/src/2021/10/mod.rs index ae311e8..4a51a9e 100644 --- a/src/2021/10/mod.rs +++ b/src/2021/10/mod.rs @@ -1,10 +1,10 @@ -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result> { - Ok(crate::util::parse::lines(fh)) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result> { + Ok(parse::lines(fh)) } -pub fn part1(lines: impl Iterator) -> anyhow::Result { +pub fn part1(lines: impl Iterator) -> Result { let mut total = 0; for line in lines { let mut open = vec![]; @@ -44,7 +44,7 @@ pub fn part1(lines: impl Iterator) -> anyhow::Result { Ok(total) } -pub fn part2(lines: impl Iterator) -> anyhow::Result { +pub fn part2(lines: impl Iterator) -> Result { let mut scores = vec![]; for line in lines { let mut open = vec![]; @@ -99,11 +99,11 @@ pub fn part2(lines: impl Iterator) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 10).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 10).unwrap()).unwrap()).unwrap(), 166191 ); assert_eq!( - part2(parse(crate::util::data(2021, 10).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 10).unwrap()).unwrap()).unwrap(), 1152088313 ); } diff --git a/src/2021/11/mod.rs b/src/2021/11/mod.rs index 38a1836..c153ddc 100644 --- a/src/2021/11/mod.rs +++ b/src/2021/11/mod.rs @@ -1,4 +1,4 @@ -use crate::util::grid::*; +use crate::prelude::*; fn iterate(grid: &mut Grid<(u8, bool)>) -> i64 { let mut flashes = 0; @@ -42,16 +42,14 @@ fn iterate(grid: &mut Grid<(u8, bool)>) -> i64 { flashes } -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok( - crate::util::parse::digit_grid(crate::util::parse::lines(fh)) - .indexed_cells() - .map(|((row, col), cell)| ((row, col), (*cell, false))) - .collect(), - ) +pub fn parse(fh: File) -> Result> { + Ok(parse::digit_grid(parse::lines(fh)) + .indexed_cells() + .map(|((row, col), cell)| ((row, col), (*cell, false))) + .collect()) } -pub fn part1(mut map: Grid<(u8, bool)>) -> anyhow::Result { +pub fn part1(mut map: Grid<(u8, bool)>) -> Result { let mut flashes = 0; for _ in 0..100 { flashes += iterate(&mut map); @@ -59,7 +57,7 @@ pub fn part1(mut map: Grid<(u8, bool)>) -> anyhow::Result { Ok(flashes) } -pub fn part2(mut map: Grid<(u8, bool)>) -> anyhow::Result { +pub fn part2(mut map: Grid<(u8, bool)>) -> Result { let mut step = 1; loop { let flashes = iterate(&mut map); @@ -74,11 +72,11 @@ pub fn part2(mut map: Grid<(u8, bool)>) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 11).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 11).unwrap()).unwrap()).unwrap(), 1673 ); assert_eq!( - part2(parse(crate::util::data(2021, 11).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 11).unwrap()).unwrap()).unwrap(), 279 ); } diff --git a/src/2021/12/mod.rs b/src/2021/12/mod.rs index 5907e67..7c7d601 100644 --- a/src/2021/12/mod.rs +++ b/src/2021/12/mod.rs @@ -1,9 +1,11 @@ +use crate::prelude::*; + fn small(s: &str) -> bool { s.bytes().all(|c| c.is_ascii_lowercase()) } fn single_small<'a>(path: impl Iterator) -> bool { - let mut set = std::collections::HashSet::new(); + let mut set = HashSet::new(); for s in path { if !small(s) { continue; @@ -17,10 +19,7 @@ fn single_small<'a>(path: impl Iterator) -> bool { } fn paths_from1<'a>( - graph: &'a std::collections::HashMap< - String, - std::collections::HashSet, - >, + graph: &'a HashMap>, path: &mut Vec<&'a str>, ) -> i64 { let mut total = 0; @@ -40,10 +39,7 @@ fn paths_from1<'a>( } fn paths_from2<'a>( - graph: &'a std::collections::HashMap< - String, - std::collections::HashSet, - >, + graph: &'a HashMap>, path: &mut Vec<&'a str>, ) -> i64 { let mut total = 0; @@ -68,53 +64,37 @@ fn paths_from2<'a>( total } -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result< - std::collections::HashMap>, -> { - let mut graph = std::collections::HashMap::new(); - for line in crate::util::parse::lines(fh) { +pub fn parse(fh: File) -> Result>> { + let mut graph = HashMap::new(); + for line in parse::lines(fh) { let nodes: Vec = line.split('-').map(|s| s.to_string()).collect(); - let edges = graph - .entry(nodes[0].clone()) - .or_insert_with(std::collections::HashSet::new); + let edges = + graph.entry(nodes[0].clone()).or_insert_with(HashSet::new); edges.insert(nodes[1].clone()); - let edges = graph - .entry(nodes[1].clone()) - .or_insert_with(std::collections::HashSet::new); + let edges = + graph.entry(nodes[1].clone()).or_insert_with(HashSet::new); edges.insert(nodes[0].clone()); } Ok(graph) } -pub fn part1( - graph: std::collections::HashMap< - String, - std::collections::HashSet, - >, -) -> anyhow::Result { +pub fn part1(graph: HashMap>) -> Result { Ok(paths_from1(&graph, &mut vec!["start"])) } -pub fn part2( - graph: std::collections::HashMap< - String, - std::collections::HashSet, - >, -) -> anyhow::Result { +pub fn part2(graph: HashMap>) -> Result { Ok(paths_from2(&graph, &mut vec!["start"])) } #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 12).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 12).unwrap()).unwrap()).unwrap(), 3230 ); assert_eq!( - part2(parse(crate::util::data(2021, 12).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 12).unwrap()).unwrap()).unwrap(), 83475 ); } diff --git a/src/2021/13/mod.rs b/src/2021/13/mod.rs index 717e032..b1a340f 100644 --- a/src/2021/13/mod.rs +++ b/src/2021/13/mod.rs @@ -1,7 +1,4 @@ -#![allow(clippy::collapsible_else_if)] -#![allow(clippy::comparison_chain)] - -use crate::util::grid::*; +use crate::prelude::*; #[derive(Default)] pub struct Paper { @@ -94,12 +91,10 @@ impl std::fmt::Display for Paper { } } -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result<(Paper, Vec<(bool, usize)>)> { +pub fn parse(fh: File) -> Result<(Paper, Vec<(bool, usize)>)> { let mut paper = Paper::default(); let mut folds = vec![]; - for line in crate::util::parse::lines(fh) { + for line in parse::lines(fh) { if line.is_empty() { continue; } @@ -118,16 +113,12 @@ pub fn parse( Ok((paper, folds)) } -pub fn part1( - (mut paper, folds): (Paper, Vec<(bool, usize)>), -) -> anyhow::Result { +pub fn part1((mut paper, folds): (Paper, Vec<(bool, usize)>)) -> Result { paper.fold(folds[0].0, folds[0].1); Ok(paper.total()) } -pub fn part2( - (mut paper, folds): (Paper, Vec<(bool, usize)>), -) -> anyhow::Result { +pub fn part2((mut paper, folds): (Paper, Vec<(bool, usize)>)) -> Result { for fold in folds { paper.fold(fold.0, fold.1); } @@ -139,11 +130,11 @@ pub fn part2( #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 13).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 13).unwrap()).unwrap()).unwrap(), 678 ); assert_eq!( - part2(parse(crate::util::data(2021, 13).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 13).unwrap()).unwrap()).unwrap(), 95 ); } diff --git a/src/2021/14/mod.rs b/src/2021/14/mod.rs index 97c2e9c..d018f53 100644 --- a/src/2021/14/mod.rs +++ b/src/2021/14/mod.rs @@ -1,7 +1,6 @@ -fn process( - polymer: &[u8], - rules: &std::collections::HashMap, u8>, -) -> Vec { +use crate::prelude::*; + +fn process(polymer: &[u8], rules: &HashMap, u8>) -> Vec { let mut insertions = vec![]; for (i, elements) in polymer.windows(2).enumerate() { for (pattern, insert) in rules { @@ -18,14 +17,12 @@ fn process( } #[allow(clippy::type_complexity)] -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result<(Vec, std::collections::HashMap, u8>)> { - let mut lines = crate::util::parse::lines(fh); +pub fn parse(fh: File) -> Result<(Vec, HashMap, u8>)> { + let mut lines = parse::lines(fh); let polymer = lines.next().unwrap(); lines.next(); - let mut rules = std::collections::HashMap::new(); + let mut rules = HashMap::new(); for line in lines { let rule: Vec<_> = line.split(" -> ").collect(); rules.insert(rule[0].as_bytes().to_vec(), rule[1].as_bytes()[0]); @@ -34,12 +31,12 @@ pub fn parse( } pub fn part1( - (mut polymer, rules): (Vec, std::collections::HashMap, u8>), -) -> anyhow::Result { + (mut polymer, rules): (Vec, HashMap, u8>), +) -> Result { for _ in 0..10 { polymer = process(&polymer, &rules); } - let mut elements = std::collections::HashMap::new(); + let mut elements = HashMap::new(); for element in polymer { let count = elements.entry(element).or_insert(0); *count += 1; @@ -48,16 +45,16 @@ pub fn part1( } pub fn part2( - (polymer, rules): (Vec, std::collections::HashMap, u8>), -) -> anyhow::Result { - let mut pairs = std::collections::HashMap::new(); + (polymer, rules): (Vec, HashMap, u8>), +) -> Result { + let mut pairs = HashMap::new(); for pair in polymer.windows(2) { let count = pairs.entry([pair[0], pair[1]]).or_insert(0); *count += 1; } for _ in 0..40 { - let mut next = std::collections::HashMap::new(); + let mut next = HashMap::new(); for (pair, count) in &mut pairs { let insert = rules[&pair[..]]; let new_pair1 = [pair[0], insert]; @@ -69,7 +66,7 @@ pub fn part2( } pairs = next; } - let mut elements = std::collections::HashMap::new(); + let mut elements = HashMap::new(); for (pair, count) in pairs { let element_count = elements.entry(pair[0]).or_insert(0); *element_count += count; @@ -88,11 +85,11 @@ pub fn part2( #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 14).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 14).unwrap()).unwrap()).unwrap(), 2874 ); assert_eq!( - part2(parse(crate::util::data(2021, 14).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 14).unwrap()).unwrap()).unwrap(), 5208377027195 ); } diff --git a/src/2021/15/mod.rs b/src/2021/15/mod.rs index e2d0656..efb2716 100644 --- a/src/2021/15/mod.rs +++ b/src/2021/15/mod.rs @@ -1,4 +1,4 @@ -use crate::util::grid::*; +use crate::prelude::*; fn dijkstra(map: &Grid) -> i64 { let mut to_visit: priority_queue::PriorityQueue<_, _> = (0..map.rows().0) @@ -40,17 +40,15 @@ fn dijkstra(map: &Grid) -> i64 { unreachable!() } -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok(crate::util::parse::digit_grid(crate::util::parse::lines( - fh, - ))) +pub fn parse(fh: File) -> Result> { + Ok(parse::digit_grid(parse::lines(fh))) } -pub fn part1(grid: Grid) -> anyhow::Result { +pub fn part1(grid: Grid) -> Result { Ok(dijkstra(&grid)) } -pub fn part2(grid: Grid) -> anyhow::Result { +pub fn part2(grid: Grid) -> Result { let mut large_grid = Grid::default(); large_grid.grow(Row(grid.rows().0 * 5), Col(grid.cols().0 * 5)); for lrow in 0..5 { @@ -71,11 +69,11 @@ pub fn part2(grid: Grid) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 15).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 15).unwrap()).unwrap()).unwrap(), 441 ); assert_eq!( - part2(parse(crate::util::data(2021, 15).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 15).unwrap()).unwrap()).unwrap(), 2849 ); } diff --git a/src/2021/16/mod.rs b/src/2021/16/mod.rs index 4a66fa8..2d32034 100644 --- a/src/2021/16/mod.rs +++ b/src/2021/16/mod.rs @@ -1,3 +1,5 @@ +use crate::prelude::*; + struct BitIter { byte: u8, pos: u8, @@ -113,7 +115,7 @@ impl Packet { } fn subpackets(&self) -> impl Iterator { - let mut to_return = std::collections::VecDeque::new(); + let mut to_return = VecDeque::new(); to_return.push_back(self); Subpackets { to_return } } @@ -173,7 +175,7 @@ fn read_varnum(bits: &mut impl Iterator) -> (u64, usize) { } struct Subpackets<'a> { - to_return: std::collections::VecDeque<&'a Packet>, + to_return: VecDeque<&'a Packet>, } impl<'a> Iterator for Subpackets<'a> { @@ -193,8 +195,8 @@ impl<'a> Iterator for Subpackets<'a> { } } -pub fn parse(fh: std::fs::File) -> anyhow::Result { - let line = crate::util::parse::lines(fh).next().unwrap(); +pub fn parse(fh: File) -> Result { + let line = parse::lines(fh).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() })); @@ -202,25 +204,25 @@ pub fn parse(fh: std::fs::File) -> anyhow::Result { Ok(packet) } -pub fn part1(packet: Packet) -> anyhow::Result { +pub fn part1(packet: Packet) -> Result { Ok(packet .subpackets() .map(|packet| i64::from(packet.version)) .sum()) } -pub fn part2(packet: Packet) -> anyhow::Result { +pub fn part2(packet: Packet) -> Result { Ok(packet.eval()) } #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 16).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 16).unwrap()).unwrap()).unwrap(), 979 ); assert_eq!( - part2(parse(crate::util::data(2021, 16).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 16).unwrap()).unwrap()).unwrap(), 277110354175 ); } diff --git a/src/2021/17/mod.rs b/src/2021/17/mod.rs index 806d9c1..2bc54fc 100644 --- a/src/2021/17/mod.rs +++ b/src/2021/17/mod.rs @@ -1,3 +1,5 @@ +use crate::prelude::*; + fn fire( mut xv: i64, mut yv: i64, @@ -23,10 +25,10 @@ fn fire( max_height = ypos; } match xv.cmp(&0) { - std::cmp::Ordering::Greater => { + Ordering::Greater => { xv -= 1; } - std::cmp::Ordering::Less => { + Ordering::Less => { xv += 1; } _ => {} @@ -36,16 +38,12 @@ fn fire( } pub fn parse( - fh: std::fs::File, -) -> anyhow::Result<( - std::ops::RangeInclusive, - std::ops::RangeInclusive, -)> { - let rx = regex::Regex::new( - r"target area: x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)", - ) - .unwrap(); - let line = crate::util::parse::lines(fh).next().unwrap(); + fh: File, +) -> Result<(std::ops::RangeInclusive, std::ops::RangeInclusive)> { + let rx = + Regex::new(r"target area: x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)") + .unwrap(); + let line = parse::lines(fh).next().unwrap(); let captures = rx.captures(&line).unwrap(); let xrange: std::ops::RangeInclusive = captures[1].parse().unwrap()..=captures[2].parse().unwrap(); @@ -59,7 +57,7 @@ pub fn part1( std::ops::RangeInclusive, std::ops::RangeInclusive, ), -) -> anyhow::Result { +) -> Result { let mut max_height = 0; for xv in *xrange.start().min(&0)..=*xrange.end().max(&0) { for yv in *yrange.start().min(&0) @@ -80,7 +78,7 @@ pub fn part2( std::ops::RangeInclusive, std::ops::RangeInclusive, ), -) -> anyhow::Result { +) -> Result { let mut count = 0; for xv in *xrange.start().min(&0)..=*xrange.end().max(&0) { for yv in *yrange.start().min(&0) @@ -97,11 +95,11 @@ pub fn part2( #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 17).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 17).unwrap()).unwrap()).unwrap(), 5886 ); assert_eq!( - part2(parse(crate::util::data(2021, 17).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 17).unwrap()).unwrap()).unwrap(), 1806 ); } diff --git a/src/2021/18/mod.rs b/src/2021/18/mod.rs index bd55f4d..56801ff 100644 --- a/src/2021/18/mod.rs +++ b/src/2021/18/mod.rs @@ -1,4 +1,4 @@ -#![allow(clippy::collapsible_else_if)] +use crate::prelude::*; #[derive(Clone)] pub enum Number { @@ -210,18 +210,16 @@ impl std::fmt::Display for Number { } } -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result> { - Ok(crate::util::parse::lines(fh).map(|line| Number::parse(&line))) +pub fn parse(fh: File) -> Result> { + Ok(parse::lines(fh).map(|line| Number::parse(&line))) } -pub fn part1(numbers: impl Iterator) -> anyhow::Result { +pub fn part1(numbers: impl Iterator) -> Result { let sum: Number = numbers.sum(); Ok(sum.magnitude()) } -pub fn part2(numbers: impl Iterator) -> anyhow::Result { +pub fn part2(numbers: impl Iterator) -> Result { let nums: Vec<_> = numbers.collect(); let mut max = 0; for (i, n1) in nums.iter().enumerate() { @@ -241,11 +239,11 @@ pub fn part2(numbers: impl Iterator) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 18).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 18).unwrap()).unwrap()).unwrap(), 3806 ); assert_eq!( - part2(parse(crate::util::data(2021, 18).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 18).unwrap()).unwrap()).unwrap(), 4727 ); } diff --git a/src/2021/19/mod.rs b/src/2021/19/mod.rs index b8a9368..28c402e 100644 --- a/src/2021/19/mod.rs +++ b/src/2021/19/mod.rs @@ -1,3 +1,5 @@ +use crate::prelude::*; + #[allow(clippy::type_complexity)] const ORIENTATIONS: &[&dyn Fn(Point) -> Point] = &[ &|p| Point::new(p.y, p.z, p.x), @@ -111,10 +113,7 @@ impl Scanner { } } - fn matches( - &self, - other: &std::collections::HashSet, - ) -> Option<(usize, Point)> { + fn matches(&self, other: &HashSet) -> Option<(usize, Point)> { for (i, beacons) in self.each_orientation().enumerate() { let mut offsets = vec![]; for beacon1 in beacons.clone() { @@ -123,7 +122,7 @@ impl Scanner { } } for offset in offsets { - let set1: std::collections::HashSet<_> = + let set1: HashSet<_> = beacons.iter().map(|beacon| *beacon + offset).collect(); let matches = set1.intersection(other).count(); if matches == 0 { @@ -178,13 +177,12 @@ impl Scan { } } -pub fn parse(fh: std::fs::File) -> anyhow::Result { - Ok(Scan::parse(crate::util::parse::lines(fh))) +pub fn parse(fh: File) -> Result { + Ok(Scan::parse(parse::lines(fh))) } -pub fn part1(scan: Scan) -> anyhow::Result { - let mut beacons: std::collections::HashSet = - std::collections::HashSet::new(); +pub fn part1(scan: Scan) -> Result { + let mut beacons: HashSet = HashSet::new(); let mut skip = None; for (i, scanner1) in scan.scanners().iter().enumerate() { for (j, scanner2) in scan.scanners().iter().enumerate().skip(i + 1) { @@ -229,9 +227,8 @@ pub fn part1(scan: Scan) -> anyhow::Result { Ok(beacons.len().try_into()?) } -pub fn part2(scan: Scan) -> anyhow::Result { - let mut beacons: std::collections::HashSet = - std::collections::HashSet::new(); +pub fn part2(scan: Scan) -> Result { + let mut beacons: HashSet = HashSet::new(); let mut skip = None; let mut offsets = vec![]; for (i, scanner1) in scan.scanners().iter().enumerate() { @@ -293,11 +290,11 @@ pub fn part2(scan: Scan) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 19).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 19).unwrap()).unwrap()).unwrap(), 338 ); assert_eq!( - part2(parse(crate::util::data(2021, 19).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 19).unwrap()).unwrap()).unwrap(), 9862 ); } diff --git a/src/2021/2/mod.rs b/src/2021/2/mod.rs index 6cac620..afbc3f0 100644 --- a/src/2021/2/mod.rs +++ b/src/2021/2/mod.rs @@ -1,13 +1,13 @@ +use crate::prelude::*; + pub enum Command { Forward(i64), Down(i64), Up(i64), } -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result> { - Ok(crate::util::parse::lines(fh).map(|line| { +pub fn parse(fh: File) -> Result> { + Ok(parse::lines(fh).map(|line| { if let Some(n) = line.strip_prefix("forward ") { Command::Forward(n.parse().unwrap()) } else if let Some(n) = line.strip_prefix("down ") { @@ -20,7 +20,7 @@ pub fn parse( })) } -pub fn part1(commands: impl Iterator) -> anyhow::Result { +pub fn part1(commands: impl Iterator) -> Result { let mut horizontal = 0; let mut vertical = 0; for command in commands { @@ -39,7 +39,7 @@ pub fn part1(commands: impl Iterator) -> anyhow::Result { Ok(horizontal * vertical) } -pub fn part2(commands: impl Iterator) -> anyhow::Result { +pub fn part2(commands: impl Iterator) -> Result { let mut aim = 0; let mut horizontal = 0; let mut vertical = 0; @@ -63,11 +63,11 @@ pub fn part2(commands: impl Iterator) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 2).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 2).unwrap()).unwrap()).unwrap(), 1694130 ); assert_eq!( - part2(parse(crate::util::data(2021, 2).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 2).unwrap()).unwrap()).unwrap(), 1698850445 ); } diff --git a/src/2021/20/mod.rs b/src/2021/20/mod.rs index 2905880..e3c3287 100644 --- a/src/2021/20/mod.rs +++ b/src/2021/20/mod.rs @@ -1,4 +1,4 @@ -use crate::util::grid::*; +use crate::prelude::*; pub struct Image { algorithm: Vec, @@ -64,8 +64,8 @@ impl Image { } } -pub fn parse(fh: std::fs::File) -> anyhow::Result { - let mut lines = crate::util::parse::lines(fh); +pub fn parse(fh: File) -> Result { + let mut lines = parse::lines(fh); let algorithm = lines.next().unwrap(); let algorithm: Vec<_> = algorithm .as_bytes() @@ -77,18 +77,18 @@ pub fn parse(fh: std::fs::File) -> anyhow::Result { }) .collect(); lines.next().unwrap(); - let map = crate::util::parse::bool_grid(lines, b'#', b'.'); + let map = parse::bool_grid(lines, b'#', b'.'); Ok(Image::new(algorithm, map)) } -pub fn part1(mut image: Image) -> anyhow::Result { +pub fn part1(mut image: Image) -> Result { for _ in 0..2 { image.enhance(); } Ok(image.count_true()) } -pub fn part2(mut image: Image) -> anyhow::Result { +pub fn part2(mut image: Image) -> Result { for _ in 0..50 { image.enhance(); } @@ -98,11 +98,11 @@ pub fn part2(mut image: Image) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 20).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 20).unwrap()).unwrap()).unwrap(), 5306 ); assert_eq!( - part2(parse(crate::util::data(2021, 20).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 20).unwrap()).unwrap()).unwrap(), 17497 ); } diff --git a/src/2021/21/mod.rs b/src/2021/21/mod.rs index beff797..21fbedd 100644 --- a/src/2021/21/mod.rs +++ b/src/2021/21/mod.rs @@ -1,3 +1,5 @@ +use crate::prelude::*; + #[derive(Clone)] pub struct Game { p1_pos: i64, @@ -164,8 +166,8 @@ impl Game { } } -pub fn parse(fh: std::fs::File) -> anyhow::Result { - let mut lines = crate::util::parse::lines(fh); +pub fn parse(fh: File) -> Result { + let mut lines = parse::lines(fh); let p1 = lines .next() .unwrap() @@ -183,7 +185,7 @@ pub fn parse(fh: std::fs::File) -> anyhow::Result { Ok(Game::new(p1, p2)) } -pub fn part1(mut game: Game) -> anyhow::Result { +pub fn part1(mut game: Game) -> Result { let mut p1 = true; loop { if let Some(value) = game.value(1000) { @@ -195,7 +197,7 @@ pub fn part1(mut game: Game) -> anyhow::Result { } } -pub fn part2(game: Game) -> anyhow::Result { +pub fn part2(game: Game) -> Result { let (p1, p2) = game.run_dirac(true); Ok(p1.max(p2)) } @@ -203,11 +205,11 @@ pub fn part2(game: Game) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 21).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 21).unwrap()).unwrap()).unwrap(), 1004670 ); assert_eq!( - part2(parse(crate::util::data(2021, 21).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 21).unwrap()).unwrap()).unwrap(), 492043106122795 ); } diff --git a/src/2021/3/mod.rs b/src/2021/3/mod.rs index 11b9c8d..a512cac 100644 --- a/src/2021/3/mod.rs +++ b/src/2021/3/mod.rs @@ -1,10 +1,10 @@ -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result> { - Ok(crate::util::parse::lines(fh)) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result> { + Ok(parse::lines(fh)) } -pub fn part1(lines: impl Iterator) -> anyhow::Result { +pub fn part1(lines: impl Iterator) -> Result { let (total_lines, by_pos) = pos_counts(lines)?; let gamma: String = by_pos .iter() @@ -17,7 +17,7 @@ pub fn part1(lines: impl Iterator) -> anyhow::Result { Ok(bin_str_to_int(&gamma) * bin_str_to_int(&epsilon)) } -pub fn part2(lines: impl Iterator) -> anyhow::Result { +pub fn part2(lines: impl Iterator) -> Result { let mut oxygen: Vec<_> = lines.collect(); let mut co2 = oxygen.clone(); @@ -61,8 +61,8 @@ pub fn part2(lines: impl Iterator) -> anyhow::Result { } fn pos_counts( - lines: impl std::iter::Iterator, -) -> anyhow::Result<(i64, Vec)> { + lines: impl Iterator, +) -> Result<(i64, Vec)> { let mut by_pos = vec![]; let mut total_lines = 0; for line in lines { @@ -92,11 +92,11 @@ fn bin_str_to_int(s: &str) -> i64 { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 3).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 3).unwrap()).unwrap()).unwrap(), 3009600 ); assert_eq!( - part2(parse(crate::util::data(2021, 3).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 3).unwrap()).unwrap()).unwrap(), 6940518 ); } diff --git a/src/2021/4/mod.rs b/src/2021/4/mod.rs index c3bd7d8..8dd35c1 100644 --- a/src/2021/4/mod.rs +++ b/src/2021/4/mod.rs @@ -1,4 +1,4 @@ -use std::io::BufRead as _; +use crate::prelude::*; #[derive(Debug)] struct Board { @@ -57,7 +57,7 @@ pub struct Game { } impl Game { - fn parse(input: T) -> anyhow::Result { + fn parse(input: T) -> Result { let mut input = std::io::BufReader::new(input); let mut line = String::new(); input.read_line(&mut line)?; @@ -133,34 +133,34 @@ impl Game { } } -pub fn parse(fh: std::fs::File) -> anyhow::Result { +pub fn parse(fh: File) -> Result { Game::parse(fh) } -pub fn part1(game: Game) -> anyhow::Result { +pub fn part1(game: Game) -> Result { if let Some((n, board)) = game.find_first_winner() { Ok((n as i64) * board.value()) } else { - anyhow::bail!("couldn't find winner") + bail!("couldn't find winner") } } -pub fn part2(game: Game) -> anyhow::Result { +pub fn part2(game: Game) -> Result { if let Some((n, board)) = game.find_last_winner() { Ok((n as i64) * board.value()) } else { - anyhow::bail!("couldn't find winner") + bail!("couldn't find winner") } } #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 4).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 4).unwrap()).unwrap()).unwrap(), 2745 ); assert_eq!( - part2(parse(crate::util::data(2021, 4).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 4).unwrap()).unwrap()).unwrap(), 6594 ); } diff --git a/src/2021/5/mod.rs b/src/2021/5/mod.rs index 2e81e34..f33a9c8 100644 --- a/src/2021/5/mod.rs +++ b/src/2021/5/mod.rs @@ -1,4 +1,4 @@ -use crate::util::grid::*; +use crate::prelude::*; #[derive(Default, Clone)] struct Map { @@ -72,11 +72,9 @@ impl Map { } } -pub fn parse( - fh: std::fs::File, -) -> anyhow::Result>> { - let rx = regex::Regex::new("^(\\d+),(\\d+) -> (\\d+),(\\d+)$")?; - Ok(crate::util::parse::lines(fh).map(move |line| { +pub fn parse(fh: File) -> Result>> { + let rx = Regex::new("^(\\d+),(\\d+) -> (\\d+),(\\d+)$")?; + Ok(parse::lines(fh).map(move |line| { rx.captures(&line) .unwrap() .iter() @@ -87,9 +85,7 @@ pub fn parse( })) } -pub fn part1( - coords: impl Iterator>, -) -> anyhow::Result { +pub fn part1(coords: impl Iterator>) -> Result { let mut map = Map::default(); for nums in coords { let _ = map.mark_horizontal(nums[0], nums[1], nums[2], nums[3]) @@ -98,9 +94,7 @@ pub fn part1( Ok(map.count_overlapping().try_into()?) } -pub fn part2( - coords: impl Iterator>, -) -> anyhow::Result { +pub fn part2(coords: impl Iterator>) -> Result { let mut map = Map::default(); for nums in coords { let _ = map.mark_horizontal(nums[0], nums[1], nums[2], nums[3]) @@ -114,11 +108,11 @@ pub fn part2( #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 5).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 5).unwrap()).unwrap()).unwrap(), 6311 ); assert_eq!( - part2(parse(crate::util::data(2021, 5).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 5).unwrap()).unwrap()).unwrap(), 19929 ); } diff --git a/src/2021/6/mod.rs b/src/2021/6/mod.rs index 353aa8f..e2dafff 100644 --- a/src/2021/6/mod.rs +++ b/src/2021/6/mod.rs @@ -1,11 +1,10 @@ -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok( - crate::util::parse::ints(crate::util::parse::split(fh, b',')) - .collect(), - ) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result> { + Ok(parse::ints(parse::split(fh, b',')).collect()) } -pub fn part1(mut fishes: Vec) -> anyhow::Result { +pub fn part1(mut fishes: Vec) -> Result { for _ in 0..80 { let mut new = 0; for fish in fishes.iter_mut() { @@ -21,8 +20,8 @@ pub fn part1(mut fishes: Vec) -> anyhow::Result { Ok(fishes.len().try_into()?) } -pub fn part2(fishes: Vec) -> anyhow::Result { - let mut by_age = std::collections::VecDeque::new(); +pub fn part2(fishes: Vec) -> Result { + let mut by_age = VecDeque::new(); by_age.resize(9, 0); for fish in fishes { by_age[fish as usize] += 1; @@ -38,11 +37,11 @@ pub fn part2(fishes: Vec) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 6).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 6).unwrap()).unwrap()).unwrap(), 379114 ); assert_eq!( - part2(parse(crate::util::data(2021, 6).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 6).unwrap()).unwrap()).unwrap(), 1702631502303 ); } diff --git a/src/2021/7/mod.rs b/src/2021/7/mod.rs index ce942dc..8ddd46e 100644 --- a/src/2021/7/mod.rs +++ b/src/2021/7/mod.rs @@ -1,11 +1,10 @@ -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok( - crate::util::parse::ints(crate::util::parse::split(fh, b',')) - .collect(), - ) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result> { + Ok(parse::ints(parse::split(fh, b',')).collect()) } -pub fn part1(crabs: Vec) -> anyhow::Result { +pub fn part1(crabs: Vec) -> Result { Ok((0..=crabs.iter().copied().max().unwrap()) .map(|start| { crabs.iter().copied().map(|crab| (crab - start).abs()).sum() @@ -14,7 +13,7 @@ pub fn part1(crabs: Vec) -> anyhow::Result { .unwrap()) } -pub fn part2(crabs: Vec) -> anyhow::Result { +pub fn part2(crabs: Vec) -> Result { Ok((0..=crabs.iter().copied().max().unwrap()) .map(|start| { crabs @@ -33,11 +32,11 @@ pub fn part2(crabs: Vec) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 7).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 7).unwrap()).unwrap()).unwrap(), 333755 ); assert_eq!( - part2(parse(crate::util::data(2021, 7).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 7).unwrap()).unwrap()).unwrap(), 94017638 ); } diff --git a/src/2021/8/mod.rs b/src/2021/8/mod.rs index 8544247..70d71a0 100644 --- a/src/2021/8/mod.rs +++ b/src/2021/8/mod.rs @@ -1,7 +1,9 @@ +use crate::prelude::*; + pub fn parse( - fh: std::fs::File, -) -> anyhow::Result, Vec)>> { - Ok(crate::util::parse::lines(fh).map(|line| { + fh: File, +) -> Result, Vec)>> { + Ok(parse::lines(fh).map(|line| { let parts: Vec<_> = line.split(" | ").collect(); ( parts[0].split(' ').map(str::to_string).collect(), @@ -12,7 +14,7 @@ pub fn parse( pub fn part1( lines: impl Iterator, Vec)>, -) -> anyhow::Result { +) -> Result { let mut count = 0i64; for (_, output) in lines { let line_count: i64 = output @@ -35,31 +37,31 @@ pub fn part1( // 66 pub fn part2( lines: impl Iterator, Vec)>, -) -> anyhow::Result { +) -> Result { let mut total = 0; for (numbers, output) in lines { let mut segments = ['x'; 7]; // zero: 6 let one = numbers.iter().find(|s| s.len() == 2).unwrap(); - let one: std::collections::HashSet = one.chars().collect(); + let one: HashSet = one.chars().collect(); // two: 5 // three: 5 let four = numbers.iter().find(|s| s.len() == 4).unwrap(); - let four: std::collections::HashSet = four.chars().collect(); + let four: HashSet = four.chars().collect(); // five: 5 // six: 6 let seven = numbers.iter().find(|s| s.len() == 3).unwrap(); - let seven: std::collections::HashSet = seven.chars().collect(); + let seven: HashSet = seven.chars().collect(); let eight = numbers.iter().find(|s| s.len() == 7).unwrap(); - let eight: std::collections::HashSet = eight.chars().collect(); + let eight: HashSet = eight.chars().collect(); // nine: 6 let mut length_five: Vec<_> = numbers .iter() .filter_map(|s| { if s.len() == 5 { - Some(s.chars().collect::>()) + Some(s.chars().collect::>()) } else { None } @@ -69,7 +71,7 @@ pub fn part2( .iter() .filter_map(|s| { if s.len() == 6 { - Some(s.chars().collect::>()) + Some(s.chars().collect::>()) } else { None } @@ -162,7 +164,7 @@ pub fn part2( let value: Vec<_> = output .iter() - .map(|s| s.chars().collect::>()) + .map(|s| s.chars().collect::>()) .map(|set| numbers.iter().position(|num| &set == num).unwrap()) .collect(); let value = @@ -175,11 +177,11 @@ pub fn part2( #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 8).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 8).unwrap()).unwrap()).unwrap(), 355 ); assert_eq!( - part2(parse(crate::util::data(2021, 8).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 8).unwrap()).unwrap()).unwrap(), 983030 ); } diff --git a/src/2021/9/mod.rs b/src/2021/9/mod.rs index 184c245..a1c343b 100644 --- a/src/2021/9/mod.rs +++ b/src/2021/9/mod.rs @@ -1,12 +1,10 @@ -use crate::util::grid::*; +use crate::prelude::*; -pub fn parse(fh: std::fs::File) -> anyhow::Result> { - Ok(crate::util::parse::digit_grid(crate::util::parse::lines( - fh, - ))) +pub fn parse(fh: File) -> Result> { + Ok(parse::digit_grid(parse::lines(fh))) } -pub fn part1(map: Grid) -> anyhow::Result { +pub fn part1(map: Grid) -> Result { let mut risk = 0; for ((row, col), pos) in map.indexed_cells() { if map @@ -20,7 +18,7 @@ pub fn part1(map: Grid) -> anyhow::Result { Ok(risk) } -pub fn part2(map: Grid) -> anyhow::Result { +pub fn part2(map: Grid) -> Result { let mut low = vec![]; for ((row, col), pos) in map.indexed_cells() { if map @@ -63,11 +61,11 @@ pub fn part2(map: Grid) -> anyhow::Result { #[test] fn test() { assert_eq!( - part1(parse(crate::util::data(2021, 9).unwrap()).unwrap()).unwrap(), + part1(parse(parse::data(2021, 9).unwrap()).unwrap()).unwrap(), 570 ); assert_eq!( - part2(parse(crate::util::data(2021, 9).unwrap()).unwrap()).unwrap(), + part2(parse(parse::data(2021, 9).unwrap()).unwrap()).unwrap(), 899392 ); } diff --git a/src/2021/mod.rs b/src/2021/mod.rs index 4c5e1d6..4931707 100644 --- a/src/2021/mod.rs +++ b/src/2021/mod.rs @@ -1,3 +1,5 @@ +use crate::prelude::*; + #[path = "1/mod.rs"] mod day1; #[path = "2/mod.rs"] @@ -42,51 +44,51 @@ mod day20; mod day21; // NEXT MOD -pub fn run(day: u8, puzzle: u8) -> anyhow::Result { +pub fn run(day: u8, puzzle: u8) -> Result { match (day, puzzle) { - (1, 1) => day1::part1(day1::parse(crate::util::data(2021, 1)?)?), - (1, 2) => day1::part2(day1::parse(crate::util::data(2021, 1)?)?), - (2, 1) => day2::part1(day2::parse(crate::util::data(2021, 2)?)?), - (2, 2) => day2::part2(day2::parse(crate::util::data(2021, 2)?)?), - (3, 1) => day3::part1(day3::parse(crate::util::data(2021, 3)?)?), - (3, 2) => day3::part2(day3::parse(crate::util::data(2021, 3)?)?), - (4, 1) => day4::part1(day4::parse(crate::util::data(2021, 4)?)?), - (4, 2) => day4::part2(day4::parse(crate::util::data(2021, 4)?)?), - (5, 1) => day5::part1(day5::parse(crate::util::data(2021, 5)?)?), - (5, 2) => day5::part2(day5::parse(crate::util::data(2021, 5)?)?), - (6, 1) => day6::part1(day6::parse(crate::util::data(2021, 6)?)?), - (6, 2) => day6::part2(day6::parse(crate::util::data(2021, 6)?)?), - (7, 1) => day7::part1(day7::parse(crate::util::data(2021, 7)?)?), - (7, 2) => day7::part2(day7::parse(crate::util::data(2021, 7)?)?), - (8, 1) => day8::part1(day8::parse(crate::util::data(2021, 8)?)?), - (8, 2) => day8::part2(day8::parse(crate::util::data(2021, 8)?)?), - (9, 1) => day9::part1(day9::parse(crate::util::data(2021, 9)?)?), - (9, 2) => day9::part2(day9::parse(crate::util::data(2021, 9)?)?), - (10, 1) => day10::part1(day10::parse(crate::util::data(2021, 10)?)?), - (10, 2) => day10::part2(day10::parse(crate::util::data(2021, 10)?)?), - (11, 1) => day11::part1(day11::parse(crate::util::data(2021, 11)?)?), - (11, 2) => day11::part2(day11::parse(crate::util::data(2021, 11)?)?), - (12, 1) => day12::part1(day12::parse(crate::util::data(2021, 12)?)?), - (12, 2) => day12::part2(day12::parse(crate::util::data(2021, 12)?)?), - (13, 1) => day13::part1(day13::parse(crate::util::data(2021, 13)?)?), - (13, 2) => day13::part2(day13::parse(crate::util::data(2021, 13)?)?), - (14, 1) => day14::part1(day14::parse(crate::util::data(2021, 14)?)?), - (14, 2) => day14::part2(day14::parse(crate::util::data(2021, 14)?)?), - (15, 1) => day15::part1(day15::parse(crate::util::data(2021, 15)?)?), - (15, 2) => day15::part2(day15::parse(crate::util::data(2021, 15)?)?), - (16, 1) => day16::part1(day16::parse(crate::util::data(2021, 16)?)?), - (16, 2) => day16::part2(day16::parse(crate::util::data(2021, 16)?)?), - (17, 1) => day17::part1(day17::parse(crate::util::data(2021, 17)?)?), - (17, 2) => day17::part2(day17::parse(crate::util::data(2021, 17)?)?), - (18, 1) => day18::part1(day18::parse(crate::util::data(2021, 18)?)?), - (18, 2) => day18::part2(day18::parse(crate::util::data(2021, 18)?)?), - (19, 1) => day19::part1(day19::parse(crate::util::data(2021, 19)?)?), - (19, 2) => day19::part2(day19::parse(crate::util::data(2021, 19)?)?), - (20, 1) => day20::part1(day20::parse(crate::util::data(2021, 20)?)?), - (20, 2) => day20::part2(day20::parse(crate::util::data(2021, 20)?)?), - (21, 1) => day21::part1(day21::parse(crate::util::data(2021, 21)?)?), - (21, 2) => day21::part2(day21::parse(crate::util::data(2021, 21)?)?), + (1, 1) => day1::part1(day1::parse(parse::data(2021, 1)?)?), + (1, 2) => day1::part2(day1::parse(parse::data(2021, 1)?)?), + (2, 1) => day2::part1(day2::parse(parse::data(2021, 2)?)?), + (2, 2) => day2::part2(day2::parse(parse::data(2021, 2)?)?), + (3, 1) => day3::part1(day3::parse(parse::data(2021, 3)?)?), + (3, 2) => day3::part2(day3::parse(parse::data(2021, 3)?)?), + (4, 1) => day4::part1(day4::parse(parse::data(2021, 4)?)?), + (4, 2) => day4::part2(day4::parse(parse::data(2021, 4)?)?), + (5, 1) => day5::part1(day5::parse(parse::data(2021, 5)?)?), + (5, 2) => day5::part2(day5::parse(parse::data(2021, 5)?)?), + (6, 1) => day6::part1(day6::parse(parse::data(2021, 6)?)?), + (6, 2) => day6::part2(day6::parse(parse::data(2021, 6)?)?), + (7, 1) => day7::part1(day7::parse(parse::data(2021, 7)?)?), + (7, 2) => day7::part2(day7::parse(parse::data(2021, 7)?)?), + (8, 1) => day8::part1(day8::parse(parse::data(2021, 8)?)?), + (8, 2) => day8::part2(day8::parse(parse::data(2021, 8)?)?), + (9, 1) => day9::part1(day9::parse(parse::data(2021, 9)?)?), + (9, 2) => day9::part2(day9::parse(parse::data(2021, 9)?)?), + (10, 1) => day10::part1(day10::parse(parse::data(2021, 10)?)?), + (10, 2) => day10::part2(day10::parse(parse::data(2021, 10)?)?), + (11, 1) => day11::part1(day11::parse(parse::data(2021, 11)?)?), + (11, 2) => day11::part2(day11::parse(parse::data(2021, 11)?)?), + (12, 1) => day12::part1(day12::parse(parse::data(2021, 12)?)?), + (12, 2) => day12::part2(day12::parse(parse::data(2021, 12)?)?), + (13, 1) => day13::part1(day13::parse(parse::data(2021, 13)?)?), + (13, 2) => day13::part2(day13::parse(parse::data(2021, 13)?)?), + (14, 1) => day14::part1(day14::parse(parse::data(2021, 14)?)?), + (14, 2) => day14::part2(day14::parse(parse::data(2021, 14)?)?), + (15, 1) => day15::part1(day15::parse(parse::data(2021, 15)?)?), + (15, 2) => day15::part2(day15::parse(parse::data(2021, 15)?)?), + (16, 1) => day16::part1(day16::parse(parse::data(2021, 16)?)?), + (16, 2) => day16::part2(day16::parse(parse::data(2021, 16)?)?), + (17, 1) => day17::part1(day17::parse(parse::data(2021, 17)?)?), + (17, 2) => day17::part2(day17::parse(parse::data(2021, 17)?)?), + (18, 1) => day18::part1(day18::parse(parse::data(2021, 18)?)?), + (18, 2) => day18::part2(day18::parse(parse::data(2021, 18)?)?), + (19, 1) => day19::part1(day19::parse(parse::data(2021, 19)?)?), + (19, 2) => day19::part2(day19::parse(parse::data(2021, 19)?)?), + (20, 1) => day20::part1(day20::parse(parse::data(2021, 20)?)?), + (20, 2) => day20::part2(day20::parse(parse::data(2021, 20)?)?), + (21, 1) => day21::part1(day21::parse(parse::data(2021, 21)?)?), + (21, 2) => day21::part2(day21::parse(parse::data(2021, 21)?)?), // NEXT PART - _ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)), + _ => Err(anyhow!("unknown puzzle {}-{}", day, puzzle)), } } diff --git a/src/main.rs b/src/main.rs index 406d78a..9892123 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![allow(clippy::collapsible_if)] +mod prelude; mod util; #[path = "2020/mod.rs"] diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 0000000..d9263f5 --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,10 @@ +pub use crate::util::grid::{Col, Grid, Row}; +pub use crate::util::parse; + +pub use std::cmp::Ordering; +pub use std::collections::{HashMap, HashSet, VecDeque}; +pub use std::fs::File; +pub use std::io::{BufRead as _, Read as _}; + +pub use anyhow::{anyhow, bail, Context as _, Error, Result}; +pub use regex::Regex; diff --git a/src/util.rs b/src/util.rs index e5316bf..d3687f2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,2 @@ pub mod grid; pub mod parse; - -pub fn data(year: u16, day: u16) -> anyhow::Result { - std::fs::File::open(format!("data/{}/{}.txt", year, day)) - .map_err(|e| anyhow::anyhow!(e)) -} diff --git a/src/util/parse.rs b/src/util/parse.rs index 054952a..903ad2f 100644 --- a/src/util/parse.rs +++ b/src/util/parse.rs @@ -1,11 +1,15 @@ -use std::io::{BufRead as _, Read as _}; +use crate::prelude::*; -pub fn lines(fh: std::fs::File) -> impl Iterator { +pub fn data(year: u16, day: u16) -> Result { + File::open(format!("data/{}/{}.txt", year, day)).map_err(|e| anyhow!(e)) +} + +pub fn lines(fh: File) -> impl Iterator { let fh = std::io::BufReader::new(fh); fh.lines().map(|res| res.unwrap()) } -pub fn split(fh: std::fs::File, sep: u8) -> impl Iterator { +pub fn split(fh: File, sep: u8) -> impl Iterator { let fh = std::io::BufReader::new(fh); fh.split(sep) .map(|res| String::from_utf8(res.unwrap()).unwrap()) @@ -15,11 +19,11 @@ pub fn ints(iter: impl Iterator) -> impl Iterator { iter.map(|s| s.trim().parse().unwrap()) } -pub fn bytes(fh: std::fs::File) -> impl Iterator { +pub fn bytes(fh: File) -> impl Iterator { fh.bytes().map(|res| res.unwrap()) } -pub fn string(fh: std::fs::File) -> String { +pub fn string(fh: File) -> String { let bytes: Vec<_> = bytes(fh).collect(); String::from_utf8(bytes).unwrap() } @@ -28,7 +32,7 @@ pub fn bool_grid( lines: impl Iterator, t: u8, f: u8, -) -> crate::util::grid::Grid { +) -> Grid { lines .map(|s| { s.as_bytes() @@ -48,9 +52,7 @@ pub fn bool_grid( .collect() } -pub fn digit_grid( - lines: impl Iterator, -) -> crate::util::grid::Grid { +pub fn digit_grid(lines: impl Iterator) -> Grid { lines .map(|s| { s.as_bytes() -- cgit v1.2.3-54-g00ecf