-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
1,411 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,73 @@ | ||
use crate::common::array_stack::ArrayStack; | ||
use crate::input::Input; | ||
|
||
pub const fn solve(_input: &Input) -> Result<u64, String> { | ||
Ok(0) | ||
pub fn solve(input: &Input) -> Result<usize, String> { | ||
let expected_smudges = input.part_values(0, 1); | ||
|
||
input | ||
.text | ||
.split("\n\n") | ||
.map(|part| { | ||
let mut cols = ArrayStack::<24, u32>::new(); | ||
let mut rows = ArrayStack::<24, u32>::new(); | ||
let mut num_cols = 0; | ||
for (row_idx, row) in part.lines().enumerate() { | ||
let mut row_bits = 0; | ||
for (col_idx, b) in row.bytes().enumerate() { | ||
if row_idx == 0 { | ||
cols.push(0)?; | ||
} | ||
let col_bits = &mut cols.elements[col_idx]; | ||
num_cols = num_cols.max(col_idx); | ||
if b == b'#' { | ||
row_bits |= 1 << col_idx; | ||
*col_bits |= 1 << row_idx; | ||
} | ||
} | ||
rows.push(row_bits)?; | ||
} | ||
|
||
Ok((1..cols.len()) | ||
.filter(|&col| is_reflection(cols.slice(), col, expected_smudges)) | ||
.next().unwrap_or_default() | ||
+ 100 | ||
* (1..rows.len()) | ||
.filter(|&row| is_reflection(rows.slice(), row, expected_smudges)) | ||
.next().unwrap_or_default()) | ||
}) | ||
.sum() | ||
} | ||
|
||
fn is_reflection(bits: &[u32], cols_to_left: usize, expected_smudges: u32) -> bool { | ||
let offset = cols_to_left.min(bits.len() - cols_to_left); | ||
let num_smudges: u32 = (0..offset) | ||
.map(|o| (bits[cols_to_left - o - 1] ^ bits[cols_to_left + o]).count_ones()) | ||
.sum(); | ||
num_smudges == expected_smudges | ||
} | ||
|
||
#[test] | ||
pub fn tests() { | ||
use crate::input::{test_part_one_no_allocations, test_part_two_no_allocations}; | ||
|
||
let test_input = "0"; | ||
test_part_one_no_allocations!(test_input => 0); | ||
test_part_two_no_allocations!(test_input => 0); | ||
let test_input = "#.##..##. | ||
..#.##.#. | ||
##......# | ||
##......# | ||
..#.##.#. | ||
..##..##. | ||
#.#.##.#. | ||
#...##..# | ||
#....#..# | ||
..##..### | ||
#####.##. | ||
#####.##. | ||
..##..### | ||
#....#..#"; | ||
test_part_one_no_allocations!(test_input => 405); | ||
|
||
let real_input = include_str!("day13_input.txt"); | ||
test_part_one_no_allocations!(real_input => 0); | ||
test_part_two_no_allocations!(real_input => 0); | ||
test_part_one_no_allocations!(real_input => 32_035); | ||
test_part_two_no_allocations!(real_input => 24_847); | ||
} |
Oops, something went wrong.
77f368c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.