From e3688dd0aca1e38e013bd119ab725d9bea0fe119 Mon Sep 17 00:00:00 2001 From: Connor Slade Date: Sun, 26 Nov 2023 14:06:06 -0500 Subject: [PATCH] Solution functions take input --- aoc_2021/src/day_01.rs | 10 +++++----- aoc_2021/src/day_02.rs | 12 +++++------- aoc_2021/src/day_03.rs | 16 +++++++--------- aoc_2021/src/day_04.rs | 14 +++++++------- aoc_2021/src/day_05.rs | 16 ++++++++-------- aoc_2021/src/day_06.rs | 12 ++++++------ aoc_2021/src/day_07.rs | 12 ++++++------ aoc_2021/src/day_08.rs | 12 ++++++------ aoc_2021/src/day_09.rs | 12 ++++++------ aoc_2021/src/day_10.rs | 12 ++++++------ aoc_2021/src/day_11.rs | 12 +++++------- aoc_2021/src/day_13.rs | 13 ++++++------- aoc_2021/src/day_14.rs | 12 +++++------- aoc_2021/src/lib.rs | 5 ++--- aoc_2022/src/day_01.rs | 12 +++++------- aoc_2022/src/day_02.rs | 12 +++++------- aoc_2022/src/day_03.rs | 12 +++++------- aoc_2022/src/day_04.rs | 14 ++++++-------- aoc_2022/src/day_05.rs | 12 +++++------- aoc_2022/src/day_06.rs | 12 +++++------- aoc_2022/src/day_07.rs | 12 +++++------- aoc_2022/src/day_08.rs | 12 +++++------- aoc_2022/src/day_09.rs | 13 ++++++------- aoc_2022/src/day_10.rs | 12 +++++------- aoc_2022/src/day_11.rs | 12 +++++------- aoc_2022/src/day_12.rs | 13 ++++++------- aoc_2022/src/day_13.rs | 12 +++++------- aoc_2022/src/day_14.rs | 13 ++++++------- aoc_2022/src/day_15.rs | 13 ++++++------- aoc_2022/src/day_16.rs | 8 +++----- aoc_2022/src/day_17.rs | 13 ++++++------- aoc_2022/src/day_18.rs | 22 ++++++++++------------ aoc_2022/src/day_19.rs | 10 ++++------ aoc_2022/src/day_20.rs | 12 +++++------- aoc_2022/src/day_21.rs | 12 +++++------- aoc_2022/src/day_22.rs | 12 ++++++------ aoc_2022/src/day_23.rs | 11 +++++------ aoc_2022/src/day_24.rs | 8 +++----- aoc_2022/src/day_25.rs | 9 ++++----- aoc_2023/src/day_01.rs | 8 +++----- common/src/lib.rs | 14 ++++++++++---- src/main.rs | 8 +++++--- 42 files changed, 229 insertions(+), 274 deletions(-) diff --git a/aoc_2021/src/day_01.rs b/aoc_2021/src/day_01.rs index 402b7b3..5380c07 100644 --- a/aoc_2021/src/day_01.rs +++ b/aoc_2021/src/day_01.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day01; @@ -7,8 +7,8 @@ impl Solution for Day01 { "Sonar Sweep" } - fn part_a(&self) -> String { - let data = problem::load(2021, 1) + fn part_a(&self, input: &str) -> String { + let data = input .lines() .map(|x| x.parse::().unwrap()) .collect::>(); @@ -17,8 +17,8 @@ impl Solution for Day01 { inc.to_string() } - fn part_b(&self) -> String { - let d = problem::load(2021, 1) + fn part_b(&self, input: &str) -> String { + let d = input .lines() .map(|x| x.parse::().unwrap()) .collect::>(); diff --git a/aoc_2021/src/day_02.rs b/aoc_2021/src/day_02.rs index e516382..09b1c94 100644 --- a/aoc_2021/src/day_02.rs +++ b/aoc_2021/src/day_02.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day02; @@ -7,12 +7,11 @@ impl Solution for Day02 { "Dive!" } - fn part_a(&self) -> String { - let d = problem::load(2021, 2); + fn part_a(&self, input: &str) -> String { let mut dep: u32 = 0; let mut hor: u32 = 0; - for i in d.lines() { + for i in input.lines() { let seg = i.split(' ').collect::>(); let x = seg[1].parse::().unwrap(); @@ -27,13 +26,12 @@ impl Solution for Day02 { (dep * hor).to_string() } - fn part_b(&self) -> String { - let d = problem::load(2021, 2); + fn part_b(&self, input: &str) -> String { let mut dep: u32 = 0; let mut hor: u32 = 0; let mut aim: u32 = 0; - for i in d.lines() { + for i in input.lines() { let seg = i.split(' ').collect::>(); let x = seg[1].parse::().unwrap(); diff --git a/aoc_2021/src/day_03.rs b/aoc_2021/src/day_03.rs index 123d8d7..3b15d7e 100644 --- a/aoc_2021/src/day_03.rs +++ b/aoc_2021/src/day_03.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day03; @@ -7,9 +7,8 @@ impl Solution for Day03 { "Binary Diagnostic" } - fn part_a(&self) -> String { - let data = problem::load(2021, 3); - let num_len = data.lines().next().unwrap().len(); + fn part_a(&self, input: &str) -> String { + let num_len = input.lines().next().unwrap().len(); let mut gamma = vec![0; num_len]; let mut epsilon = vec![1; num_len]; @@ -18,7 +17,7 @@ impl Solution for Day03 { let mut z = 0; let mut o = 0; - data.lines().for_each(|j| match j.chars().nth(i).unwrap() { + input.lines().for_each(|j| match j.chars().nth(i).unwrap() { '0' => z += 1, '1' => o += 1, _ => {} @@ -36,11 +35,10 @@ impl Solution for Day03 { (epsilon * gamma).to_string() } - fn part_b(&self) -> String { - let data = problem::load(2021, 3); - let num_len = data.lines().next().unwrap().len(); + fn part_b(&self, input: &str) -> String { + let num_len = input.lines().next().unwrap().len(); - let mut oxygen_keep = data.lines().collect::>(); + let mut oxygen_keep = input.lines().collect::>(); let mut oxygen_raw = vec![[0, 0]; num_len]; let mut oxygen_gen = 0; diff --git a/aoc_2021/src/day_04.rs b/aoc_2021/src/day_04.rs index 8743c3e..b01b7c3 100644 --- a/aoc_2021/src/day_04.rs +++ b/aoc_2021/src/day_04.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day04; @@ -7,15 +7,15 @@ impl Solution for Day04 { "Giant Squid" } - fn part_a(&self) -> String { - let bingo = Bingo::parse_input(problem::load(2021, 4)); + fn part_a(&self, input: &str) -> String { + let bingo = Bingo::parse_input(input); let winning = bingo.solve(); winning.0[winning.1].final_out(winning.2).to_string() } - fn part_b(&self) -> String { - let bingo = Bingo::parse_input(problem::load(2021, 4)); + fn part_b(&self, input: &str) -> String { + let bingo = Bingo::parse_input(input); let loseing = bingo.loseing_solve(); loseing.0[loseing.1].final_out(loseing.2).to_string() @@ -36,7 +36,7 @@ struct Board { } impl Bingo { - fn parse_input(inp: String) -> Self { + fn parse_input(inp: &str) -> Self { let mut lines = inp.lines(); let numbers = lines .next() @@ -103,7 +103,7 @@ impl Bingo { } impl Board { - fn parse(inp: String) -> Vec { + fn parse(inp: &str) -> Vec { let mut boards = Vec::new(); let inp = inp.replace('\r', ""); let raw_boards = inp.split("\n\n").skip(1); diff --git a/aoc_2021/src/day_05.rs b/aoc_2021/src/day_05.rs index 338b060..2133400 100644 --- a/aoc_2021/src/day_05.rs +++ b/aoc_2021/src/day_05.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; use hashbrown::HashMap; @@ -9,18 +9,18 @@ impl Solution for Day05 { "Hydrothermal Venture" } - fn part_a(&self) -> String { - run(false).to_string() + fn part_a(&self, input: &str) -> String { + run(input, false).to_string() } - fn part_b(&self) -> String { - run(true).to_string() + fn part_b(&self, input: &str) -> String { + run(input, true).to_string() } } /// dig -> Weather to include Diagonal Lines -fn run(dig: bool) -> u32 { - let data = Segment::parse_inp(problem::load(2021, 5), dig).unwrap(); +fn run(input: &str, dig: bool) -> u32 { + let data = Segment::parse_inp(input, dig).unwrap(); let mut all_loc = HashMap::new(); for x in data { @@ -47,7 +47,7 @@ struct Segment { } impl Segment { - fn parse_inp(inp: String, dig: bool) -> Option> { + fn parse_inp(inp: &str, dig: bool) -> Option> { let mut out = Vec::new(); for line in inp.lines() { diff --git a/aoc_2021/src/day_06.rs b/aoc_2021/src/day_06.rs index dc1bff0..140ae05 100644 --- a/aoc_2021/src/day_06.rs +++ b/aoc_2021/src/day_06.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; use std::hash::Hash; @@ -11,15 +11,15 @@ impl Solution for Day06 { "Lanternfish" } - fn part_a(&self) -> String { - let data = Fish::parse_inp(problem::load(2021, 6)); + fn part_a(&self, input: &str) -> String { + let data = Fish::parse_inp(input); let out = Fish::sim(data, 80); out.to_string() } - fn part_b(&self) -> String { - let data = Fish::parse_inp(problem::load(2021, 6)); + fn part_b(&self, input: &str) -> String { + let data = Fish::parse_inp(input); let out = Fish::sim(data, 256); out.to_string() @@ -36,7 +36,7 @@ impl Fish { Fish { timer } } - fn parse_inp(inp: String) -> Vec { + fn parse_inp(inp: &str) -> Vec { inp.lines() .next() .unwrap() diff --git a/aoc_2021/src/day_07.rs b/aoc_2021/src/day_07.rs index 7386219..6052171 100644 --- a/aoc_2021/src/day_07.rs +++ b/aoc_2021/src/day_07.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day07; @@ -7,8 +7,8 @@ impl Solution for Day07 { "The Treachery of Whales" } - fn part_a(&self) -> String { - let data = parse_crabs(problem::load(2021, 7)); + fn part_a(&self, input: &str) -> String { + let data = parse_crabs(input); let min = data.iter().min().unwrap(); let max = data.iter().max().unwrap(); @@ -24,8 +24,8 @@ impl Solution for Day07 { this_min.to_string() } - fn part_b(&self) -> String { - let data = parse_crabs(problem::load(2021, 7)); + fn part_b(&self, input: &str) -> String { + let data = parse_crabs(input); let min = data.iter().min().unwrap(); let max = data.iter().max().unwrap(); @@ -42,7 +42,7 @@ impl Solution for Day07 { } } -fn parse_crabs(inp: String) -> Vec { +fn parse_crabs(inp: &str) -> Vec { inp.lines() .next() .unwrap() diff --git a/aoc_2021/src/day_08.rs b/aoc_2021/src/day_08.rs index 443e694..7a6565a 100644 --- a/aoc_2021/src/day_08.rs +++ b/aoc_2021/src/day_08.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; use hashbrown::HashMap; @@ -15,8 +15,8 @@ impl Solution for Day08 { "Seven Segment Search" } - fn part_a(&self) -> String { - let data = parse(problem::load(2021, 8)); + fn part_a(&self, input: &str) -> String { + let data = parse(input); let mut inc = 0; for i in data { @@ -29,8 +29,8 @@ impl Solution for Day08 { inc.to_string() } - fn part_b(&self) -> String { - let data = parse(problem::load(2021, 8)); + fn part_b(&self, input: &str) -> String { + let data = parse(input); let mut inc = 0; let perms = permutations(CHARS.to_vec()); @@ -98,7 +98,7 @@ impl Solution for Day08 { } } -fn parse(inp: String) -> Vec<(Vec, Vec)> { +fn parse(inp: &str) -> Vec<(Vec, Vec)> { let mut out = Vec::new(); for i in inp.lines() { diff --git a/aoc_2021/src/day_09.rs b/aoc_2021/src/day_09.rs index 490b6b5..11529e9 100644 --- a/aoc_2021/src/day_09.rs +++ b/aoc_2021/src/day_09.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day09; @@ -7,22 +7,22 @@ impl Solution for Day09 { "Smoke Basin" } - fn part_a(&self) -> String { - let data = parse(problem::load(2021, 9)); + fn part_a(&self, input: &str) -> String { + let data = parse(input); let low = lowest(data); low.iter().map(|x| *x + 1).sum::().to_string() } - fn part_b(&self) -> String { - let data = parse(problem::load(2021, 9)); + fn part_b(&self, input: &str) -> String { + let data = parse(input); let basins = basins(data); basins.iter().rev().take(3).product::().to_string() } } -fn parse(inp: String) -> Vec> { +fn parse(inp: &str) -> Vec> { inp.lines() .map(|x| x.chars().map(|f| f.to_digit(10).unwrap()).collect()) .collect() diff --git a/aoc_2021/src/day_10.rs b/aoc_2021/src/day_10.rs index ccac899..1112143 100644 --- a/aoc_2021/src/day_10.rs +++ b/aoc_2021/src/day_10.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; const CHARS: [(char, char); 4] = [('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')]; @@ -9,8 +9,8 @@ impl Solution for Day10 { "Syntax Scoring" } - fn part_a(&self) -> String { - let data = parse(problem::load(2021, 10)); + fn part_a(&self, input: &str) -> String { + let data = parse(input); let mut total = 0; for i in data { @@ -37,8 +37,8 @@ impl Solution for Day10 { total.to_string() } - fn part_b(&self) -> String { - let data = parse(problem::load(2021, 10)); + fn part_b(&self, input: &str) -> String { + let data = parse(input); let mut scores = Vec::new(); for i in data { @@ -79,7 +79,7 @@ impl Solution for Day10 { } } -fn parse(lines: String) -> Vec { +fn parse(lines: &str) -> Vec { lines .lines() .map(|x| x.to_string()) diff --git a/aoc_2021/src/day_11.rs b/aoc_2021/src/day_11.rs index 32007ab..3fb3e1e 100644 --- a/aoc_2021/src/day_11.rs +++ b/aoc_2021/src/day_11.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day11; @@ -7,9 +7,8 @@ impl Solution for Day11 { "Dumbo Octopus" } - fn part_a(&self) -> String { - let raw = problem::load(2021, 11); - let mut octopi = parse(&raw); + fn part_a(&self, input: &str) -> String { + let mut octopi = parse(input); (0..100) .map(|_| step_octopi(&mut octopi)) @@ -17,9 +16,8 @@ impl Solution for Day11 { .to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2021, 11); - let mut octopi = parse(&raw); + fn part_b(&self, input: &str) -> String { + let mut octopi = parse(input); let octopi_count = octopi.len() * octopi[0].len(); let mut i = 0; diff --git a/aoc_2021/src/day_13.rs b/aoc_2021/src/day_13.rs index fd09129..97b6f4d 100644 --- a/aoc_2021/src/day_13.rs +++ b/aoc_2021/src/day_13.rs @@ -1,6 +1,7 @@ use hashbrown::HashSet; -use crate::{problem, Solution, aoc_lib}; +use crate::aoc_lib; +use common::Solution; type Point = aoc_lib::Point; @@ -11,17 +12,15 @@ impl Solution for Day13 { "Transparent Origami" } - fn part_a(&self) -> String { - let raw = problem::load(2021, 13); - let mut paper = Paper::parse(&raw); + fn part_a(&self, input: &str) -> String { + let mut paper = Paper::parse(input); paper.fold(0); paper.data.len().to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2021, 13); - let mut paper = Paper::parse(&raw); + fn part_b(&self, input: &str) -> String { + let mut paper = Paper::parse(input); (0..paper.folds.len()).for_each(|x| paper.fold(x)); paper.print() diff --git a/aoc_2021/src/day_14.rs b/aoc_2021/src/day_14.rs index fa55fc9..69441f5 100644 --- a/aoc_2021/src/day_14.rs +++ b/aoc_2021/src/day_14.rs @@ -1,6 +1,6 @@ use hashbrown::HashMap; -use crate::{problem, Solution}; +use common::Solution; pub struct Day14; @@ -9,15 +9,13 @@ impl Solution for Day14 { "Extended Polymerization" } - fn part_a(&self) -> String { - let raw = problem::load(2021, 14); - process(&raw, 10).to_string() + fn part_a(&self, input: &str) -> String { + process(input, 10).to_string() } // TODO: work with counts of units instead of the units themselves - fn part_b(&self) -> String { - let raw = problem::load(2021, 14); - let mut _polymer = Polymer::parse(&raw); + fn part_b(&self, input: &str) -> String { + let mut _polymer = Polymer::parse(input); todo!() } } diff --git a/aoc_2021/src/lib.rs b/aoc_2021/src/lib.rs index d65cb54..e4281f6 100644 --- a/aoc_2021/src/lib.rs +++ b/aoc_2021/src/lib.rs @@ -1,4 +1,4 @@ -pub use common as problem; +use common::DummySolution; pub use common::Solution; mod aoc_lib; @@ -28,8 +28,7 @@ pub const ALL: [&dyn Solution; 14] = [ &day_09::Day09, &day_10::Day10, &day_11::Day11, - &day_11::Day11, - // ^ PLACEHOLDER + &DummySolution, &day_13::Day13, &day_14::Day14, ]; diff --git a/aoc_2022/src/day_01.rs b/aoc_2022/src/day_01.rs index 136aa46..b93df9c 100644 --- a/aoc_2022/src/day_01.rs +++ b/aoc_2022/src/day_01.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day01; @@ -7,16 +7,14 @@ impl Solution for Day01 { "Calorie Counting" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 1); - let elfs = get_elfs(&raw); + fn part_a(&self, input: &str) -> String { + let elfs = get_elfs(input); elfs.last().unwrap().to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 1); - let elfs = get_elfs(&raw); + fn part_b(&self, input: &str) -> String { + let elfs = get_elfs(input); elfs.iter().rev().take(3).sum::().to_string() } diff --git a/aoc_2022/src/day_02.rs b/aoc_2022/src/day_02.rs index 8879723..4554755 100644 --- a/aoc_2022/src/day_02.rs +++ b/aoc_2022/src/day_02.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day02; @@ -7,11 +7,10 @@ impl Solution for Day02 { "Rock Paper Scissors" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 2); + fn part_a(&self, input: &str) -> String { let mut score = 0; - for (other, self_) in raw + for (other, self_) in input .lines() .filter(|x| !x.is_empty()) .map(|x| x.split_once(' ').unwrap()) @@ -26,11 +25,10 @@ impl Solution for Day02 { score.to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 2); + fn part_b(&self, input: &str) -> String { let mut score = 0; - for (other, self_) in raw + for (other, self_) in input .lines() .filter(|x| !x.is_empty()) .map(|x| x.split_once(' ').unwrap()) diff --git a/aoc_2022/src/day_03.rs b/aoc_2022/src/day_03.rs index 3360afb..627e9b9 100644 --- a/aoc_2022/src/day_03.rs +++ b/aoc_2022/src/day_03.rs @@ -1,6 +1,6 @@ use hashbrown::HashSet; -use crate::{problem, Solution}; +use common::Solution; pub struct Day03; @@ -9,11 +9,10 @@ impl Solution for Day03 { "Rucksack Reorganization" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 3); + fn part_a(&self, input: &str) -> String { let mut out = 0; - for i in raw.trim().lines() { + for i in input.trim().lines() { let mut bolth = i[0..i.len() / 2].chars().collect::>(); let pocket_2 = i[i.len() / 2..].chars().collect::>(); bolth.retain(|x| pocket_2.contains(x)); @@ -26,11 +25,10 @@ impl Solution for Day03 { out.to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 3); + fn part_b(&self, input: &str) -> String { let mut out = 0; - for i in raw.trim().lines().collect::>().chunks(3) { + for i in input.trim().lines().collect::>().chunks(3) { let mut all = HashSet::new(); i.iter().for_each(|x| all.extend(x.chars())); i.iter().for_each(|x| all.retain(|y| x.contains(*y))); diff --git a/aoc_2022/src/day_04.rs b/aoc_2022/src/day_04.rs index b69590e..15f877f 100644 --- a/aoc_2022/src/day_04.rs +++ b/aoc_2022/src/day_04.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day04; @@ -7,22 +7,20 @@ impl Solution for Day04 { "Camp Cleanup" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 4); + fn part_a(&self, input: &str) -> String { let mut out = 0; - for (p1, p2) in assignment_loop(raw) { + for (p1, p2) in assignment_loop(input) { out += ((p1.0 >= p2.0 && p1.1 <= p2.1) || (p2.0 >= p1.0 && p2.1 <= p1.1)) as usize; } out.to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 4); + fn part_b(&self, input: &str) -> String { let mut out = 0; - for (p1, p2) in assignment_loop(raw) { + for (p1, p2) in assignment_loop(input) { out += (p1.0.max(p2.0) <= p1.1.min(p2.1)) as usize; } @@ -30,7 +28,7 @@ impl Solution for Day04 { } } -fn assignment_loop(raw: String) -> Vec<((u32, u32), (u32, u32))> { +fn assignment_loop(raw: &str) -> Vec<((u32, u32), (u32, u32))> { raw.trim() .lines() .map(|x| x.split_once(',').unwrap()) diff --git a/aoc_2022/src/day_05.rs b/aoc_2022/src/day_05.rs index c203579..3110c6d 100644 --- a/aoc_2022/src/day_05.rs +++ b/aoc_2022/src/day_05.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day05; @@ -7,14 +7,12 @@ impl Solution for Day05 { "Supply Stacks" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 5); - process(&raw, true) + fn part_a(&self, input: &str) -> String { + process(&input, true) } - fn part_b(&self) -> String { - let raw = problem::load(2022, 5); - process(&raw, false) + fn part_b(&self, input: &str) -> String { + process(&input, false) } } diff --git a/aoc_2022/src/day_06.rs b/aoc_2022/src/day_06.rs index 7367b1e..044ce30 100644 --- a/aoc_2022/src/day_06.rs +++ b/aoc_2022/src/day_06.rs @@ -1,6 +1,6 @@ use hashbrown::HashSet; -use crate::{problem, Solution}; +use common::Solution; pub struct Day06; @@ -9,14 +9,12 @@ impl Solution for Day06 { "Tuning Trouble" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 6); - process(&raw, 4).to_string() + fn part_a(&self, input: &str) -> String { + process(input, 4).to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 6); - process(&raw, 14).to_string() + fn part_b(&self, input: &str) -> String { + process(input, 14).to_string() } } diff --git a/aoc_2022/src/day_07.rs b/aoc_2022/src/day_07.rs index edd4fd9..6ed0fb6 100644 --- a/aoc_2022/src/day_07.rs +++ b/aoc_2022/src/day_07.rs @@ -1,6 +1,6 @@ use hashbrown::HashSet; -use crate::{problem, Solution}; +use common::Solution; pub struct Day07; @@ -9,9 +9,8 @@ impl Solution for Day07 { "No Space Left On Device" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 7); - process(&raw) + fn part_a(&self, input: &str) -> String { + process(input) .get_all_children() .iter() .filter(|x| x.is_dir && x.size <= 100000) @@ -19,9 +18,8 @@ impl Solution for Day07 { .to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 7); - let folders = process(&raw); + fn part_b(&self, input: &str) -> String { + let folders = process(input); let needed_space = 30000000 - (70000000 - folders.size); let folder_vec = folders.get_all_children(); diff --git a/aoc_2022/src/day_08.rs b/aoc_2022/src/day_08.rs index ab9c4a3..4d9528d 100644 --- a/aoc_2022/src/day_08.rs +++ b/aoc_2022/src/day_08.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day08; @@ -7,9 +7,8 @@ impl Solution for Day08 { "Treetop Tree House" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 8); - let trees = parse_trees(&raw); + fn part_a(&self, input: &str) -> String { + let trees = parse_trees(input); let mut count = 0; for row in 0..trees.len() { @@ -28,9 +27,8 @@ impl Solution for Day08 { count.to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 8); - let trees = parse_trees(&raw); + fn part_b(&self, input: &str) -> String { + let trees = parse_trees(input); let mut count = 0; for row in 0..trees.len() { diff --git a/aoc_2022/src/day_09.rs b/aoc_2022/src/day_09.rs index 4626873..e7f3d43 100644 --- a/aoc_2022/src/day_09.rs +++ b/aoc_2022/src/day_09.rs @@ -2,7 +2,8 @@ use std::vec; use hashbrown::HashSet; -use crate::{aoc_lib, problem, Solution}; +use crate::aoc_lib; +use common::Solution; type Point = aoc_lib::Point; @@ -13,14 +14,12 @@ impl Solution for Day09 { "Rope Bridge" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 9); - process(&raw, 1).to_string() + fn part_a(&self, input: &str) -> String { + process(input, 1).to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 9); - process(&raw, 9).to_string() + fn part_b(&self, input: &str) -> String { + process(input, 9).to_string() } } diff --git a/aoc_2022/src/day_10.rs b/aoc_2022/src/day_10.rs index c757a79..d9f7027 100644 --- a/aoc_2022/src/day_10.rs +++ b/aoc_2022/src/day_10.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day10; @@ -7,9 +7,8 @@ impl Solution for Day10 { "Cathode-Ray Tube" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 10); - let instructions = parse(&raw); + fn part_a(&self, input: &str) -> String { + let instructions = parse(input); let cycles = cycle(&instructions); let mut out = 0; @@ -20,9 +19,8 @@ impl Solution for Day10 { out.to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 10); - let instructions = parse(&raw); + fn part_b(&self, input: &str) -> String { + let instructions = parse(input); let mut out = "\n".to_owned(); let mut sprite = 1; let mut cycle = 0; diff --git a/aoc_2022/src/day_11.rs b/aoc_2022/src/day_11.rs index bf5d742..38afa30 100644 --- a/aoc_2022/src/day_11.rs +++ b/aoc_2022/src/day_11.rs @@ -1,6 +1,6 @@ use std::collections::VecDeque; -use crate::{problem, Solution}; +use common::Solution; pub struct Day11; @@ -9,16 +9,14 @@ impl Solution for Day11 { "Monkey in the Middle" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 11); - let monkeys = parse_monkeys(&raw); + fn part_a(&self, input: &str) -> String { + let monkeys = parse_monkeys(input); process(monkeys, 20, |x| x / 3).to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 11); - let monkeys = parse_monkeys(&raw); + fn part_b(&self, input: &str) -> String { + let monkeys = parse_monkeys(input); let magic = monkeys.iter().map(|x| x.test.divisor).product::(); process(monkeys, 10000, |x| x % magic).to_string() diff --git a/aoc_2022/src/day_12.rs b/aoc_2022/src/day_12.rs index 4d76f8d..bd85591 100644 --- a/aoc_2022/src/day_12.rs +++ b/aoc_2022/src/day_12.rs @@ -1,6 +1,7 @@ use std::collections::VecDeque; -use crate::{problem, Solution, aoc_lib}; +use crate::aoc_lib; +use common::Solution; type Point = aoc_lib::Point; @@ -11,18 +12,16 @@ impl Solution for Day12 { "Hill Climbing Algorithm" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 12); - let map = parse(&raw); + fn part_a(&self, input: &str) -> String { + let map = parse(input); run_path(&map, |a, b| a <= b + 1, |c| c == map.end) .unwrap() .to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 12); - let mut map = parse(&raw); + fn part_b(&self, input: &str) -> String { + let mut map = parse(input); map.start = map.end; map.current = map.start; diff --git a/aoc_2022/src/day_13.rs b/aoc_2022/src/day_13.rs index 9b04ebf..3a6ac1c 100644 --- a/aoc_2022/src/day_13.rs +++ b/aoc_2022/src/day_13.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use crate::{problem, Solution}; +use common::Solution; pub struct Day13; @@ -9,9 +9,8 @@ impl Solution for Day13 { "Distress Signal" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 13); - let signals = parse(&raw); + fn part_a(&self, input: &str) -> String { + let signals = parse(input); signals .chunks(2) @@ -22,9 +21,8 @@ impl Solution for Day13 { .to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 13); - let mut signals = parse(&raw); + fn part_b(&self, input: &str) -> String { + let mut signals = parse(input); let div = [Token::Number(6), Token::Number(2)]; signals.extend(div.clone()); signals.sort(); diff --git a/aoc_2022/src/day_14.rs b/aoc_2022/src/day_14.rs index e799f7f..e89a37e 100644 --- a/aoc_2022/src/day_14.rs +++ b/aoc_2022/src/day_14.rs @@ -1,6 +1,7 @@ use aoc_lib::Matrix; -use crate::{aoc_lib, problem, Solution}; +use crate::aoc_lib; +use common::Solution; type Point = aoc_lib::Point; @@ -13,9 +14,8 @@ impl Solution for Day14 { "Regolith Reservoir" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 14); - let mut world = World::parse(&raw); + fn part_a(&self, input: &str) -> String { + let mut world = World::parse(input); 'o: loop { world.working = NEW_POINT; @@ -31,9 +31,8 @@ impl Solution for Day14 { (world.count_sand() - 1).to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 14); - let mut world = World::parse(&raw); + fn part_b(&self, input: &str) -> String { + let mut world = World::parse(input); loop { if world.working.y == 0 { diff --git a/aoc_2022/src/day_15.rs b/aoc_2022/src/day_15.rs index 8a96f48..21cbe4e 100644 --- a/aoc_2022/src/day_15.rs +++ b/aoc_2022/src/day_15.rs @@ -1,4 +1,5 @@ -use crate::{problem, Solution, aoc_lib}; +use crate::aoc_lib; +use common::Solution; use rayon::prelude::*; @@ -11,9 +12,8 @@ impl Solution for Day15 { "Beacon Exclusion Zone" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 15); - let world = World::parse(&raw); + fn part_a(&self, input: &str) -> String { + let world = World::parse(input); let y_level = 2000000; // 10 for example let blocked = (world.bounds.0.x..=world.bounds.1.x) @@ -25,9 +25,8 @@ impl Solution for Day15 { (blocked - 1).to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 15); - let world = World::parse(&raw); + fn part_b(&self, input: &str) -> String { + let world = World::parse(input); let bounds = 4000000; let distress = world.search(bounds).expect("No distress beacon found"); diff --git a/aoc_2022/src/day_16.rs b/aoc_2022/src/day_16.rs index 182f497..0e01dc7 100644 --- a/aoc_2022/src/day_16.rs +++ b/aoc_2022/src/day_16.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; // use hashbrown::HashMap; // use petgraph::Graph; @@ -10,8 +10,7 @@ impl Solution for Day16 { "Proboscidea Volcanium" } - fn part_a(&self) -> String { - let _raw = problem::load(2022, 16); + fn part_a(&self, _input: &str) -> String { // let mut _graph = parse(&raw); // let out = dfs(&mut HashMap::new(), &graph, 0, 0, 0, 30); @@ -19,8 +18,7 @@ impl Solution for Day16 { todo!() } - fn part_b(&self) -> String { - let _raw = problem::load(2022, 16); + fn part_b(&self, _input: &str) -> String { todo!() } } diff --git a/aoc_2022/src/day_17.rs b/aoc_2022/src/day_17.rs index 92239fe..74541f6 100644 --- a/aoc_2022/src/day_17.rs +++ b/aoc_2022/src/day_17.rs @@ -1,6 +1,7 @@ use hashbrown::{hash_map::Entry, HashMap}; -use crate::{problem, Solution, aoc_lib}; +use crate::aoc_lib; +use common::Solution; type Point = aoc_lib::Point; @@ -11,15 +12,13 @@ impl Solution for Day17 { "Pyroclastic Flow" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 17); - let mut world = World::new(&raw); + fn part_a(&self, input: &str) -> String { + let mut world = World::new(input); process(&mut world, 2022).to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 17); - let mut world = World::new(&raw); + fn part_b(&self, input: &str) -> String { + let mut world = World::new(input); process(&mut world, 1000000000000).to_string() } } diff --git a/aoc_2022/src/day_18.rs b/aoc_2022/src/day_18.rs index 050842d..ba9e6a9 100644 --- a/aoc_2022/src/day_18.rs +++ b/aoc_2022/src/day_18.rs @@ -1,7 +1,7 @@ -use aoc_lib::Point3; use hashbrown::HashSet; -use crate::{problem, Solution, aoc_lib}; +use crate::aoc_lib::Point3; +use common::Solution; pub struct Day18; @@ -10,9 +10,8 @@ impl Solution for Day18 { "Boiling Boulders" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 18); - let world = World::parse(&raw); + fn part_a(&self, input: &str) -> String { + let world = World::parse(input); let mut open_faces = 0; @@ -23,14 +22,13 @@ impl Solution for Day18 { open_faces.to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 18); - let world = World::parse(&raw); + fn part_b(&self, input: &str) -> String { + let world = World::parse(input); let outside = world.flood_fill(Point3::new(0, 0, 0)); let mut out = 0; for i in &world.points { - for j in NEIHBORS { + for j in NEIGHBORS { let n = *i + j; if !world.points.contains(&n) && outside.contains(&n) { out += 1; @@ -46,7 +44,7 @@ struct World { points: HashSet, } -const NEIHBORS: [Point3; 6] = [ +const NEIGHBORS: [Point3; 6] = [ Point3::new(1, 0, 0), Point3::new(-1, 0, 0), Point3::new(0, 1, 0), @@ -65,7 +63,7 @@ impl World { fn neighbors(&self, point: &Point3) -> usize { let mut out = 0; - for i in NEIHBORS { + for i in NEIGHBORS { out += self.points.contains(&(*point + i)) as usize; } @@ -91,7 +89,7 @@ impl World { while let Some(s) = new.pop() { steam.insert(s); - for n in NEIHBORS { + for n in NEIGHBORS { let n = s + n; if n.x > bounds.1.x + 1 || n.x < bounds.0.x - 1 { continue; diff --git a/aoc_2022/src/day_19.rs b/aoc_2022/src/day_19.rs index f7d4a18..0bdae0a 100644 --- a/aoc_2022/src/day_19.rs +++ b/aoc_2022/src/day_19.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; use std::mem; @@ -9,9 +9,8 @@ impl Solution for Day19 { "" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 19); - let robots = parse(&raw); + fn part_a(&self, input: &str) -> String { + let robots = parse(input); let mut geodes = Vec::new(); for i in robots.into_iter().take(1) { @@ -27,8 +26,7 @@ impl Solution for Day19 { .to_string() } - fn part_b(&self) -> String { - let _raw = problem::load(2022, 19); + fn part_b(&self, _input: &str) -> String { todo!() } } diff --git a/aoc_2022/src/day_20.rs b/aoc_2022/src/day_20.rs index 098d209..07127f5 100644 --- a/aoc_2022/src/day_20.rs +++ b/aoc_2022/src/day_20.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day20; @@ -7,16 +7,14 @@ impl Solution for Day20 { "Grove Positioning System" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 20); - let mut file = File::new(&raw); + fn part_a(&self, input: &str) -> String { + let mut file = File::new(input); file.mix(); file.coordinates().to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 20); - let mut file = File::new(&raw).multiply(811589153); + fn part_b(&self, input: &str) -> String { + let mut file = File::new(input).multiply(811589153); (0..10).for_each(|_| file.mix()); file.coordinates().to_string() } diff --git a/aoc_2022/src/day_21.rs b/aoc_2022/src/day_21.rs index 735f02d..c660c44 100644 --- a/aoc_2022/src/day_21.rs +++ b/aoc_2022/src/day_21.rs @@ -1,6 +1,6 @@ use hashbrown::HashMap; -use crate::{problem, Solution}; +use common::Solution; pub struct Day21; @@ -9,15 +9,13 @@ impl Solution for Day21 { "Monkey Math" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 21); - let monkeys = MonkeyBusiness::new(&raw); + fn part_a(&self, input: &str) -> String { + let monkeys = MonkeyBusiness::new(input); monkeys.evaluate("root").to_string() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 21); - let monkeys = MonkeyBusiness::new(&raw).root_eq(); + fn part_b(&self, input: &str) -> String { + let monkeys = MonkeyBusiness::new(input).root_eq(); monkeys.solve("root").to_string() } } diff --git a/aoc_2022/src/day_22.rs b/aoc_2022/src/day_22.rs index 8172da8..599afd6 100644 --- a/aoc_2022/src/day_22.rs +++ b/aoc_2022/src/day_22.rs @@ -1,7 +1,9 @@ -use crate::{problem, Solution, aoc_lib}; use hashbrown::HashSet; use std::collections::VecDeque; +use crate::aoc_lib; +use common::Solution; + type Point = aoc_lib::Point; pub struct Day22; @@ -10,16 +12,14 @@ impl Solution for Day22 { "Monkey Map" } - fn part_a(&self) -> String { - let raw = problem::load_raw(2022, 22); - let mut world = World::parse(&raw); + fn part_a(&self, input: &str) -> String { + let mut world = World::parse(input); world.run(wrap_1); world.password().to_string() } - fn part_b(&self) -> String { - let _raw = problem::load(2022, 22); + fn part_b(&self, _input: &str) -> String { todo!() } } diff --git a/aoc_2022/src/day_23.rs b/aoc_2022/src/day_23.rs index d9a1afc..9e1f0ca 100644 --- a/aoc_2022/src/day_23.rs +++ b/aoc_2022/src/day_23.rs @@ -1,6 +1,7 @@ use hashbrown::{hash_map::Entry, HashMap, HashSet}; -use crate::{aoc_lib, problem, Solution}; +use crate::aoc_lib; +use common::Solution; type Point = aoc_lib::Point; @@ -11,9 +12,8 @@ impl Solution for Day23 { "Unstable Diffusion" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 23); - let mut world = World::parse(&raw); + fn part_a(&self, input: &str) -> String { + let mut world = World::parse(input); world.draw(); for _ in 0..10 { @@ -24,8 +24,7 @@ impl Solution for Day23 { world.count_blank().to_string() } - fn part_b(&self) -> String { - let _raw = problem::load(2022, 23); + fn part_b(&self, _input: &str) -> String { todo!() } } diff --git a/aoc_2022/src/day_24.rs b/aoc_2022/src/day_24.rs index 0ca06a8..9b935e7 100644 --- a/aoc_2022/src/day_24.rs +++ b/aoc_2022/src/day_24.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day24; @@ -7,13 +7,11 @@ impl Solution for Day24 { "" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 24); + fn part_a(&self, _input: &str) -> String { todo!() } - fn part_b(&self) -> String { - let raw = problem::load(2022, 24); + fn part_b(&self, _input: &str) -> String { todo!() } } diff --git a/aoc_2022/src/day_25.rs b/aoc_2022/src/day_25.rs index 7a9134d..28ed1c2 100644 --- a/aoc_2022/src/day_25.rs +++ b/aoc_2022/src/day_25.rs @@ -1,4 +1,4 @@ -use crate::{problem, Solution}; +use common::Solution; pub struct Day25; @@ -7,12 +7,11 @@ impl Solution for Day25 { "Full of Hot Air" } - fn part_a(&self) -> String { - let raw = problem::load(2022, 25); - snafu::encode(raw.lines().map(snafu::decode).sum::()).to_string() + fn part_a(&self, input: &str) -> String { + snafu::encode(input.lines().map(snafu::decode).sum::()).to_string() } - fn part_b(&self) -> String { + fn part_b(&self, _input: &str) -> String { String::new() } } diff --git a/aoc_2023/src/day_01.rs b/aoc_2023/src/day_01.rs index f7b1f11..59d30a4 100644 --- a/aoc_2023/src/day_01.rs +++ b/aoc_2023/src/day_01.rs @@ -1,4 +1,4 @@ -use common::{load, Solution}; +use common::Solution; pub struct Day01; @@ -7,13 +7,11 @@ impl Solution for Day01 { "" } - fn part_a(&self) -> String { - let raw = load(2023, 1); + fn part_a(&self, input: &str) -> String { todo!() } - fn part_b(&self) -> String { - let raw = load(2023, 1); + fn part_b(&self, input: &str) -> String { todo!() } } diff --git a/common/src/lib.rs b/common/src/lib.rs index 6629f4d..851d62a 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -2,14 +2,20 @@ use std::fs; pub trait Solution { fn name(&self) -> &'static str; - fn part_a(&self) -> String; - fn part_b(&self) -> String; + fn part_a(&self, input: &str) -> String; + fn part_b(&self, input: &str) -> String; fn is_dummy(&self) -> bool { false } } +pub enum Answer { + String(String), + Number(u64), + Float(f64), +} + /// Load the input for the given year and day. /// Removes carriage returns and trims leading and trailing whitespace. pub fn load(year: u32, day: u32) -> String { @@ -29,11 +35,11 @@ impl Solution for DummySolution { unreachable!() } - fn part_a(&self) -> String { + fn part_a(&self, _input: &str) -> String { unreachable!() } - fn part_b(&self) -> String { + fn part_b(&self, _input: &str) -> String { unreachable!() } diff --git a/src/main.rs b/src/main.rs index 565b95e..4a348d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ use std::time::Instant; -use args::{Args, Commands}; use clap::Parser; use common::Solution; + +use args::{Args, Commands}; mod args; const DEFAULT_YEAR: u32 = 2023; @@ -25,11 +26,12 @@ fn main() { }; println!("[*] Running: {} ({})", solution.name(), part.to_uppercase()); + let input = common::load(year, day + 1); let start = Instant::now(); let out = match part.to_lowercase().to_string().as_str() { - "a" => solution.part_a(), - "b" => solution.part_b(), + "a" => solution.part_a(&input), + "b" => solution.part_b(&input), _ => return println!("[-] Invalid Part {}", part), };