From b15a06e07c242fc56debca5b0163d6b154092611 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Wed, 8 Dec 2021 18:16:15 -0800 Subject: [PATCH] Day 8 part 2 perf improvements with s/HashSet/Vec/ and others --- 2021/src/day8.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/2021/src/day8.rs b/2021/src/day8.rs index 7f4367d..5be26ea 100644 --- a/2021/src/day8.rs +++ b/2021/src/day8.rs @@ -117,7 +117,7 @@ //! For each entry, determine all of the wire/segment connections and decode the four-digit output values. What do you get if you add up all of the output values? //! use std::{ - collections::{HashMap, HashSet}, + collections::HashMap, convert::Infallible, fmt::{Debug, Error, Formatter}, ops::BitAnd, @@ -158,7 +158,7 @@ impl FromStr for Segment { fn from_str(s: &str) -> Result { let mut bits = 0; for b in s.as_bytes() { - bits |= 1 << b - b'a'; + bits |= 1 << (b - b'a'); } Ok(Segment(bits)) @@ -176,18 +176,18 @@ impl BitAnd for Segment { fn build_lookup(input: &str) -> Result> { let mut map: HashMap = HashMap::new(); - let set: HashSet<_> = input.split(' ').collect(); + let set: Vec<_> = input.split(' ').collect(); for digit in &set { - let d = digit.parse().unwrap(); + let s = digit.parse().unwrap(); match digit.len() { // 1 - 2 => map.insert(1, d), + 2 => map.insert(1, s), // 7 - 3 => map.insert(7, d), + 3 => map.insert(7, s), // 4 - 4 => map.insert(4, d), + 4 => map.insert(4, s), // 8 - 7 => map.insert(8, d), + 7 => map.insert(8, s), _ => None, }; } @@ -232,18 +232,13 @@ fn build_lookup(input: &str) -> Result> { fn output(line: &str) -> Result { let (inp, out) = line.split_once(" | ").expect("line missing |"); let lookup = build_lookup(inp)?; - let mut answer = 0; - for digit in out + Ok(out .split(' ') .map(|s| { - let s: Segment = s.parse()?; - Ok(lookup[&s]) + let s: Segment = s.parse().unwrap(); + lookup[&s] }) - .collect::>>()? - { - answer = 10 * answer + digit as u64; - } - Ok(answer) + .fold(0, |answer, d| 10 * answer + d as u64)) } #[aoc(day8, part2)]