1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
}
|