-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
140 additions
and
35 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
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::knot_hash::knot_hash; | ||
use utils::bit::BitIterator; | ||
use utils::prelude::*; | ||
|
||
/// Finding connected regions in a hash-derived grid. | ||
/// | ||
/// This puzzle is a combination of [`Day10`](crate::Day10), which introduced the custom knot hash | ||
/// function used to create the grid, and [`Day12`](crate::Day12), which also involved finding | ||
/// connected components. | ||
#[derive(Clone, Debug)] | ||
pub struct Day14 { | ||
grid: [u128; 128], | ||
} | ||
|
||
impl Day14 { | ||
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> { | ||
let mut grid = [0u128; 128]; | ||
|
||
let mut buf = Vec::with_capacity(input.len() + 4); | ||
buf.extend_from_slice(input.as_bytes()); | ||
buf.push(b'-'); | ||
|
||
for (i, row) in grid.iter_mut().enumerate() { | ||
buf.truncate(input.len() + 1); | ||
if i < 10 { | ||
buf.push(b'0' + (i as u8)); | ||
} else if i < 100 { | ||
buf.push(b'0' + ((i / 10) as u8)); | ||
buf.push(b'0' + ((i % 10) as u8)); | ||
} else { | ||
buf.push(b'0' + ((i / 100) as u8)); | ||
buf.push(b'0' + (((i / 10) % 10) as u8)); | ||
buf.push(b'0' + ((i % 10) as u8)); | ||
} | ||
|
||
let hash = knot_hash(buf.iter().copied()); | ||
*row = u128::from_be_bytes(hash); | ||
} | ||
|
||
Ok(Day14 { grid }) | ||
} | ||
|
||
#[must_use] | ||
pub fn part1(&self) -> u32 { | ||
self.grid.iter().map(|x| x.count_ones()).sum() | ||
} | ||
|
||
#[must_use] | ||
pub fn part2(&self) -> u32 { | ||
let mut regions = 0; | ||
let mut visited = [0u128; 128]; | ||
|
||
for (r, &row) in self.grid.iter().enumerate() { | ||
for (_, bit) in BitIterator::ones(row) { | ||
if visited[r] & bit == 0 { | ||
regions += 1; | ||
self.visit(&mut visited, r, bit) | ||
} | ||
} | ||
} | ||
|
||
regions | ||
} | ||
|
||
fn visit(&self, visited: &mut [u128; 128], r: usize, bit: u128) { | ||
visited[r] |= bit; | ||
|
||
if r > 0 && self.grid[r - 1] & bit != 0 && visited[r - 1] & bit == 0 { | ||
self.visit(visited, r - 1, bit); | ||
} | ||
if r < 127 && self.grid[r + 1] & bit != 0 && visited[r + 1] & bit == 0 { | ||
self.visit(visited, r + 1, bit); | ||
} | ||
|
||
let left = bit << 1; | ||
if left != 0 && self.grid[r] & left != 0 && visited[r] & left == 0 { | ||
self.visit(visited, r, left); | ||
} | ||
let right = bit >> 1; | ||
if right != 0 && self.grid[r] & right != 0 && visited[r] & right == 0 { | ||
self.visit(visited, r, right); | ||
} | ||
} | ||
} | ||
|
||
examples!(Day14 -> (u32, u32) [ | ||
{input: "flqrgnkx", part1: 8108, part2: 1242}, | ||
]); |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! Knot Hash implementation. | ||
//! | ||
//! See [`Day10`](crate::Day10) and [`Day14`](crate::Day14). | ||
use std::array; | ||
use utils::md5; | ||
|
||
#[inline] | ||
pub(crate) fn knot_rounds(lengths: impl Iterator<Item = u8> + Clone, rounds: u32) -> [u8; 256] { | ||
let mut list = array::from_fn(|i| i as u8); | ||
let mut position = 0; | ||
let mut skip = 0; | ||
|
||
for _ in 0..rounds { | ||
for length in lengths.clone() { | ||
list[0..length as usize].reverse(); | ||
list.rotate_left((length as usize + skip) % 256); | ||
position = (position + length as usize + skip) % 256; | ||
skip += 1; | ||
} | ||
} | ||
|
||
list.rotate_right(position); | ||
list | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn knot_hash(lengths: impl Iterator<Item = u8> + Clone) -> [u8; 16] { | ||
let sparse = knot_rounds(lengths.chain([17, 31, 73, 47, 23]), 64); | ||
|
||
array::from_fn(|i| { | ||
sparse[16 * i..16 * (i + 1)] | ||
.iter() | ||
.fold(0, |acc, x| acc ^ x) | ||
}) | ||
} | ||
|
||
#[inline] | ||
pub(crate) fn knot_hash_hex(lengths: impl Iterator<Item = u8> + Clone) -> [u8; 32] { | ||
let hash = knot_hash(lengths); | ||
|
||
md5::to_hex(array::from_fn(|i| { | ||
u32::from_be_bytes(hash[4 * i..4 * (i + 1)].try_into().unwrap()) | ||
})) | ||
} |
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