From f85316c44bac766e2496d0487886fe879ce4afff Mon Sep 17 00:00:00 2001 From: Josh Cook Date: Wed, 10 Jan 2024 08:02:50 -0500 Subject: [PATCH] just pushing local changes so can work on elsewhere --- puzzle-input/20.txt | 58 ++++++++++++++++++++++++++++++++++++++++++ src/solutions/day19.rs | 21 ++++++++++----- tests/test_day19.rs | 8 +++++- 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/puzzle-input/20.txt b/puzzle-input/20.txt index e69de29..8eb8b7c 100644 --- a/puzzle-input/20.txt +++ b/puzzle-input/20.txt @@ -0,0 +1,58 @@ +%pj -> sh +%mn -> jp +&hf -> rg, vl, tq, qq, mv, zz +%xl -> hf +%sv -> mn, dl +%kk -> lh +&sj -> kz +%jj -> lq, kk +%mr -> bm, hb +%sx -> lq, fn +%fn -> zr, lq +%pf -> dl, gv +%lr -> jj, lq +%jp -> dl, pj +&hb -> sj, mr, rz, qg, pr +%vg -> zz, hf +%pr -> zq +%hn -> pf +%jg -> tj +%qg -> vk +%dv -> xl, hf +&qq -> kz +%fm -> lr +&ls -> kz +%pd -> hb, xg +%rj -> hb +%fb -> hf, tq +%zz -> np +%bm -> pd, hb +%xn -> lq, fm +%gv -> jg, dl +%dz -> sx +%zs -> dl, nh +%tj -> zs, dl +%mv -> vl +&kz -> rx +%np -> cl, hf +&bg -> kz +%vl -> vg +%xg -> rz, hb +%rz -> pr +%zq -> hb, qg +%lh -> rd +%zr -> lq +%fl -> hb, rj +%xr -> xn, lq +%rd -> dz, lq +%cl -> hf, gj +%nh -> dl +%sh -> hn, dl +%vk -> fx, hb +%gj -> hf, dv +%tq -> mv +&dl -> hn, pj, ls, mn, jg, sv +%fx -> fl, hb +&lq -> bg, kk, dz, xr, lh, fm +%rg -> hf, fb +broadcaster -> xr, mr, rg, sv diff --git a/src/solutions/day19.rs b/src/solutions/day19.rs index a876f34..b99e86f 100644 --- a/src/solutions/day19.rs +++ b/src/solutions/day19.rs @@ -259,6 +259,15 @@ pub fn puzzle_1(input: &str) -> Result { Ok(organize_parts(&parts, &workflows_map)) } +pub fn puzzle_2(input: &str) -> Result { + let (workflows, parts) = parse_input(input)?; + let workflows_map = workflows + .iter() + .map(|w| (w.name.as_str(), w.clone())) + .collect::>(); + Ok(organize_parts(&parts, &workflows_map)) +} + pub fn main(data_dir: &str) { println!("Day 19: Aplenty"); let data = load(data_dir, 19, None); @@ -269,13 +278,13 @@ pub fn main(data_dir: &str) { Ok(x) => println!(" Puzzle 1: {}", x), Err(e) => panic!("No solution to puzzle 1: {}.", e), } - // assert_eq!(answer_1, Ok(37113)); + assert_eq!(answer_1, Ok(509597)); // Puzzle 2. - // let answer_2 = puzzle_2(&data); - // match answer_2 { - // Ok(x) => println!(" Puzzle 2: {}", x), - // Err(e) => panic!("No solution to puzzle 2: {}", e), - // } + let answer_2 = puzzle_2(&data); + match answer_2 { + Ok(x) => println!(" Puzzle 2: {}", x), + Err(e) => panic!("No solution to puzzle 2: {}", e), + } // assert_eq!(answer_2, Ok(30449)) } diff --git a/tests/test_day19.rs b/tests/test_day19.rs index 2ab3959..ca88873 100644 --- a/tests/test_day19.rs +++ b/tests/test_day19.rs @@ -1,4 +1,4 @@ -use aoc_2023::solutions::day19::puzzle_1; +use aoc_2023::solutions::day19::{puzzle_1, puzzle_2}; const EXAMPLE_INPUT_1: &str = " px{a<2006:qkq,m>2090:A,rfg} @@ -25,3 +25,9 @@ fn puzzle_1_example_1() { let _ = env_logger::try_init(); assert_eq!(puzzle_1(self::EXAMPLE_INPUT_1), Ok(19114)); } + +#[test] +fn puzzle_2_example_1() { + let _ = env_logger::try_init(); + assert_eq!(puzzle_1(self::EXAMPLE_INPUT_1), Ok(167409079868000)); +}