Skip to content

Commit c097a12

Browse files
committed
Revert "Use new system every where"
This reverts commit 3d3c769.
1 parent 3d3c769 commit c097a12

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

src/day23.rs

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ const BAL: usize = (MAX * MAX).div_ceil(64);
7474
pub fn part2(s: &str) -> &'static str {
7575
let s = s.as_bytes();
7676

77+
let mut g = BitArray::<[u64; BAL]>::default();
78+
7779
let mut vertecies = const {
7880
let mut vs = [(0u16, 0); MAX];
7981
let mut i = 0;
@@ -99,6 +101,8 @@ pub fn part2(s: &str) -> &'static str {
99101
connections
100102
.get_unchecked_mut(cp2 as usize)
101103
.push_unchecked(cp1);
104+
g.set(cp2 as usize * MAX + cp1 as usize, true);
105+
g.set(cp1 as usize * MAX + cp2 as usize, true);
102106
vertecies.get_unchecked_mut(cp1 as usize).1 += 1;
103107
vertecies.get_unchecked_mut(cp2 as usize).1 += 1;
104108

@@ -127,7 +131,7 @@ pub fn part2(s: &str) -> &'static str {
127131
let mut q = heapless::Vec::<u16, MAX_C>::new();
128132
let mut q_max = heapless::Vec::<u16, MAX_C>::new();
129133

130-
expand_first(vertecies, &connections, &mut q, &mut q_max, &mut cs);
134+
expand_first(vertecies, &g, &connections, &mut q, &mut q_max, &mut cs);
131135

132136
q_max.sort_unstable();
133137

@@ -147,15 +151,13 @@ pub fn part2(s: &str) -> &'static str {
147151
// 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
148152
fn expand_first(
149153
mut r: &mut [(u16, u16)],
154+
g: &BitArray<[u64; BAL]>,
150155
cons: &[heapless::Vec<u16, MAX_C>; MAX],
151156
q: &mut heapless::Vec<u16, MAX_C>,
152157
q_max: &mut heapless::Vec<u16, MAX_C>,
153158
cs: &mut [heapless::Vec<u16, MAX_C>; MAX_C],
154159
) {
155-
let mut r_map = [false; MAX];
156-
for (p, _) in r.iter() {
157-
*unsafe { r_map.get_unchecked_mut(*p as usize) } = true;
158-
}
160+
let mut r_map = [true; MAX];
159161
while let Some(((p, color), rest)) = r.split_last_mut() {
160162
let p = *p as usize;
161163
if q.len() + *color as usize + 1 > q_max.len() {
@@ -169,8 +171,8 @@ fn expand_first(
169171
}
170172

171173
if !new_r.is_empty() {
172-
unsafe { number_sort(new_r.as_mut_slice(), cons, cs) };
173-
expand_first(&mut new_r, cons, q, q_max, cs);
174+
unsafe { number_sort(new_r.as_mut_slice(), g, cs) };
175+
expand(&mut new_r, g, q, q_max, cs);
174176
} else if q.len() > q_max.len() {
175177
q_max.clone_from(q);
176178
}
@@ -183,44 +185,44 @@ fn expand_first(
183185
}
184186
}
185187

186-
// // Using this algorithm: https://web.archive.org/web/20160911054636/http://www.dcs.gla.ac.uk/~pat/jchoco/clique/indSetMachrahanish/papers/tomita2003.pdf
187-
// fn expand(
188-
// mut r: &mut [(u16, u16)],
189-
// g: &BitArray<[u64; BAL]>,
190-
// q: &mut heapless::Vec<u16, MAX_C>,
191-
// q_max: &mut heapless::Vec<u16, MAX_C>,
192-
// cs: &mut [heapless::Vec<u16, MAX_C>; MAX_C],
193-
// ) {
194-
// while let Some(((p, color), rest)) = r.split_last_mut() {
195-
// let p = *p as usize;
196-
// if q.len() + *color as usize + 1 > q_max.len() {
197-
// q.push(p as u16).unwrap();
198-
199-
// let mut new_r = heapless::Vec::<(u16, u16), MAX_C>::new();
200-
// for (i, _) in rest.iter() {
201-
// if unsafe { *g.get_unchecked(*i as usize * MAX + p) } {
202-
// new_r.push((*i, 0)).unwrap();
203-
// }
204-
// }
205-
206-
// if !new_r.is_empty() {
207-
// unsafe { number_sort(new_r.as_mut_slice(), g, cs) };
208-
// expand(&mut new_r, g, q, q_max, cs);
209-
// } else if q.len() > q_max.len() {
210-
// q_max.clone_from(q);
211-
// }
212-
// q.pop();
213-
// } else {
214-
// return;
215-
// }
216-
// r = rest;
217-
// }
218-
// }
188+
// Using this algorithm: https://web.archive.org/web/20160911054636/http://www.dcs.gla.ac.uk/~pat/jchoco/clique/indSetMachrahanish/papers/tomita2003.pdf
189+
fn expand(
190+
mut r: &mut [(u16, u16)],
191+
g: &BitArray<[u64; BAL]>,
192+
q: &mut heapless::Vec<u16, MAX_C>,
193+
q_max: &mut heapless::Vec<u16, MAX_C>,
194+
cs: &mut [heapless::Vec<u16, MAX_C>; MAX_C],
195+
) {
196+
while let Some(((p, color), rest)) = r.split_last_mut() {
197+
let p = *p as usize;
198+
if q.len() + *color as usize + 1 > q_max.len() {
199+
q.push(p as u16).unwrap();
200+
201+
let mut new_r = heapless::Vec::<(u16, u16), MAX_C>::new();
202+
for (i, _) in rest.iter() {
203+
if unsafe { *g.get_unchecked(*i as usize * MAX + p) } {
204+
new_r.push((*i, 0)).unwrap();
205+
}
206+
}
207+
208+
if !new_r.is_empty() {
209+
unsafe { number_sort(new_r.as_mut_slice(), g, cs) };
210+
expand(&mut new_r, g, q, q_max, cs);
211+
} else if q.len() > q_max.len() {
212+
q_max.clone_from(q);
213+
}
214+
q.pop();
215+
} else {
216+
return;
217+
}
218+
r = rest;
219+
}
220+
}
219221

220222
#[inline(always)]
221223
unsafe fn number_sort(
222224
r: &mut [(u16, u16)],
223-
cons: &[heapless::Vec<u16, MAX_C>; MAX],
225+
g: &BitArray<[u64; BAL]>,
224226
cs: &mut [heapless::Vec<u16, MAX_C>; MAX_C],
225227
) {
226228
let mut maxno = 0;
@@ -235,7 +237,7 @@ unsafe fn number_sort(
235237

236238
'outer: loop {
237239
for i in cs.get_unchecked(k) {
238-
if cons.get_unchecked(*i as usize).contains(&(p as u16)) {
240+
if *g.get_unchecked(*i as usize * MAX + p) {
239241
k += 1;
240242
continue 'outer;
241243
}

0 commit comments

Comments
 (0)