diff --git a/src/12part1.rs b/src/12part1.rs new file mode 100644 index 0000000..457bc60 --- /dev/null +++ b/src/12part1.rs @@ -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 = line_components.next().unwrap().chars().map(line_to_enum).collect(); + let damaged: Vec = line_components.next().unwrap().split(",").map(|s| s.parse::().ok().unwrap()).collect(); + let mut subtotal = 0; + let mut possible: Vec> = 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, + } +}