diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-21 04:18:01 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-21 04:18:01 -0500 |
commit | 3a5398399221c7986a69c6cc90f7bb70669f39a8 (patch) | |
tree | ec4b06df956dbc2aab7c4a2d25a47a65fd4ac81e /src/2020 | |
parent | 5d64e313a46b002205ecfe854555d1e8c94572e0 (diff) | |
download | advent-of-code-3a5398399221c7986a69c6cc90f7bb70669f39a8.tar.gz advent-of-code-3a5398399221c7986a69c6cc90f7bb70669f39a8.zip |
prelude to reduce some typing
Diffstat (limited to 'src/2020')
-rw-r--r-- | src/2020/1/mod.rs | 18 | ||||
-rw-r--r-- | src/2020/2/mod.rs | 21 | ||||
-rw-r--r-- | src/2020/3/mod.rs | 22 | ||||
-rw-r--r-- | src/2020/4/mod.rs | 28 | ||||
-rw-r--r-- | src/2020/5/mod.rs | 29 | ||||
-rw-r--r-- | src/2020/6/mod.rs | 24 | ||||
-rw-r--r-- | src/2020/7/mod.rs | 34 | ||||
-rw-r--r-- | src/2020/8/mod.rs | 34 | ||||
-rw-r--r-- | src/2020/9/mod.rs | 22 | ||||
-rw-r--r-- | src/2020/mod.rs | 42 |
10 files changed, 129 insertions, 145 deletions
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<Vec<i64>> { - Ok(crate::util::parse::ints(crate::util::parse::lines(fh)).collect()) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result<Vec<i64>> { + Ok(parse::ints(parse::lines(fh)).collect()) } -pub fn part1(ints: Vec<i64>) -> anyhow::Result<i64> { +pub fn part1(ints: Vec<i64>) -> Result<i64> { for i in &ints { for j in &ints { if i + j == 2020 { @@ -10,10 +12,10 @@ pub fn part1(ints: Vec<i64>) -> anyhow::Result<i64> { } } } - Err(anyhow::anyhow!("no numbers summing to 2020 found")) + Err(anyhow!("no numbers summing to 2020 found")) } -pub fn part2(ints: Vec<i64>) -> anyhow::Result<i64> { +pub fn part2(ints: Vec<i64>) -> Result<i64> { for i in &ints { for j in &ints { for k in &ints { @@ -23,17 +25,17 @@ pub fn part2(ints: Vec<i64>) -> anyhow::Result<i64> { } } } - 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<Self> { - let rx = regex::Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap(); + fn parse(line: &str) -> Result<Self> { + 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<impl Iterator<Item = Line>> { - Ok(crate::util::parse::lines(fh).map(|line| Line::parse(&line).unwrap())) +pub fn parse(fh: File) -> Result<impl Iterator<Item = Line>> { + Ok(parse::lines(fh).map(|line| Line::parse(&line).unwrap())) } -pub fn part1(lines: impl Iterator<Item = Line>) -> anyhow::Result<i64> { +pub fn part1(lines: impl Iterator<Item = Line>) -> Result<i64> { let count = lines.filter(|l| l.valid_part_1()).count(); Ok(count.try_into()?) } -pub fn part2(lines: impl Iterator<Item = Line>) -> anyhow::Result<i64> { +pub fn part2(lines: impl Iterator<Item = Line>) -> Result<i64> { let count = lines.filter(|l| l.valid_part_2()).count(); Ok(count.try_into()?) } @@ -70,11 +67,11 @@ pub fn part2(lines: impl Iterator<Item = Line>) -> anyhow::Result<i64> { #[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<bool>, @@ -13,7 +13,7 @@ impl Map { self.grid.rows().0 } - fn tree_at(&self, row: Row, col: Col) -> anyhow::Result<bool> { + fn tree_at(&self, row: Row, col: Col) -> Result<bool> { // 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<i64> { + ) -> Result<i64> { 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<Map> { - Ok(Map::new(crate::util::parse::bool_grid( - crate::util::parse::lines(fh), - b'#', - b'.', - ))) +pub fn parse(fh: File) -> Result<Map> { + Ok(Map::new(parse::bool_grid(parse::lines(fh), b'#', b'.'))) } -pub fn part1(map: Map) -> anyhow::Result<i64> { +pub fn part1(map: Map) -> Result<i64> { map.trees_for_slope(1, 3) } -pub fn part2(map: Map) -> anyhow::Result<i64> { +pub fn part2(map: Map) -> Result<i64> { 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<i64> { #[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<Vec<std::collections::HashMap<String, String>>> { +pub fn parse(fh: File) -> Result<Vec<HashMap<String, String>>> { 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<std::collections::HashMap<String, String>>, -) -> anyhow::Result<i64> { +pub fn part1(passports: Vec<HashMap<String, String>>) -> Result<i64> { 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<std::collections::HashMap<String, String>>, -) -> anyhow::Result<i64> { +pub fn part2(passports: Vec<HashMap<String, String>>) -> Result<i64> { 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<bool> { +fn validate(key: &str, val: &str) -> Result<bool> { match key { "byr" => match val.parse::<i32>() { Ok(year) => Ok((1920..=2002).contains(&year)), @@ -127,18 +121,18 @@ fn validate(key: &str, val: &str) -> anyhow::Result<bool> { == val .matches(|c: char| c.is_ascii_digit()) .collect::<String>()), - _ => 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<impl Iterator<Item = i64>> { - Ok(crate::util::parse::lines(fh).map(|line| seat_id(&line).unwrap())) +pub fn parse(fh: File) -> Result<impl Iterator<Item = i64>> { + Ok(parse::lines(fh).map(|line| seat_id(&line).unwrap())) } -pub fn part1(ids: impl Iterator<Item = i64>) -> anyhow::Result<i64> { +pub fn part1(ids: impl Iterator<Item = i64>) -> Result<i64> { let mut max = 0; for id in ids { if id > max { @@ -15,7 +14,7 @@ pub fn part1(ids: impl Iterator<Item = i64>) -> anyhow::Result<i64> { Ok(max) } -pub fn part2(ids: impl Iterator<Item = i64>) -> anyhow::Result<i64> { +pub fn part2(ids: impl Iterator<Item = i64>) -> Result<i64> { 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<Item = i64>) -> anyhow::Result<i64> { .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<i64> { +fn seat_id(desc: &str) -> Result<i64> { 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<i64> { '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<i64> { '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<i64> { #[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<impl Iterator<Item = String>> { - Ok(crate::util::parse::lines(fh)) +use crate::prelude::*; + +pub fn parse(fh: File) -> Result<impl Iterator<Item = String>> { + Ok(parse::lines(fh)) } -pub fn part1(lines: impl Iterator<Item = String>) -> anyhow::Result<i64> { - let mut yes = std::collections::HashSet::new(); +pub fn part1(lines: impl Iterator<Item = String>) -> Result<i64> { + 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<Item = String>) -> anyhow::Result<i64> { Ok(total) } -pub fn part2(lines: impl Iterator<Item = String>) -> anyhow::Result<i64> { - let mut yes = std::collections::HashSet::new(); +pub fn part2(lines: impl Iterator<Item = String>) -> Result<i64> { + let mut yes = HashSet::new(); for c in 'a'..='z' { yes.insert(c); } @@ -30,7 +30,7 @@ pub fn part2(lines: impl Iterator<Item = String>) -> anyhow::Result<i64> { 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<Item = String>) -> anyhow::Result<i64> { #[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<String, Vec<(i64, String)>>; +type Graph = HashMap<String, Vec<(i64, String)>>; -pub fn parse(fh: std::fs::File) -> anyhow::Result<Graph> { - let input = crate::util::parse::string(fh); +pub fn parse(fh: File) -> Result<Graph> { + 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<Graph> { Ok(graph) } -pub fn part1(graph: Graph) -> anyhow::Result<i64> { +pub fn part1(graph: Graph) -> Result<i64> { 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<i64> { Ok(colors) } -pub fn part2(graph: Graph) -> anyhow::Result<i64> { +pub fn part2(graph: Graph) -> Result<i64> { // 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::<anyhow::Result<_>>()?, + .collect::<Result<_>>()?, )) } } -fn bag_contains( - graph: &Graph, - start: &str, - target: &str, -) -> anyhow::Result<bool> { +fn bag_contains(graph: &Graph, start: &str, target: &str) -> Result<bool> { 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<i64> { +fn count_bags(graph: &Graph, color: &str) -> Result<i64> { 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::<anyhow::Result<Vec<_>>>()? + .collect::<Result<Vec<_>>>()? .iter() .sum::<i64>()) } @@ -100,11 +96,11 @@ fn count_bags(graph: &Graph, color: &str) -> anyhow::Result<i64> { #[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<Self, Self::Err> { 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<Self, Self::Err> { - 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<Vec<Op>> { - crate::util::parse::lines(fh) - .map(|line| line.parse()) - .collect() +pub fn parse(fh: File) -> Result<Vec<Op>> { + parse::lines(fh).map(|line| line.parse()).collect() } -pub fn part1(opcodes: Vec<Op>) -> anyhow::Result<i64> { +pub fn part1(opcodes: Vec<Op>) -> Result<i64> { 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<Op>) -> anyhow::Result<i64> { +pub fn part2(opcodes: Vec<Op>) -> Result<i64> { for i in 0..opcodes.len() { match opcodes[i].ty { OpType::Nop => { @@ -79,10 +77,10 @@ pub fn part2(opcodes: Vec<Op>) -> anyhow::Result<i64> { } } } - 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<Vec<i64>> { - Ok(crate::util::parse::ints(crate::util::parse::lines(fh)).collect()) +pub fn parse(fh: File) -> Result<Vec<i64>> { + Ok(parse::ints(parse::lines(fh)).collect()) } -pub fn part1(list: Vec<i64>) -> anyhow::Result<i64> { +pub fn part1(list: Vec<i64>) -> Result<i64> { 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<i64>) -> anyhow::Result<i64> { } } - Err(anyhow::anyhow!("failed to find invalid number")) + Err(anyhow!("failed to find invalid number")) } -pub fn part2(list: Vec<i64>) -> anyhow::Result<i64> { +pub fn part2(list: Vec<i64>) -> Result<i64> { 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<i64>) -> anyhow::Result<i64> { } } 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<i64>) -> anyhow::Result<i64> { } } - 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<i64> { +pub fn run(day: u8, puzzle: u8) -> Result<i64> { 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)), } } |