Day 8 part 2 perf improvements with s/HashSet/Vec/ and others

This commit is contained in:
Bill Thiede 2021-12-08 18:16:15 -08:00
parent 19ca505fde
commit b15a06e07c

View File

@ -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? //! 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::{ use std::{
collections::{HashMap, HashSet}, collections::HashMap,
convert::Infallible, convert::Infallible,
fmt::{Debug, Error, Formatter}, fmt::{Debug, Error, Formatter},
ops::BitAnd, ops::BitAnd,
@ -158,7 +158,7 @@ impl FromStr for Segment {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut bits = 0; let mut bits = 0;
for b in s.as_bytes() { for b in s.as_bytes() {
bits |= 1 << b - b'a'; bits |= 1 << (b - b'a');
} }
Ok(Segment(bits)) Ok(Segment(bits))
@ -176,18 +176,18 @@ impl BitAnd for Segment {
fn build_lookup(input: &str) -> Result<HashMap<Segment, u8>> { fn build_lookup(input: &str) -> Result<HashMap<Segment, u8>> {
let mut map: HashMap<u8, Segment> = HashMap::new(); let mut map: HashMap<u8, Segment> = HashMap::new();
let set: HashSet<_> = input.split(' ').collect(); let set: Vec<_> = input.split(' ').collect();
for digit in &set { for digit in &set {
let d = digit.parse().unwrap(); let s = digit.parse().unwrap();
match digit.len() { match digit.len() {
// 1 // 1
2 => map.insert(1, d), 2 => map.insert(1, s),
// 7 // 7
3 => map.insert(7, d), 3 => map.insert(7, s),
// 4 // 4
4 => map.insert(4, d), 4 => map.insert(4, s),
// 8 // 8
7 => map.insert(8, d), 7 => map.insert(8, s),
_ => None, _ => None,
}; };
} }
@ -232,18 +232,13 @@ fn build_lookup(input: &str) -> Result<HashMap<Segment, u8>> {
fn output(line: &str) -> Result<u64> { fn output(line: &str) -> Result<u64> {
let (inp, out) = line.split_once(" | ").expect("line missing |"); let (inp, out) = line.split_once(" | ").expect("line missing |");
let lookup = build_lookup(inp)?; let lookup = build_lookup(inp)?;
let mut answer = 0; Ok(out
for digit in out
.split(' ') .split(' ')
.map(|s| { .map(|s| {
let s: Segment = s.parse()?; let s: Segment = s.parse().unwrap();
Ok(lookup[&s]) lookup[&s]
}) })
.collect::<Result<Vec<_>>>()? .fold(0, |answer, d| 10 * answer + d as u64))
{
answer = 10 * answer + digit as u64;
}
Ok(answer)
} }
#[aoc(day8, part2)] #[aoc(day8, part2)]