From 28e1b3e9ec18e288fe92351c0e2b88a1f13347a8 Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 13 Dec 2020 15:02:59 +0000 Subject: [PATCH] day10: part2 ie --- day10/src/main.rs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/day10/src/main.rs b/day10/src/main.rs index db28dc7..bc87414 100644 --- a/day10/src/main.rs +++ b/day10/src/main.rs @@ -28,12 +28,13 @@ fn parse_input(file: &str) -> Result, io::Error Ok(input::read_input(BufReader::new(file))) } -fn find_smallest(map: &Adaptors, finding: impl Into) -> (Option, Diffs) +/// Get an iterator over the possible adaptors after the adaptor `finding`. The iterator will be between 0 and 3 elements. +/// Which adaptor has been changed *first* is increased in `diffs` if it is provided. +fn iterate_adaptor_chain(map: &Adaptors, mut diffs: Option<&mut Diffs>, finding: impl Into) -> impl Iterator { #![allow(unused_assignments)] let finding = finding.into(); - let mut diffs = Diffs::default(); let mut set=false; macro_rules! find { ($l:literal) => { @@ -46,7 +47,9 @@ fn find_smallest(map: &Adaptors, finding: impl Into) -> (Option, Diffs) }).map(|_| shim) { Some(yes) if !set => { set = true; - *diffs.at_mut($l-1) += 1; + if let Some(diffs) = diffs.as_mut() { + *diffs.at_mut($l-1) += 1; + } Some(yes) }, x => x @@ -54,12 +57,17 @@ fn find_smallest(map: &Adaptors, finding: impl Into) -> (Option, Diffs) } } }; - (iter![ + iter![ find!(1), find!(2), find!(3) ].filter_map(std::convert::identity) - .min(), diffs) +} + +#[inline] fn find_smallest(map: &Adaptors, finding: impl Into) -> (Option, Diffs) +{ + let mut diffs = Diffs::default(); + (iterate_adaptor_chain(map, Some(&mut diffs), finding).min(), diffs) } #[inline(always)] fn conv_input(input: impl Iterator) -> impl Iterator @@ -77,14 +85,20 @@ fn find_smallest(map: &Adaptors, finding: impl Into) -> (Option, Diffs) } } } + +mod part2; + + fn main() -> Result<(), Box> { - let all_jolts = { + + let (all_jolts, output_rat) = { let mut map: Adaptors = conv_input(get_input()).collect(); + let orat = map.iter().map(|&(x, _)| x).max().unwrap().get() + 3; // rating of output device unsafe { - map.insert(NonZeroU8::new_unchecked(map.iter().map(|&(x, _)| x).max().unwrap().get() + 3), ()); + map.insert(NonZeroU8::new_unchecked(orat), ()); } - map + (map, orat) }; debug_assert_eq!(all_jolts.num_pages(), 1); // Assert we have space efficiency. @@ -97,5 +111,9 @@ fn main() -> Result<(), Box> }).map(|(_, d)| d).sum(); println!("{}", diffs.jd1() * diffs.jd3()); + println!("{}", { + //let start = all_jolts.iter().map(|&(k, _)| u8::from(k)).min().unwrap(); + part2::solve(all_jolts, output_rat) + }); Ok(()) }