This commit is contained in:
JonOfUs 2023-12-13 11:55:51 +01:00
parent eac5a9dff7
commit 4f06bb8fe5
5 changed files with 1509 additions and 3 deletions

15
res/13/example Normal file
View file

@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#

1349
res/13/input Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, fs}; use std::fs;
pub fn solve() { pub fn solve() {
let path = "res/09/input"; let path = "res/09/input";

143
src/days/d13.rs Normal file
View file

@ -0,0 +1,143 @@
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}");
}

View file

@ -1,9 +1,8 @@
pub mod days; pub mod days;
#[macro_use] extern crate scan_fmt; #[macro_use] extern crate scan_fmt;
fn main() { fn main() {
days::d12::solve() days::d13::solve()
//_all_days() //_all_days()
} }