-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
159 lines (136 loc) · 3.67 KB
/
day12.rs
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
use ahash::AHashMap;
use rayon::prelude::*;
#[derive(Debug)]
struct Springs {
row: Vec<char>,
condition: Vec<i64>,
}
fn parse(input: &str) -> Vec<Springs> {
input
.lines()
.map(|line| {
let mut words = line.split_ascii_whitespace();
let row = words.next().unwrap().chars().collect();
let condition = words
.next()
.unwrap()
.split(',')
.map(|n| n.parse().unwrap())
.collect();
Springs { row, condition }
})
.collect()
}
fn prefix_valid<'a, 'b>(
prefix: &'a mut [char],
cond: &'b [i64],
) -> Option<(&'a mut [char], &'b [i64])> {
let mut num = 0;
for (n, c) in prefix.iter().enumerate().skip_while(|(_, &c)| c == '.') {
match c {
'#' => num += 1,
'.' => {
if cond.first() == Some(&num) {
return Some((&mut prefix[n..], &cond[1..]));
} else {
return None;
}
}
'?' => {
if let Some(c) = cond.first() {
if *c >= num {
return Some((prefix, cond));
} else {
return None;
}
} else {
return Some((prefix, cond));
}
}
_ => unreachable!(),
}
}
if let Some(c) = cond.first() {
if *c == num {
Some((prefix, cond))
} else {
None
}
} else {
Some((&mut [], &[]))
}
}
fn arrangements(
cache: &mut AHashMap<(String, Vec<i64>), i64>,
row: &mut [char],
cond: &[i64],
) -> i64 {
let key = (row.iter().collect::<String>(), cond.to_vec());
if let Some(v) = cache.get(&key) {
return *v;
}
let Some((rest, cond_rest)) = prefix_valid(row, cond) else {
return 0;
};
for i in 0..rest.len() {
if rest[i] == '?' {
rest[i] = '#';
let mut sum = arrangements(cache, rest, cond_rest);
rest[i] = '.';
sum += arrangements(cache, rest, cond_rest);
rest[i] = '?';
cache.insert(key, sum);
return sum;
}
}
let nums: Vec<i64> = row
.split(|c| *c == '.')
.filter(|w| !w.is_empty())
.map(|w| w.len() as i64)
.collect();
let res = if nums == cond { 1 } else { 0 };
cache.insert(key, res);
res
}
fn part1(puzzle: &[Springs]) -> i64 {
let mut cache = AHashMap::new();
puzzle
.iter()
.map(|springs| {
let mut row = springs.row.clone();
arrangements(&mut cache, &mut row, &springs.condition)
})
.sum()
}
fn part2(puzzle: &[Springs]) -> i64 {
puzzle
.par_iter()
.map(|p| {
let mut row =
(0..5).map(|_| &p.row[..]).collect::<Vec<_>>().join(&'?');
let cond: Vec<_> = (0..5).map(|_| &p.condition[..]).collect();
let cond = cond.concat();
let mut cache = AHashMap::new();
arrangements(&mut cache, &mut row, &cond)
})
.sum()
}
fn main() {
let input = include_str!("../../input/input12.txt");
let input = parse(input);
println!("part1 = {}", part1(&input));
println!("part2 = {}", part2(&input));
}
#[test]
fn test_day12() {
let input = "\
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1";
let input = parse(input);
assert_eq!(part1(&input), 21);
assert_eq!(part2(&input), 525152);
}