2023-13: Implement part one

This commit is contained in:
Mathis 2023-12-23 17:05:09 +01:00
parent 7b0d96551a
commit 4148a107ef
5 changed files with 1499 additions and 0 deletions

1319
input/2023/day_13/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -0,0 +1,25 @@
pub mod part_one;
// pub mod part_two;
// pub mod common;
use crate::utils::solution::Solution;
use crate::utils::input_reader;
pub struct DaySolution ;
impl Solution for DaySolution {
fn input_path(&self) -> &'static str {
"input/2023/day_13/input.txt"
}
fn part_one(&self) -> String {
input_reader::read_input_file(self.input_path(), part_one::part_one)
}
fn part_two(&self) -> String {
// input_reader::read_input_file(self.input_path(), part_two::part_two)
String::from("Not implemented")
}
}

View File

@ -0,0 +1,138 @@
pub fn part_one(input_lines: Vec<String>) -> String {
let mut patterns: Vec<Vec<Vec<bool>>> = Vec::new();
let mut acc: Vec<Vec<bool>> = Vec::new();
for line in input_lines {
if line.is_empty() && !acc.is_empty() {
patterns.push(acc.clone());
acc = Vec::new();
continue;
}
let mut line_vec: Vec<bool> = Vec::new();
for c in line.chars() {
if c == '#' {
line_vec.push(true);
} else {
line_vec.push(false);
}
}
acc.push(line_vec);
}
if !acc.is_empty() {
patterns.push(acc);
}
let mut result = 0;
// println!("Found {} patterns", patterns.len());
for pattern in patterns {
let val = process_pattern(&pattern);
result += val;
}
result.to_string()
// String::from("Not implemented")
}
fn find_same_rows(pattern: &Vec<Vec<bool>>) -> Vec<usize> {
let mut res: Vec<usize> = Vec::new();
for i in 1..pattern.len() {
if pattern[i] == pattern[i-1] {
res.push(i);
}
}
res
}
fn find_same_cols(pattern: &Vec<Vec<bool>>) -> Vec<usize> {
let mut res: Vec<usize> = Vec::new();
for i in 1..pattern[0].len() {
let col_i: Vec<bool> = pattern.iter().map(|x| x[i]).collect();
let col_j: Vec<bool> = pattern.iter().map(|x| x[i-1]).collect();
if col_i == col_j {
res.push(i)
}
}
res
}
fn validate_row_pattern(pattern: &Vec<Vec<bool>>, mirror_idx: usize) -> bool {
let mut i = mirror_idx - 1;
let mut j = mirror_idx;
while j < pattern.len() {
if pattern[i] != pattern[j] {
return false;
}
if i == 0 {
break;
}
i -= 1;
j += 1;
}
true
}
fn validate_col_pattern(pattern: &Vec<Vec<bool>>, mirror_idx: usize) -> bool {
let mut i = mirror_idx - 1;
let mut j = mirror_idx;
while j < pattern[0].len() {
let col_i: Vec<bool> = pattern.iter().map(|x| x[i]).collect();
let col_j: Vec<bool> = pattern.iter().map(|x| x[j]).collect();
if col_i != col_j {
return false;
}
if i == 0 {
break;
}
i -= 1;
j += 1;
}
true
}
fn process_pattern(pattern: &Vec<Vec<bool>>) -> usize {
let mut mirror_indexes = find_same_rows(&pattern);
if !mirror_indexes.is_empty() {
for mirror_idx in mirror_indexes {
if validate_row_pattern(&pattern, mirror_idx) {
return 100 * mirror_idx
}
}
}
mirror_indexes = find_same_cols(&pattern);
if !mirror_indexes.is_empty() {
for mirror_idx in mirror_indexes {
if validate_col_pattern(&pattern, mirror_idx) {
return mirror_idx
}
}
}
panic!("Invalid pattern");
}

View File

@ -10,6 +10,7 @@ pub mod day_09_mirage_maintenance;
pub mod day_10_pipe_maze;
pub mod day_11_cosmic_expantion;
pub mod day_12_hot_springs;
pub mod day_13_point_of_incidence;
pub fn run(day: &str) {
let solution: Box<dyn Solution> = match day {
@ -23,6 +24,7 @@ pub fn run(day: &str) {
"10" => Box::new(day_10_pipe_maze::DaySolution),
"11" => Box::new(day_11_cosmic_expantion::DaySolution),
"12" => Box::new(day_12_hot_springs::DaySolution),
"13" => Box::new(day_13_point_of_incidence::DaySolution),
_ => panic!("Invalid day specified"),
};