day11: optimized

This commit is contained in:
Ishan Jain 2023-12-11 14:50:34 +05:30
parent 0024d9175c
commit 80f9c4195b
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 65 additions and 69 deletions

View File

@ -2,53 +2,50 @@
extern crate test; extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; const INPUTS: [&[u8]; 2] = [
include_bytes!("./sample.txt"),
include_bytes!("./input.txt"),
];
fn process(data: &str) -> usize { fn process(data: &[u8]) -> usize {
let grid: Vec<Vec<char>> = data let mut size = 1;
.lines() let mut i = 0;
.filter(|x| !x.is_empty()) while i < data.len() - 1 {
.map(|x| x.chars().collect()) if data[i] == b'\n' {
.collect(); i += (data[i + 1] == b'\n') as usize;
size += 1;
let n = grid[0].len();
let mut blank_cols = Vec::new();
for j in 0..n {
let mut all_empty = true;
for row in grid.iter() {
if row[j] != '.' {
all_empty = false;
break;
} }
i += 1;
} }
if all_empty { let input: Vec<u8> = data.iter().filter(|&&x| x != b'\n').cloned().collect();
blank_cols.push(j);
}
}
let mut nodes = vec![];
let mut total_row_offset = 0; let mut total_row_offset = 0;
for (i, row) in grid.iter().enumerate() { let mut total_col_offset = vec![0; size];
if !row.iter().any(|&x| x == '#') { let mut nodes = vec![];
for i in 0..size {
if !input[size * i..size * i + size].iter().any(|&x| x == b'#') {
total_row_offset += 1; total_row_offset += 1;
} }
let mut total_col_offset = 0;
let mut j_offset = 0;
for j in 0..n { for j in 0..size {
if j_offset < blank_cols.len() && j >= blank_cols[j_offset] { if total_col_offset[j] == 0
total_col_offset += 1; && !input[j..size * size]
j_offset += 1; .iter()
.step_by(size)
.any(|&x| x != b'.')
{
total_col_offset[j] = total_col_offset[j - 1] + 1;
} else if j > 0 && total_col_offset[j] == 0 {
total_col_offset[j] += total_col_offset[j - 1];
} }
if grid[i][j] != '#' {
if input[i * size + j] != b'#' {
continue; continue;
} }
nodes.push(((i + total_row_offset), (j + total_col_offset))); nodes.push((i + total_row_offset, j + total_col_offset[j]));
} }
} }

View File

@ -2,55 +2,54 @@
extern crate test; extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; const INPUTS: [&[u8]; 2] = [
include_bytes!("./sample.txt"),
include_bytes!("./input.txt"),
];
const MULTIPLIER: usize = 999999; const MULTIPLIER: usize = 999999;
fn process(data: &str) -> usize { fn process(data: &[u8]) -> usize {
let grid: Vec<Vec<char>> = data let mut size = 1;
.lines() let mut i = 0;
.filter(|x| !x.is_empty()) while i < data.len() - 1 {
.map(|x| x.chars().collect()) if data[i] == b'\n' {
.collect(); i += (data[i + 1] == b'\n') as usize;
size += 1;
let n = grid[0].len();
let mut blank_cols = Vec::new();
for j in 0..n {
let mut all_empty = true;
for row in grid.iter() {
if row[j] != '.' {
all_empty = false;
break;
} }
i += 1;
} }
if all_empty { let input: Vec<u8> = data.iter().filter(|&&x| x != b'\n').cloned().collect();
blank_cols.push(j);
}
}
let mut nodes = vec![];
let mut total_row_offset = 0; let mut total_row_offset = 0;
for (i, row) in grid.iter().enumerate() { let mut total_col_offset = vec![0; size];
if !row.iter().any(|&x| x == '#') { let mut nodes = vec![];
for i in 0..size {
// size * i is offset for the start of the row;
if !input[size * i..size * i + size].iter().any(|&x| x == b'#') {
total_row_offset += MULTIPLIER; total_row_offset += MULTIPLIER;
} }
let mut total_col_offset = 0;
let mut j_offset = 0;
for j in 0..n { for j in 0..size {
if j_offset < blank_cols.len() && j >= blank_cols[j_offset] { if total_col_offset[j] == 0
total_col_offset += MULTIPLIER; && !input[j..size * size]
j_offset += 1; .iter()
.step_by(size)
.any(|&x| x != b'.')
{
total_col_offset[j] = total_col_offset[j - 1] + MULTIPLIER
} else if j > 0 && total_col_offset[j] == 0 {
total_col_offset[j] += total_col_offset[j - 1];
} }
if grid[i][j] != '#' {
if input[i * size + j] != b'#' {
continue; continue;
} }
nodes.push(((i + total_row_offset), (j + total_col_offset))); nodes.push((i + total_row_offset, j + total_col_offset[j]));
} }
} }
@ -73,7 +72,7 @@ fn main() {
} }
#[bench] #[bench]
fn part1(b: &mut test::Bencher) { fn part2(b: &mut test::Bencher) {
b.iter(|| { b.iter(|| {
let v = process(INPUTS[INPUTS.len() - 1]); let v = process(INPUTS[INPUTS.len() - 1]);
test::black_box(v); test::black_box(v);