143 lines
4.1 KiB
Rust
143 lines
4.1 KiB
Rust
use std::fs;
|
|
|
|
pub fn solve() {
|
|
let path = "res/13/input";
|
|
|
|
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
|
|
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
|
|
|
|
let patterns = contents
|
|
.split("\n\n")
|
|
.map(|block| {
|
|
block
|
|
.lines()
|
|
.map(|line| line.chars().collect::<Vec<_>>())
|
|
.collect::<Vec<_>>()
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
let result: usize = patterns
|
|
.iter()
|
|
.map(|block| {
|
|
|
|
// check columns
|
|
let mut ks: Vec<usize> = vec![];
|
|
for k in 1..block[0].len() {
|
|
if (0..k.min(block[0].len() - k)).all(|j| {
|
|
let col1: Vec<char> = (0..block.len()).map(|i| block[i][k - 1 - j]).collect();
|
|
let col2: Vec<char> = (0..block.len()).map(|i| block[i][k + j]).collect();
|
|
col1.eq(&col2)
|
|
}) {
|
|
ks.push(k)
|
|
}
|
|
}
|
|
|
|
if ks.len() > 1 {
|
|
dbg!(&ks);
|
|
}
|
|
if !ks.is_empty() {
|
|
return ks[ks.len() / 2];
|
|
}
|
|
|
|
// check rows
|
|
let mut ks: Vec<usize> = vec![];
|
|
for k in 1..block.len() {
|
|
if (0..k.min(block.len() - k)).all(|i| block[k - 1 - i].eq(&block[k + i])) {
|
|
ks.push(k);
|
|
}
|
|
}
|
|
|
|
if ks.len() > 1 {
|
|
dbg!(&ks);
|
|
}
|
|
if !ks.is_empty() {
|
|
return ks[ks.len() / 2] * 100;
|
|
}
|
|
|
|
0
|
|
})
|
|
.sum();
|
|
|
|
println!("Result 1: {result}");
|
|
|
|
let result: usize = patterns
|
|
.iter()
|
|
.map(|block| {
|
|
let mut smudge_repaired = false;
|
|
|
|
// check columns
|
|
let mut ks: Vec<usize> = vec![];
|
|
for k in 1..block[0].len() {
|
|
if (0..k.min(block[0].len() - k)).all(|j| {
|
|
let col1: Vec<char> = (0..block.len()).map(|i| block[i][k - 1 - j]).collect();
|
|
let col2: Vec<char> = (0..block.len()).map(|i| block[i][k + j]).collect();
|
|
if col1.eq(&col2) {
|
|
return true;
|
|
}
|
|
if col1
|
|
.iter()
|
|
.zip(col2.iter())
|
|
.filter(|&(a, b)| a != b)
|
|
.count()
|
|
== 1
|
|
&& !smudge_repaired
|
|
{
|
|
smudge_repaired = true;
|
|
return true;
|
|
}
|
|
false
|
|
}) {
|
|
if smudge_repaired {
|
|
ks.push(k)
|
|
}
|
|
}
|
|
smudge_repaired = false
|
|
}
|
|
|
|
if ks.len() > 1 {
|
|
dbg!(&ks);
|
|
}
|
|
if !ks.is_empty() {
|
|
return ks[ks.len() / 2];
|
|
}
|
|
|
|
// check rows
|
|
ks.clear();
|
|
for k in 1..block.len() {
|
|
if (0..k.min(block.len() - k)).all(|i| {
|
|
if block[k - 1 - i].eq(&block[k + i]) {
|
|
return true;
|
|
}
|
|
if block[k - 1 - i]
|
|
.iter()
|
|
.zip(block[k + i].iter())
|
|
.filter(|&(a, b)| a != b)
|
|
.count()
|
|
== 1
|
|
&& !smudge_repaired
|
|
{
|
|
smudge_repaired = true;
|
|
return true;
|
|
}
|
|
false
|
|
}) {
|
|
if smudge_repaired {
|
|
ks.push(k)
|
|
}
|
|
}
|
|
smudge_repaired = false
|
|
}
|
|
|
|
if ks.len() > 1 {
|
|
dbg!(&ks);
|
|
}
|
|
if !ks.is_empty() {
|
|
return ks[ks.len() / 2] * 100;
|
|
}
|
|
|
|
0
|
|
})
|
|
.sum();
|
|
|
|
println!("Result 2: {result}");
|
|
}
|