Skip to content

Commit 3d3c769

Browse files
committed
Use new system every where
1 parent 867a13a commit 3d3c769

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

src/day23.rs

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ 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-
7977
let mut vertecies = const {
8078
let mut vs = [(0u16, 0); MAX];
8179
let mut i = 0;
@@ -101,8 +99,6 @@ pub fn part2(s: &str) -> &'static str {
10199
connections
102100
.get_unchecked_mut(cp2 as usize)
103101
.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);
106102
vertecies.get_unchecked_mut(cp1 as usize).1 += 1;
107103
vertecies.get_unchecked_mut(cp2 as usize).1 += 1;
108104

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

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

136132
q_max.sort_unstable();
137133

@@ -151,13 +147,15 @@ pub fn part2(s: &str) -> &'static str {
151147
// 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
152148
fn expand_first(
153149
mut r: &mut [(u16, u16)],
154-
g: &BitArray<[u64; BAL]>,
155150
cons: &[heapless::Vec<u16, MAX_C>; MAX],
156151
q: &mut heapless::Vec<u16, MAX_C>,
157152
q_max: &mut heapless::Vec<u16, MAX_C>,
158153
cs: &mut [heapless::Vec<u16, MAX_C>; MAX_C],
159154
) {
160-
let mut r_map = [true; MAX];
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+
}
161159
while let Some(((p, color), rest)) = r.split_last_mut() {
162160
let p = *p as usize;
163161
if q.len() + *color as usize + 1 > q_max.len() {
@@ -171,8 +169,8 @@ fn expand_first(
171169
}
172170

173171
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);
172+
unsafe { number_sort(new_r.as_mut_slice(), cons, cs) };
173+
expand_first(&mut new_r, cons, q, q_max, cs);
176174
} else if q.len() > q_max.len() {
177175
q_max.clone_from(q);
178176
}
@@ -185,44 +183,44 @@ fn expand_first(
185183
}
186184
}
187185

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-
}
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+
// }
221219

222220
#[inline(always)]
223221
unsafe fn number_sort(
224222
r: &mut [(u16, u16)],
225-
g: &BitArray<[u64; BAL]>,
223+
cons: &[heapless::Vec<u16, MAX_C>; MAX],
226224
cs: &mut [heapless::Vec<u16, MAX_C>; MAX_C],
227225
) {
228226
let mut maxno = 0;
@@ -237,7 +235,7 @@ unsafe fn number_sort(
237235

238236
'outer: loop {
239237
for i in cs.get_unchecked(k) {
240-
if *g.get_unchecked(*i as usize * MAX + p) {
238+
if cons.get_unchecked(*i as usize).contains(&(p as u16)) {
241239
k += 1;
242240
continue 'outer;
243241
}

0 commit comments

Comments
 (0)