optimize part1

This commit is contained in:
nora 2023-12-03 16:26:21 +01:00
parent adc39acadc
commit 5c1a089038
2 changed files with 84 additions and 42 deletions

View file

@ -1,3 +1,4 @@
mod p1;
mod p2basic;
mod p2bytes;
mod p2faster_hash;
@ -15,7 +16,8 @@ struct Day03;
helper::define_variants! {
day => crate::Day03;
part1 {
basic => crate::part1;
basic => crate::p1::basic;
bytes => crate::p1::bytes;
}
part2 {
basic => crate::p2basic::part2;
@ -36,47 +38,6 @@ impl Day for Day03 {
}
}
fn part1(input: &str) -> u64 {
fn contains_symbol(s: &str) -> bool {
s.chars().any(|c| !c.is_ascii_digit() && c != '.')
}
let len = input.lines().next().unwrap().len();
let empty_border = std::iter::repeat('.').take(len).collect::<String>();
let mut prev2 = empty_border.as_str();
let mut prev1 = input.lines().next().unwrap();
let mut acc = 0;
for next in input.lines().skip(1).chain(Some(empty_border.as_str())) {
let mut numbers = prev1.char_indices().peekable();
while let Some((start, c)) = numbers.next() {
if c.is_ascii_digit() {
let mut end = start;
while let Some((idx, '0'..='9')) = numbers.next() {
end = idx;
}
let box_bounds = (start.saturating_sub(1))..std::cmp::min(end + 2, len);
let number = prev1[start..=end].parse::<u64>().unwrap();
if contains_symbol(&prev2[box_bounds.clone()])
|| contains_symbol(&prev1[box_bounds.clone()])
|| contains_symbol(&next[box_bounds])
{
acc += number;
}
}
}
prev2 = prev1;
prev1 = next;
}
acc
}
helper::tests! {
day03 Day03;
part1 {

81
2023/day03/src/p1.rs Normal file
View file

@ -0,0 +1,81 @@
pub fn basic(input: &str) -> u64 {
fn contains_symbol(s: &str) -> bool {
s.chars().any(|c| !c.is_ascii_digit() && c != '.')
}
let len = input.lines().next().unwrap().len();
let empty_border = std::iter::repeat('.').take(len).collect::<String>();
let mut prev2 = empty_border.as_str();
let mut prev1 = input.lines().next().unwrap();
let mut acc = 0;
for next in input.lines().skip(1).chain(Some(empty_border.as_str())) {
let mut numbers = prev1.char_indices().peekable();
while let Some((start, c)) = numbers.next() {
if c.is_ascii_digit() {
let mut end = start;
while let Some((idx, '0'..='9')) = numbers.next() {
end = idx;
}
let box_bounds = (start.saturating_sub(1))..std::cmp::min(end + 2, len);
let number = prev1[start..=end].parse::<u64>().unwrap();
if contains_symbol(&prev2[box_bounds.clone()])
|| contains_symbol(&prev1[box_bounds.clone()])
|| contains_symbol(&next[box_bounds])
{
acc += number;
}
}
}
prev2 = prev1;
prev1 = next;
}
acc
}
pub fn bytes(input: &str) -> u64 {
fn contains_symbol(s: &str) -> bool {
s.bytes().any(|c| !c.is_ascii_digit() && c != b'.')
}
let len = input.lines().next().unwrap().len();
let empty_border = std::iter::repeat('.').take(len).collect::<String>();
let mut prev2 = empty_border.as_str();
let mut prev1 = input.lines().next().unwrap();
let mut acc = 0;
for next in input.lines().skip(1).chain(Some(empty_border.as_str())) {
let mut numbers = prev1.bytes().enumerate().peekable();
while let Some((start, c)) = numbers.next() {
if c.is_ascii_digit() {
let mut end = start;
while let Some((idx, b'0'..=b'9')) = numbers.next() {
end = idx;
}
let box_bounds = (start.saturating_sub(1))..std::cmp::min(end + 2, len);
let number = prev1[start..=end].parse::<u64>().unwrap();
if contains_symbol(&prev2[box_bounds.clone()])
|| contains_symbol(&prev1[box_bounds.clone()])
|| contains_symbol(&next[box_bounds])
{
acc += number;
}
}
}
prev2 = prev1;
prev1 = next;
}
acc
}