Skip to content

Commit b82b7aa

Browse files
committed
chore: WIP y2025::day_12
1 parent 73fd732 commit b82b7aa

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

aoclp_solutions/src/y2025/day_12.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ where
3131
fn from(value: I) -> Self {
3232
let shape = value
3333
.map(|line| {
34-
line.as_ref()
35-
.chars()
36-
.map(|c| c == '#')
37-
.collect_array()
38-
.unwrap()
34+
let line = line.as_ref();
35+
line.bytes().map(|c| c == b'#').collect_array().unwrap()
3936
})
4037
.collect_array()
4138
.unwrap();
@@ -83,46 +80,37 @@ fn parse_input<I, S>(input: I) -> (Vec<Present>, Vec<Region>)
8380
where
8481
I: IntoIterator<Item = S>,
8582
<I as IntoIterator>::IntoIter: Clone,
86-
S: AsRef<str> + Clone,
83+
S: AsRef<str>,
8784
{
8885
static INDEX_REGEX: OnceLock<Regex> = OnceLock::new();
8986
let index_re = INDEX_REGEX.get_or_init(|| Regex::new(r"^(?<idx>\d+):\s*$").unwrap());
9087

91-
let mut it = input.into_iter().peekable();
88+
let it = input.into_iter();
89+
90+
let mut present_it = it
91+
.clone()
92+
.take_while(|line| line.as_ref().parse::<Region>().is_err());
9293
let mut presents = Vec::new();
9394
let mut i = 0;
9495
loop {
95-
// if let Some(line) = it.peek() && line.as_ref().trim_ascii().is_empty() {
96-
//
97-
// }
98-
99-
let index_s = it.next().expect("end of data before regions!");
100-
let index_s = index_s.as_ref();
101-
if index_s.trim_ascii().is_empty() {
102-
continue;
103-
}
104-
105-
match index_re.captures(index_s) {
96+
match present_it.next() {
10697
None => break,
107-
Some(index_cap) => {
98+
Some(s) if s.as_ref().trim_ascii().is_empty() => continue,
99+
Some(s) => {
100+
let index_cap = index_re.captures(s.as_ref()).unwrap();
108101
let index: usize = index_cap.ez_get("idx");
109102
if index != i {
110103
panic!("expected present #{i}, found present #{index}");
111104
}
112105

113-
let present: Present = it.clone().take(3).into();
106+
let present: Present = present_it.by_ref().take(3).into();
114107
presents.push(present);
115-
116-
it = it.dropping(3);
117108
i += 1;
118109
},
119110
}
120111
}
121112

122-
let regions = it
123-
.skip_while(|l| l.as_ref().trim_ascii().is_empty())
124-
.map(|l| l.as_ref().parse().unwrap())
125-
.collect();
113+
let regions = it.filter_map(|l| l.as_ref().parse().ok()).collect();
126114

127115
(presents, regions)
128116
}

0 commit comments

Comments
 (0)