optimized day07 parsing.

This commit is contained in:
Gabe Venberg 2023-12-09 19:45:19 -06:00
parent 6ce20d7c69
commit c9e4dab862
3 changed files with 4 additions and 14 deletions

2
Cargo.lock generated
View file

@ -69,8 +69,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"aoc_libs", "aoc_libs",
"itertools", "itertools",
"once_cell",
"regex",
] ]
[[package]] [[package]]

View file

@ -7,6 +7,4 @@ edition.workspace = true
[dependencies] [dependencies]
aoc_libs.workspace = true aoc_libs.workspace = true
regex.workspace = true
once_cell.workspace = true
itertools.workspace = true itertools.workspace = true

View file

@ -1,6 +1,4 @@
use itertools::Itertools; use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum Card { pub enum Card {
@ -82,16 +80,13 @@ impl Hand {
} }
} }
static PARSE_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^([AKQJT98765432]{5}) (\d+)$").unwrap());
pub fn parse(input: &str) -> Vec<(Hand, u32)> { pub fn parse(input: &str) -> Vec<(Hand, u32)> {
input input
.lines() .lines()
.map(|line| { .map(|line| {
let captures = PARSE_REGEX.captures(line).unwrap(); let mut splits = line.split(' ');
let hand = &captures[1]; let hand = splits.next().unwrap();
let bid: u32 = captures[2].parse().unwrap(); let bid: u32 = splits.next().unwrap().parse().unwrap();
let hand: Vec<Card> = hand let hand: Vec<Card> = hand
.chars() .chars()
.map(|c| match c { .map(|c| match c {
@ -111,8 +106,7 @@ pub fn parse(input: &str) -> Vec<(Hand, u32)> {
e => panic!("invalid card {}", e), e => panic!("invalid card {}", e),
}) })
.collect(); .collect();
let hand = Hand::new(hand.try_into().unwrap()); (Hand::new(hand.try_into().unwrap()), bid)
(hand, bid)
}) })
.collect() .collect()
} }