diff options
| author | nekineki <nekineki@nekineki.net> | 2023-12-15 19:19:47 +0100 |
|---|---|---|
| committer | nekineki <nekineki@nekineki.net> | 2023-12-15 19:19:47 +0100 |
| commit | 126af40828e92869410fa11eeb6d28847622a4d5 (patch) | |
| tree | 870af93b25155b828e54600ddc7600bb3fca9486 /2023/day12.rs | |
| parent | ec665a8eff948785b635d568610659e50dd11fe9 (diff) | |
day12 part1
Diffstat (limited to '2023/day12.rs')
| -rw-r--r-- | 2023/day12.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/2023/day12.rs b/2023/day12.rs new file mode 100644 index 0000000..0fe4b97 --- /dev/null +++ b/2023/day12.rs @@ -0,0 +1,99 @@ +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(unused_mut)] +use std::fs::File; +use std::io::Read; + +#[derive(Eq, Debug, PartialEq, Clone, Copy)] +enum State { + Ok, + Nok, + Unk, +} + +type Line = (Vec<State>, Vec<usize>); + +fn rec(l: Line) -> usize { + for i in 0..l.0.len() { + if l.0[i] == State::Unk { + let mut sum = 0; + let mut l1 = l.clone(); + let mut l2 = l.clone(); + l1.0[i] = State::Ok; + l2.0[i] = State::Nok; + sum += rec(l1); + sum += rec(l2); + return sum; + } + } + + if is_valid(l) { + return 1; + } else { + return 0; + } +} + +fn is_valid(l: Line) -> bool { + let mut a: Vec<usize> = Vec::new(); + + let mut prev = State::Ok; + let mut count = 0; + + for i in 0..l.0.len() { + if l.0[i] == State::Nok { + count += 1; + } else if l.0[i] == State::Ok && prev == State::Nok { + a.push(count); + count = 0; + } + prev = l.0[i]; + } + if count != 0 { + a.push(count); + } + + let ret = a == l.1; + ret +} + +fn main() { + // let filename = "in/day12.ref"; + let filename = "in/day12.pzl"; + + let mut f = File::open(filename).expect("cannot open file"); + let mut content = String::new(); + f.read_to_string(&mut content).expect("cannot read file"); + let lines = content.trim_end().split('\n'); + + let mut ls: Vec<Line> = Vec::new(); + for line in lines { + let (a, b) = line.split_once(' ').unwrap(); + let mut l: Line = (Vec::new(), Vec::new()); + for c in a.chars() { + let s = match c { + '.' => State::Ok, + '#' => State::Nok, + '?' => State::Unk, + _ => panic!(), + }; + l.0.push(s); + } + for c in b.split(',') { + l.1.push(c.parse().unwrap()); + } + ls.push(l); + } + + let mut res1 = 0; + let mut res2 = 0; + + for l in ls { + let a = rec(l); + // println!("{}", a); + res1 += a; + } + + println!("res1: {}", res1); + println!("res2: {}", res2); +} |
