summaryrefslogtreecommitdiff
path: root/2023/day12.rs
blob: 87199522af547499ebf6e9c67c22d471dbfa866b (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::fmt;
use std::fs::File;
use std::io::Read;

#[derive(Eq, PartialEq, Clone, Copy)]
enum State {
    Ok,
    Nok,
    Unk,
}
impl std::fmt::Debug for State {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let a = match self {
            State::Ok => '.',
            State::Nok => '#',
            State::Unk => '?',
        };
        write!(f, "{}", a)
    }
}

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 rec2(mut s: &[State], mut arr: &[usize], depth: u32) -> usize {
    // println!("{} {:?} {:?}", depth, s, arr);

    if arr.len() == 0 {
        // println!("ret 1");
        return 1;
    }
    if s.len() == 0 {
        // println!("ret 0");
        return 0;
    }

    if s.len() < arr.iter().fold(0, |x, y| x + y) + arr.len() - 1 {
        // println!("ret 0");
        return 0;
    }

    let n = arr[0];
    let arr = &arr[1..];

    let mut ret = 0;
    for i in 0..(s.len() - n) {
        let mut all = true;
        // check before first #
        for ii in 0..i {
            if s[ii] == State::Nok {
                all = false;
            }
        }
        // check if # * n
        for ii in 0..n {
            if s[i + ii] == State::Ok {
                all = false;
            }
        }
        // check if next is not #
        if s[i + n] == State::Nok {
            all = false;
        }

        if all {
            ret += rec2(&s[i + n + 1..], arr, depth + 1);
        }
    }
    if s.len() == n {
        let mut all = true;
        for ii in 0..n {
            if s[ii] == State::Ok {
                all = false;
            }
        }
        if all {
            // println!("ret 1");
            return 1;
        }
    }

    // handle last digit and last place
    if arr.len() == 0 {
        let mut all = true;
        for i in 0..(s.len() - n) {
            // check before first #
            if s[i] == State::Nok {
                all = false;
            }
        }
        for i in (s.len() - n)..s.len() {
            // check if # * n
            if s[i] == State::Ok {
                all = false;
            }
        }
        if all {
            ret += 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;
    // }

    let mut ls2: Vec<Line> = Vec::new();
    for i in 0..ls.len() {
        let mut l2: Line = Default::default();
        for _ in 0..5 {
            l2.0.append(&mut ls[i].0.clone());
            l2.0.push(State::Unk);
            l2.1.append(&mut ls[i].1.clone());
        }
        ls2.push(l2);
    }

    // for l in [ls2[0].clone()] {
    // for l in [ls[5].clone()] {
    for l in ls2 {
        let a = rec2(&l.0, &l.1, 0);
        println!("{}", a);
        res2 += a;
    }

    println!("res1: {}", res1);
    println!("res2: {}", res2);
}