Day 12 part 1
This commit is contained in:
parent
b8f91dee00
commit
3d52e80966
103
src/12part1.rs
Normal file
103
src/12part1.rs
Normal file
|
@ -0,0 +1,103 @@
|
|||
use std::fs;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct SpringSection {
|
||||
status: Springs,
|
||||
quantity: usize,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
|
||||
enum Springs {
|
||||
Damaged = 0,
|
||||
Unknown = 1,
|
||||
Operational = 2,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let file_path = "input/12input.txt";
|
||||
let contents = fs::read_to_string(file_path).expect("should read file");
|
||||
let mut lines = contents.lines();
|
||||
let mut total = 0;
|
||||
loop {
|
||||
let line = lines.next();
|
||||
if line.is_none() {
|
||||
break;
|
||||
}
|
||||
let mut line_components = line.unwrap().split_whitespace();
|
||||
let map: Vec<Springs> = line_components.next().unwrap().chars().map(line_to_enum).collect();
|
||||
let damaged: Vec<usize> = line_components.next().unwrap().split(",").map(|s| s.parse::<usize>().ok().unwrap()).collect();
|
||||
let mut subtotal = 0;
|
||||
let mut possible: Vec<Vec<Springs>> = Vec::new();
|
||||
for i in 0..map.len() {
|
||||
possible.push(Vec::new());
|
||||
match map[i] {
|
||||
Springs::Damaged => {
|
||||
for j in 0..possible.len() {
|
||||
possible[j].push(Springs::Damaged);
|
||||
}
|
||||
},
|
||||
Springs::Operational => {
|
||||
for j in 0..possible.len() {
|
||||
possible[j].push(Springs::Operational);
|
||||
}
|
||||
}
|
||||
Springs::Unknown => {
|
||||
let possible_len = possible.len();
|
||||
for j in 0..possible_len {
|
||||
possible.push(possible[j].to_owned());
|
||||
possible[j].push(Springs::Damaged);
|
||||
possible[possible_len+j].push(Springs::Operational);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut possible_as_sections = Vec::new();
|
||||
for i in 0..possible.len() {
|
||||
possible_as_sections.push(Vec::new());
|
||||
let mut damaged_index = 0;
|
||||
let mut current = SpringSection{status: Springs::Unknown, quantity: 0};
|
||||
let mut is_possible = possible[i].len() == map.len();
|
||||
if !is_possible {
|
||||
continue;
|
||||
}
|
||||
for j in 0..possible[i].len() {
|
||||
if possible[i][j] == current.status {
|
||||
current.quantity += 1;
|
||||
} else {
|
||||
if current.status == Springs::Damaged {
|
||||
if damaged_index == damaged.len() || damaged[damaged_index] != current.quantity {
|
||||
is_possible = false;
|
||||
break;
|
||||
} else {
|
||||
damaged_index += 1;
|
||||
}
|
||||
}
|
||||
possible_as_sections[i].push(current);
|
||||
current.status = possible[i][j];
|
||||
current.quantity = 1;
|
||||
}
|
||||
}
|
||||
if current.status == Springs::Damaged {
|
||||
if damaged_index == damaged.len() || damaged[damaged_index] != current.quantity {
|
||||
is_possible = false;
|
||||
} else {
|
||||
possible_as_sections[i].push(current);
|
||||
damaged_index += 1;
|
||||
}
|
||||
}
|
||||
if is_possible && damaged_index == damaged.len() {
|
||||
subtotal += 1;
|
||||
}
|
||||
}
|
||||
total += subtotal;
|
||||
}
|
||||
println!("{}", total);
|
||||
}
|
||||
|
||||
fn line_to_enum(c: char) -> Springs {
|
||||
match c {
|
||||
'.' => Springs::Operational,
|
||||
'#' => Springs::Damaged,
|
||||
_ => Springs::Unknown,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user