day14/1: optimized;

This commit is contained in:
Ishan Jain 2023-12-14 21:21:32 +05:30
parent 3fa44cb9e2
commit 41bce72736
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 15 additions and 27 deletions

View File

@ -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<u8> = 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,
b'O' => {
let final_pos = i - *a;
let score = size - final_pos;
answer += score;
}
// 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';
_ => (),
}
}
}
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));
}
}