Add unchecked string parsing
Saves ~5-10% in parsing benchmarks
This commit is contained in:
parent
9db1906760
commit
30c8e0e1ff
|
@ -57,10 +57,10 @@ fn has_won(board: &Board) -> bool {
|
|||
|
||||
fn parse_input(raw: &str) -> BingoGame {
|
||||
let (input_numbers, boards) = raw.split_once("\n\n").unwrap();
|
||||
let input_numbers = input_numbers.split(',').map(|n| n.parse().unwrap()).collect();
|
||||
let input_numbers = input_numbers.split(',').map(parse_num).collect();
|
||||
let boards = boards
|
||||
.split("\n\n")
|
||||
.map(|b| b.split_ascii_whitespace().map(|n| n.parse().unwrap()).collect())
|
||||
.map(|b| b.split_ascii_whitespace().map(parse_num).collect())
|
||||
.map(|v: Vec<u8>| {
|
||||
debug_assert_eq!(v.len(), BOARD_SIZE * BOARD_SIZE);
|
||||
WINNING_INDICES.map(|row_or_col| row_or_col.map(|i| v[i]).to_vec()).to_vec()
|
||||
|
|
|
@ -20,7 +20,7 @@ fn parse_input(raw: &str) -> Parsed {
|
|||
raw.lines()
|
||||
.filter_map(|line| line.split_once(" -> "))
|
||||
.filter_map(|(c1, c2)| c1.split_once(',').zip(c2.split_once(',')))
|
||||
.map(|((x1, y1), (x2, y2))| ((x1.parse().unwrap(), y1.parse().unwrap()), (x2.parse().unwrap(), y2.parse().unwrap())))
|
||||
.map(|((x1, y1), (x2, y2))| ((parse_num(x1), parse_num(y1)), (parse_num(x2), parse_num(y2))))
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
use std::env;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
use std::ops::{AddAssign, MulAssign};
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
pub fn read_file(day: usize) -> String {
|
||||
std::fs::read_to_string(env::var("AOC_INPUT").unwrap_or_else(|_| format!("inputs/day{:0>2}", day))).unwrap()
|
||||
}
|
||||
|
@ -12,7 +21,18 @@ pub fn parse_nums_comma(l: &str) -> Vec<usize> {
|
|||
l.trim().split(',').map(parse_num).collect()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn parse_num(s: &str) -> usize {
|
||||
#[cfg(debug_assertions)]
|
||||
pub fn parse_num<T: FromStr<Err: Debug> + Display>(s: &str) -> T {
|
||||
s.parse().unwrap_or_else(|e| panic!("Invalid number {s}: {e:?}"))
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
pub fn parse_num<T: From<u8> + AddAssign<T> + MulAssign<T>>(s: &str) -> T {
|
||||
let mut digits = s.bytes().map(|b| T::from(b - b'0'));
|
||||
let mut n = digits.next().unwrap();
|
||||
for digit in digits {
|
||||
n *= T::from(10);
|
||||
n += digit;
|
||||
}
|
||||
n
|
||||
}
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
#![feature(associated_type_bounds)]
|
||||
pub mod common;
|
||||
pub mod teststuff;
|
||||
|
|
Loading…
Reference in New Issue
Block a user