Skip to content

Commit

Permalink
2023-13
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 14, 2023
1 parent faa5c75 commit 77f368c
Show file tree
Hide file tree
Showing 2 changed files with 1,411 additions and 7 deletions.
69 changes: 62 additions & 7 deletions crates/core/src/year2023/day13.rs
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);
}
Loading

1 comment on commit 77f368c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@                     Benchmark Difference                    @@
#     Name   Old (instructions)   New (instructions)   Change (%)
  2023_1_1            1,453,000            1,453,000            0
  2023_1_2            1,426,870            1,426,870            0
  2023_2_1              614,077              614,077            0
  2023_2_2              590,018              590,018            0
  2023_3_1              928,553              928,553            0
  2023_3_2              674,566              674,566            0
Benchmark Instructions (count) Instructions (%)
2023_1_1 1,453,000 25.5
2023_1_2 1,426,870 25.1
2023_3_1 928,553 16.3
2023_3_2 674,566 11.9
2023_2_1 614,077 10.8
2023_2_2 590,018 10.4

Please sign in to comment.