Skip to content

Commit 9abdb0a

Browse files
committed
speed up puzzle 1 for Day 12
1 parent d141692 commit 9abdb0a

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

src/solutions/day12.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::data::load;
22
use itertools::Itertools;
3-
use std::{fmt::Display, num::ParseIntError};
3+
use std::{fmt::Display, iter::zip, num::ParseIntError};
44
use thiserror::Error;
55

66
#[derive(Error, Debug, PartialEq, Eq)]
@@ -85,24 +85,29 @@ impl Display for Row {
8585
}
8686
}
8787

88+
fn _split_condition_vec(conditions: &[Condition]) -> Vec<&[Condition]> {
89+
conditions
90+
.split(|x| x == &Condition::Operational)
91+
.filter(|cs| !cs.is_empty())
92+
.collect::<Vec<_>>()
93+
}
94+
8895
impl Row {
8996
fn _final_validation(&self, prop_conds: &[Condition]) -> bool {
90-
let s = display_vec(prop_conds);
91-
let splits = s.split('.').filter(|s| !s.is_empty()).collect::<Vec<_>>();
97+
let splits = _split_condition_vec(prop_conds);
9298
if self.groups.len() != splits.len() {
9399
return false;
94100
}
95-
for (i, seq) in splits.iter().enumerate() {
96-
if seq.len() != self.groups[i] {
101+
for (seq, gr) in zip(splits.iter(), self.groups.iter()) {
102+
if seq.len() != *gr {
97103
return false;
98104
}
99105
}
100106
true
101107
}
102108

103109
fn _in_progress_validation(&self, prop_conds: &[Condition]) -> bool {
104-
let s = display_vec(prop_conds);
105-
let splits = s.split('.').filter(|s| !s.is_empty()).collect::<Vec<_>>();
110+
let splits = _split_condition_vec(prop_conds);
106111
if self.groups.len() < splits.len() {
107112
return false;
108113
}
@@ -152,9 +157,6 @@ impl Row {
152157
}
153158
prop_conds = self.prune(&prop_conds);
154159
}
155-
for cond in prop_conds.iter() {
156-
log::debug!(" {}", display_vec(cond));
157-
}
158160
prop_conds.len()
159161
}
160162
}
@@ -172,6 +174,33 @@ pub fn puzzle_1(input: &str) -> Result<usize, PuzzleErr> {
172174
Ok(rows.into_iter().map(|r| r.num_solutions()).sum())
173175
}
174176

177+
pub fn puzzle_2(input: &str) -> Result<usize, PuzzleErr> {
178+
let rows = parse_input(input)?
179+
.iter()
180+
.map(|r| {
181+
let mut c = (0..5)
182+
.map(|_| {
183+
let mut new_c = r.conditions.clone();
184+
new_c.push(Condition::Unknown);
185+
new_c
186+
})
187+
.concat();
188+
let _ = c.pop();
189+
let g = (0..5).map(|_| r.groups.clone()).concat();
190+
Row {
191+
conditions: c,
192+
groups: g,
193+
}
194+
})
195+
.collect::<Vec<Row>>();
196+
197+
Ok(rows.into_iter().map(|r| r.num_solutions()).sum())
198+
// for row in rows.iter() {
199+
// log::debug!("{}", row);
200+
// }
201+
// Ok(0)
202+
}
203+
175204
pub fn main(data_dir: &str) {
176205
println!("Day 12: Hot Springs");
177206
let data = load(data_dir, 12, None);
@@ -185,10 +214,10 @@ pub fn main(data_dir: &str) {
185214
assert_eq!(answer_1, Ok(7716));
186215

187216
// Puzzle 2.
188-
// let answer_2 = puzzle_2(&data);
189-
// match answer_2 {
190-
// Ok(x) => println!(" Puzzle 2: {}", x),
191-
// Err(e) => panic!("No solution to puzzle 2: {}", e),
192-
// }
217+
let answer_2 = puzzle_2(&data);
218+
match answer_2 {
219+
Ok(x) => println!(" Puzzle 2: {}", x),
220+
Err(e) => panic!("No solution to puzzle 2: {}", e),
221+
}
193222
// assert_eq!(answer_2, Ok(933))
194223
}

tests/test_day12.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use aoc_2023::solutions::day12::puzzle_1;
1+
use aoc_2023::solutions::day12::{puzzle_1, puzzle_2};
22

33
const EXAMPLE_INPUT_1: &str = "
44
???.### 1,1,3
@@ -14,3 +14,9 @@ fn puzzle_1_example_1() {
1414
let _ = env_logger::try_init();
1515
assert_eq!(puzzle_1(self::EXAMPLE_INPUT_1), Ok(21));
1616
}
17+
18+
#[test]
19+
fn puzzle_2_example_1() {
20+
let _ = env_logger::try_init();
21+
assert_eq!(puzzle_2(self::EXAMPLE_INPUT_1), Ok(525152));
22+
}

0 commit comments

Comments
 (0)