day14/1: optimized;
This commit is contained in:
parent
3fa44cb9e2
commit
41bce72736
|
@ -7,45 +7,33 @@ const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.t
|
||||||
fn process(data: &str) -> usize {
|
fn process(data: &str) -> usize {
|
||||||
let size = data.lines().count();
|
let size = data.lines().count();
|
||||||
|
|
||||||
let mut data: Vec<u8> = data.bytes().collect();
|
let data = data.as_bytes();
|
||||||
let mut answer = 0;
|
let mut answer = 0;
|
||||||
|
|
||||||
for i in 0..size {
|
let mut available_space = [0; 100];
|
||||||
for j in 0..size {
|
|
||||||
let c = unsafe { *data.get_unchecked(size * i + j + i) };
|
|
||||||
|
|
||||||
if c != b'O' {
|
for (i, line) in data.chunks_exact(size + 1).enumerate() {
|
||||||
continue;
|
for (c, a) in line.iter().zip(available_space.iter_mut()) {
|
||||||
}
|
match c {
|
||||||
|
b'#' => *a = 0,
|
||||||
|
b'.' => *a += 1,
|
||||||
|
|
||||||
// move the rock north
|
b'O' => {
|
||||||
let mut start = i;
|
let final_pos = i - *a;
|
||||||
while start > 0
|
let score = size - final_pos;
|
||||||
&& unsafe { *data.get_unchecked(size * (start - 1) + j + start - 1) } == b'.'
|
answer += score;
|
||||||
{
|
}
|
||||||
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
|
answer
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
for input in INPUTS.iter() {
|
for input in INPUTS.iter() {
|
||||||
println!("total = {}", process(input));
|
println!("answer = {}", process(input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,4 +7,4 @@ O.#..O.#.#
|
||||||
..O..#O..O
|
..O..#O..O
|
||||||
.......O..
|
.......O..
|
||||||
#....###..
|
#....###..
|
||||||
#OO..#....
|
#OO..#....
|
||||||
|
|
Loading…
Reference in New Issue
Block a user