From 159c41390cd89179b09e4bbd34a441d5730fcbbe Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Sun, 3 Dec 2023 14:21:15 +0530 Subject: [PATCH] day3: optimized p2 and p1 --- src/day3/1.rs | 1 - src/day3/2.rs | 38 ++++++++++++++++++++------------------ src/day3/sample.txt | 2 +- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/day3/1.rs b/src/day3/1.rs index 458b3ae..73f0d52 100644 --- a/src/day3/1.rs +++ b/src/day3/1.rs @@ -43,7 +43,6 @@ fn process(data: &[u8]) -> u64 { } let mut valid = false; - 'outer: for (a, b) in DIRS.iter() { let x = i as i32 + a; diff --git a/src/day3/2.rs b/src/day3/2.rs index 1584b38..1c35d62 100644 --- a/src/day3/2.rs +++ b/src/day3/2.rs @@ -1,6 +1,6 @@ #![feature(test)] -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; extern crate test; const INPUTS: [&[u8]; 2] = [ @@ -30,13 +30,10 @@ fn process(data: &[u8]) -> u64 { let m = grid.len(); let n = grid[0].len(); - let mut map = HashMap::with_capacity(200); - + let mut set = HashSet::with_capacity(2); for i in 0..m { for j in 0..n { - let c = grid[i][j]; - - if c != b'*' { + if grid[i][j] != b'*' { continue; } @@ -65,26 +62,31 @@ fn process(data: &[u8]) -> u64 { ey += 1; } - let num = String::from_utf8_lossy(&grid[x][sy..ey]) - .parse::() - .unwrap(); + // only insert valid parts in this + if set.len() < 2 { + set.insert((parse(&grid[x][sy..ey]), sy, ey, x)); + } + } - map.entry((i, j)) - .or_insert_with(HashSet::new) - .insert((num, x, sy, ey)); + if set.len() == 2 { + total += set.drain().fold(1, |a, (x, _, _, _)| a * x); + } else { + set.clear(); } } } - for v in map.into_values() { - if v.len() < 2 { - continue; - } + total +} - total += v.into_iter().fold(1, |a, (b, _, _, _)| a * b); +fn parse(b: &[u8]) -> u64 { + let mut output = 0; + + for (i, v) in b.iter().enumerate() { + output += 10u64.pow(b.len() as u32 - i as u32 - 1) * ((v - b'0') as u64); } - total + output } fn main() { diff --git a/src/day3/sample.txt b/src/day3/sample.txt index 624ea4f..b20187f 100644 --- a/src/day3/sample.txt +++ b/src/day3/sample.txt @@ -7,4 +7,4 @@ ..592..... ......755. ...$.*.... -.664.598.. \ No newline at end of file +.664.598..