Skip to content

Commit 867a13a

Browse files
committed
Modify first expand
1 parent 9f7c453 commit 867a13a

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/day23.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub fn part2(s: &str) -> &'static str {
8585
}
8686
vs
8787
};
88+
let mut connections = [const { heapless::Vec::<u16, MAX_C>::new() }; MAX];
8889

8990
unsafe {
9091
let mut i = 0;
@@ -94,6 +95,12 @@ pub fn part2(s: &str) -> &'static str {
9495
let cp2 = (s.get_unchecked(i + 3) - b'a') as u16 * 26
9596
+ (s.get_unchecked(i + 4) - b'a') as u16;
9697

98+
connections
99+
.get_unchecked_mut(cp1 as usize)
100+
.push_unchecked(cp2);
101+
connections
102+
.get_unchecked_mut(cp2 as usize)
103+
.push_unchecked(cp1);
97104
g.set(cp2 as usize * MAX + cp1 as usize, true);
98105
g.set(cp1 as usize * MAX + cp2 as usize, true);
99106
vertecies.get_unchecked_mut(cp1 as usize).1 += 1;
@@ -123,12 +130,8 @@ pub fn part2(s: &str) -> &'static str {
123130

124131
let mut q = heapless::Vec::<u16, MAX_C>::new();
125132
let mut q_max = heapless::Vec::<u16, MAX_C>::new();
126-
#[cfg(not(test))]
127-
{
128-
q_max.resize(12, 0).unwrap();
129-
}
130133

131-
expand(vertecies, &g, &mut q, &mut q_max, &mut cs);
134+
expand_first(vertecies, &g, &connections, &mut q, &mut q_max, &mut cs);
132135

133136
q_max.sort_unstable();
134137

@@ -145,6 +148,43 @@ pub fn part2(s: &str) -> &'static str {
145148
}
146149
}
147150

151+
// Using a modified version of this algorithm: https://web.archive.org/web/20160911054636/http://www.dcs.gla.ac.uk/~pat/jchoco/clique/indSetMachrahanish/papers/tomita2003.pdf
152+
fn expand_first(
153+
mut r: &mut [(u16, u16)],
154+
g: &BitArray<[u64; BAL]>,
155+
cons: &[heapless::Vec<u16, MAX_C>; MAX],
156+
q: &mut heapless::Vec<u16, MAX_C>,
157+
q_max: &mut heapless::Vec<u16, MAX_C>,
158+
cs: &mut [heapless::Vec<u16, MAX_C>; MAX_C],
159+
) {
160+
let mut r_map = [true; MAX];
161+
while let Some(((p, color), rest)) = r.split_last_mut() {
162+
let p = *p as usize;
163+
if q.len() + *color as usize + 1 > q_max.len() {
164+
q.push(p as u16).unwrap();
165+
166+
let mut new_r = heapless::Vec::<(u16, u16), MAX_C>::new();
167+
for i in cons[p].iter() {
168+
if unsafe { *r_map.get_unchecked(*i as usize) } {
169+
new_r.push((*i, 0)).unwrap();
170+
}
171+
}
172+
173+
if !new_r.is_empty() {
174+
unsafe { number_sort(new_r.as_mut_slice(), g, cs) };
175+
expand(&mut new_r, g, q, q_max, cs);
176+
} else if q.len() > q_max.len() {
177+
q_max.clone_from(q);
178+
}
179+
q.pop();
180+
} else {
181+
return;
182+
}
183+
*unsafe { r_map.get_unchecked_mut(p) } = false;
184+
r = rest;
185+
}
186+
}
187+
148188
// Using this algorithm: https://web.archive.org/web/20160911054636/http://www.dcs.gla.ac.uk/~pat/jchoco/clique/indSetMachrahanish/papers/tomita2003.pdf
149189
fn expand(
150190
mut r: &mut [(u16, u16)],

0 commit comments

Comments
 (0)