2023-12: Implement first part

This commit is contained in:
Mathis 2023-12-22 19:52:14 +01:00
parent e520118ac2
commit b0447bed64
6 changed files with 1148 additions and 0 deletions

1000
input/2023/day_12/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1

View File

@ -0,0 +1,6 @@
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Spring {
Operational,
Damaged,
Unknown,
}

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_12/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,109 @@
use super::common::Spring;
pub fn part_one(input_lines: Vec<String>) -> String {
let mut springs: Vec<Vec<Spring>> = Vec::new();
let mut values: Vec<Vec<usize>> = Vec::new();
for line in input_lines {
let split_line: Vec<&str> = line.split(' ').collect();
springs.push(parse_springs(split_line[0].to_string()));
values.push(parse_nums(split_line[1].to_string()));
}
// for i in 0..springs.len() {
// println!("{:?} - {:?}", springs[i], values[i]);
// }
let mut res = 0;
for i in 0..springs.len() {
let search_spring = springs[i].clone();
res += unknown_search(search_spring, values[i].clone());
}
res.to_string()
}
fn parse_springs(line: String) -> Vec<Spring> {
let mut spring_line: Vec<Spring> = Vec::new();
for c in line.chars() {
match c {
'.' => spring_line.push(Spring::Operational),
'#' => spring_line.push(Spring::Damaged),
_ => spring_line.push(Spring::Unknown),
}
}
spring_line
}
fn parse_nums(line: String) -> Vec<usize> {
let nums: Vec<usize> = line.split(',').map(|x| x.parse().unwrap()).collect();
nums
}
fn validate(spring_line: Vec<Spring>, values: Vec<usize>) -> usize {
let mut validation_vals: Vec<usize> = Vec::new();
let mut count: usize = 0;
// println!("{:?}: {:?}", spring_line, values);
for i in 0..spring_line.len() {
match spring_line[i] {
Spring::Operational => {
if count > 0 {
validation_vals.push(count);
count = 0;
}
},
Spring::Damaged => {
count += 1;
},
_ => {
panic!("Invalid spring");
},
}
}
if count > 0 {
validation_vals.push(count);
}
if validation_vals == values {
return 1;
}
return 0;
}
fn unknown_search(spring_line: Vec<Spring>, values: Vec<usize>) -> usize {
// println!("Unknown search: {:?}", spring_line);
let mut res = 0;
let unknowns: Vec<usize> = spring_line.iter().enumerate().filter(|(_, x)| **x == Spring::Unknown).map(|(i, _)| i).collect();
let i = unknowns.first();
if i.is_none() {
return validate(spring_line, values);
}
let i = i.unwrap();
let mut op_spring_line = spring_line.clone();
op_spring_line[*i] = Spring::Operational;
res += unknown_search(op_spring_line, values.clone());
let mut dmg_spring_line = spring_line.clone();
dmg_spring_line[*i] = Spring::Damaged;
res += unknown_search(dmg_spring_line, values.clone());
res
}

View File

@ -9,6 +9,7 @@ pub mod day_08_haunted_wasteland;
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 fn run(day: &str) {
let solution: Box<dyn Solution> = match day {
@ -21,6 +22,7 @@ pub fn run(day: &str) {
"9" => Box::new(day_09_mirage_maintenance::DaySolution),
"10" => Box::new(day_10_pipe_maze::DaySolution),
"11" => Box::new(day_11_cosmic_expantion::DaySolution),
"12" => Box::new(day_12_hot_springs::DaySolution),
_ => panic!("Invalid day specified"),
};