-
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
1 parent
0fb71f3
commit 707d37f
Showing
8 changed files
with
170 additions
and
3 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
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
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 @@ | ||
11100010111110100 |
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,133 @@ | ||
use itertools::Itertools; | ||
|
||
const DIVIDERS: [usize; 15] = [ | ||
65536, | ||
32768, | ||
16384, | ||
8192, | ||
4096, | ||
2048, | ||
1024, | ||
512, | ||
256, | ||
128, | ||
64, | ||
32, | ||
16, | ||
8, | ||
4, | ||
]; | ||
|
||
#[inline] | ||
fn string2bools(input: &str) -> Vec<bool> { | ||
input | ||
.trim_end() | ||
.chars() | ||
.map(|c| if c == '1' { true } else { false }) | ||
.collect_vec() | ||
} | ||
|
||
#[inline] | ||
fn bools2string(input: Vec<bool>) -> String { | ||
input | ||
.into_iter() | ||
.map(|b| if b { '1' } else { '0' }) | ||
.collect() | ||
} | ||
|
||
fn fill_until(mut input: Vec<bool>, disk_size: usize) -> Vec<bool> { | ||
while input.len() < disk_size { | ||
let mut reverse = input.iter().rev().map(|b| !b).collect_vec(); | ||
input.push(false); | ||
input.append(&mut reverse); | ||
} | ||
input.truncate(disk_size); | ||
|
||
input | ||
} | ||
|
||
#[inline] | ||
fn fold16(input: &[bool]) -> bool { | ||
fold8(&input[0..8]) == fold8(&input[8..16]) | ||
} | ||
|
||
#[inline] | ||
fn fold8(input: &[bool]) -> bool { | ||
fold4(&input[0..4]) == fold4(&input[4..8]) | ||
} | ||
|
||
#[inline] | ||
fn fold4(input: &[bool]) -> bool { | ||
(input[0] == input[1]) == (input[2] == input[3]) | ||
} | ||
|
||
#[inline] | ||
fn completely_reduce(mut input: Vec<bool>) -> bool { | ||
while input.len() % 16 == 0 { | ||
input = input.chunks(16).map(|chunk| fold16(chunk)).collect_vec(); | ||
} | ||
while input.len() % 8 == 0 { | ||
input = input.chunks(8).map(|chunk| fold8(chunk)).collect_vec(); | ||
} | ||
while input.len() % 4 == 0 { | ||
input = input.chunks(4).map(|chunk| fold4(chunk)).collect_vec(); | ||
} | ||
if input.len() == 2 { | ||
return input[0] == input[1]; | ||
} | ||
input[0] | ||
} | ||
|
||
fn checksum(input: Vec<bool>) -> Vec<bool> { | ||
let mut results = input; | ||
for divider in DIVIDERS { | ||
while results.len() % divider == 0 { | ||
results = results | ||
.chunks(divider) | ||
.map(|chunk| completely_reduce(chunk.to_vec())) | ||
.collect_vec() | ||
} | ||
} | ||
|
||
while results.len() % 2 == 0 { | ||
results = results | ||
.chunks(2) | ||
.map(|pair| pair[0] == pair[1]) | ||
.collect_vec() | ||
} | ||
|
||
results | ||
} | ||
|
||
pub fn calculate_checksum(input: &str, disk_size: usize) -> String { | ||
let mut values = string2bools(&input); | ||
values = fill_until(values, disk_size); | ||
values = checksum(values); | ||
|
||
bools2string(values) | ||
} | ||
|
||
const DISK_SIZE_V1: usize = 272; | ||
|
||
pub fn day_16_v1(input: impl Into<String>) -> String { | ||
calculate_checksum(&input.into(), DISK_SIZE_V1) | ||
} | ||
|
||
const DISK_SIZE_V2: usize = 35_651_584; | ||
|
||
pub fn day_16_v2(input: impl Into<String>) -> String { | ||
calculate_checksum(&input.into(), DISK_SIZE_V2) | ||
} | ||
|
||
solvable!(day_16, day_16_v1, day_16_v2, String); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn works_with_samples() { | ||
let sample_one = "10000"; | ||
assert_eq!(calculate_checksum(sample_one, 20), "01100"); | ||
} | ||
} |