From 41bce7273647f4eacbb2e7d72bcaaf63575fe19d Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Thu, 14 Dec 2023 21:21:32 +0530 Subject: [PATCH] day14/1: optimized; --- src/day14/1.rs | 40 ++++++++++++++-------------------------- src/day14/sample.txt | 2 +- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/day14/1.rs b/src/day14/1.rs index 8a6fb60..2c41470 100644 --- a/src/day14/1.rs +++ b/src/day14/1.rs @@ -7,45 +7,33 @@ const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.t fn process(data: &str) -> usize { let size = data.lines().count(); - let mut data: Vec = data.bytes().collect(); + let data = data.as_bytes(); let mut answer = 0; - for i in 0..size { - for j in 0..size { - let c = unsafe { *data.get_unchecked(size * i + j + i) }; + let mut available_space = [0; 100]; - if c != b'O' { - continue; - } + for (i, line) in data.chunks_exact(size + 1).enumerate() { + for (c, a) in line.iter().zip(available_space.iter_mut()) { + match c { + b'#' => *a = 0, + b'.' => *a += 1, - // move the rock north - let mut start = i; - while start > 0 - && unsafe { *data.get_unchecked(size * (start - 1) + j + start - 1) } == b'.' - { - start -= 1; - } - - unsafe { - *data.get_unchecked_mut(size * i + j + i) = b'.'; - *data.get_unchecked_mut(size * start + j + start) = b'O'; + b'O' => { + let final_pos = i - *a; + let score = size - final_pos; + answer += score; + } + _ => (), } } } - for i in 0..size { - let line = unsafe { data.get_unchecked(size * i + i..size * i + i + size) }; - let count: usize = line.iter().filter(|&&x| x == b'O').count(); - - answer += count * (size - i); - } - answer } fn main() { for input in INPUTS.iter() { - println!("total = {}", process(input)); + println!("answer = {}", process(input)); } } diff --git a/src/day14/sample.txt b/src/day14/sample.txt index b92d1a3..5a24dce 100644 --- a/src/day14/sample.txt +++ b/src/day14/sample.txt @@ -7,4 +7,4 @@ O.#..O.#.# ..O..#O..O .......O.. #....###.. -#OO..#.... \ No newline at end of file +#OO..#....