diff --git a/src/day11/1.rs b/src/day11/1.rs index 0cc58ff..c6f6de5 100644 --- a/src/day11/1.rs +++ b/src/day11/1.rs @@ -2,53 +2,50 @@ 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 { - let grid: Vec> = data - .lines() - .filter(|x| !x.is_empty()) - .map(|x| x.chars().collect()) - .collect(); - - 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; - } - } - - if all_empty { - blank_cols.push(j); +fn process(data: &[u8]) -> usize { + let mut size = 1; + let mut i = 0; + while i < data.len() - 1 { + if data[i] == b'\n' { + i += (data[i + 1] == b'\n') as usize; + size += 1; } + i += 1; } - let mut nodes = vec![]; + let input: Vec = data.iter().filter(|&&x| x != b'\n').cloned().collect(); let mut total_row_offset = 0; - for (i, row) in grid.iter().enumerate() { - if !row.iter().any(|&x| x == '#') { + let mut total_col_offset = vec![0; size]; + 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; } - let mut total_col_offset = 0; - let mut j_offset = 0; - for j in 0..n { - if j_offset < blank_cols.len() && j >= blank_cols[j_offset] { - total_col_offset += 1; - j_offset += 1; + for j in 0..size { + if total_col_offset[j] == 0 + && !input[j..size * size] + .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; } - nodes.push(((i + total_row_offset), (j + total_col_offset))); + nodes.push((i + total_row_offset, j + total_col_offset[j])); } } diff --git a/src/day11/2.rs b/src/day11/2.rs index dcc4295..65a13f1 100644 --- a/src/day11/2.rs +++ b/src/day11/2.rs @@ -2,55 +2,54 @@ 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; -fn process(data: &str) -> usize { - let grid: Vec> = data - .lines() - .filter(|x| !x.is_empty()) - .map(|x| x.chars().collect()) - .collect(); - - 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; - } - } - - if all_empty { - blank_cols.push(j); +fn process(data: &[u8]) -> usize { + let mut size = 1; + let mut i = 0; + while i < data.len() - 1 { + if data[i] == b'\n' { + i += (data[i + 1] == b'\n') as usize; + size += 1; } + i += 1; } - let mut nodes = vec![]; + let input: Vec = data.iter().filter(|&&x| x != b'\n').cloned().collect(); let mut total_row_offset = 0; - for (i, row) in grid.iter().enumerate() { - if !row.iter().any(|&x| x == '#') { + let mut total_col_offset = vec![0; size]; + 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; } - let mut total_col_offset = 0; - let mut j_offset = 0; - for j in 0..n { - if j_offset < blank_cols.len() && j >= blank_cols[j_offset] { - total_col_offset += MULTIPLIER; - j_offset += 1; + for j in 0..size { + if total_col_offset[j] == 0 + && !input[j..size * size] + .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; } - 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] -fn part1(b: &mut test::Bencher) { +fn part2(b: &mut test::Bencher) { b.iter(|| { let v = process(INPUTS[INPUTS.len() - 1]); test::black_box(v);