day(12): move the mutability into the function
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
dc2df6d7fb
commit
20de5928c2
1 changed files with 20 additions and 25 deletions
|
@ -30,11 +30,7 @@ impl FromStr for Row {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[memoize]
|
#[memoize]
|
||||||
fn arrangements(
|
fn arrangements(map: VecDeque<char>, damaged: VecDeque<usize>, in_damaged: bool) -> usize {
|
||||||
mut map: VecDeque<char>,
|
|
||||||
mut damaged: VecDeque<usize>,
|
|
||||||
mut in_damaged: bool,
|
|
||||||
) -> usize {
|
|
||||||
// all damaged have been placed
|
// all damaged have been placed
|
||||||
if damaged.is_empty() {
|
if damaged.is_empty() {
|
||||||
return if map.iter().all(|&c| c == '.' || c == '?') {
|
return if map.iter().all(|&c| c == '.' || c == '?') {
|
||||||
|
@ -44,6 +40,10 @@ fn arrangements(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut map = map.clone();
|
||||||
|
let mut damaged = damaged.clone();
|
||||||
|
let mut in_damaged = in_damaged;
|
||||||
|
|
||||||
// skip the functional
|
// skip the functional
|
||||||
while let Some('.') = map.front() {
|
while let Some('.') = map.front() {
|
||||||
if in_damaged {
|
if in_damaged {
|
||||||
|
@ -60,43 +60,38 @@ fn arrangements(
|
||||||
|
|
||||||
match map[0] {
|
match map[0] {
|
||||||
'?' => {
|
'?' => {
|
||||||
map[0] = '#';
|
|
||||||
let as_non_functional = arrangements(map.clone(), damaged.clone(), true);
|
|
||||||
|
|
||||||
map[0] = '.';
|
map[0] = '.';
|
||||||
let as_functional = if !in_damaged {
|
let as_functional = if !in_damaged {
|
||||||
arrangements(map, damaged, false)
|
arrangements(map.clone(), damaged.clone(), false)
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
map[0] = '#';
|
||||||
|
let as_non_functional = arrangements(map, damaged, true);
|
||||||
|
|
||||||
as_functional + as_non_functional
|
as_functional + as_non_functional
|
||||||
}
|
}
|
||||||
'#' if damaged.is_empty() => 0,
|
'#' if damaged.is_empty() => 0,
|
||||||
'#' => {
|
'#' => {
|
||||||
damaged[0] -= 1;
|
damaged[0] -= 1;
|
||||||
|
|
||||||
|
map.pop_front();
|
||||||
|
|
||||||
if damaged[0] == 0 {
|
if damaged[0] == 0 {
|
||||||
if 1 < map.len() && map[1] == '#' {
|
damaged.pop_front();
|
||||||
|
|
||||||
|
if let Some(&'#') = map.front() {
|
||||||
0
|
0
|
||||||
} else if 1 < map.len() {
|
} else if !map.is_empty() {
|
||||||
map[1] = '.';
|
map[0] = '.';
|
||||||
arrangements(
|
arrangements(map, damaged, false)
|
||||||
map.iter().cloned().skip(1).collect(),
|
|
||||||
damaged.iter().cloned().skip(1).collect(),
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
let result = arrangements(
|
map.pop_front();
|
||||||
map.iter().cloned().skip(2).collect(),
|
arrangements(map, damaged, false)
|
||||||
damaged.iter().cloned().skip(1).collect(),
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let result = arrangements(map.iter().cloned().skip(1).collect(), damaged, true);
|
arrangements(map, damaged, true)
|
||||||
result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
|
|
Loading…
Reference in a new issue