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 = [
"aoc_libs",
"itertools",
"once_cell",
"regex",
]
[[package]]

View file

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

View file

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