Skip to content

Commit f50f082

Browse files
committed
Multithread the whole thing
This doesn't really help a lot, except actually making use of the CPUs as a heater.
1 parent 09e982c commit f50f082

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/day12.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl ConditionRecord {
272272
}
273273
}
274274

275-
if true || print {
275+
if print {
276276
println!("{}/{}: |states after repeat| = {:?}, dropped = {}", r, repeat, states.len(), next_states.len() - states.len());
277277
}
278278
}
@@ -411,7 +411,7 @@ impl std::str::FromStr for ConditionRecord {
411411

412412
fn from_str(s: &str) -> Result<Self, Self::Err> {
413413
if let Some((p, g)) = s.split_once(' ') {
414-
println!("from_str: p='{}', g='{}'", p, g);
414+
// println!("from_str: p='{}', g='{}'", p, g);
415415
if let Ok(damaged_spring_groups) = g.split(',').map(usize::from_str).collect::<Result<Vec<usize>, _>>() {
416416
Ok(ConditionRecord {
417417
conditions: ConditionRecord::parse_state(p),
@@ -428,13 +428,47 @@ impl std::str::FromStr for ConditionRecord {
428428
}
429429

430430
fn num_arrangements(input: &str, repeat: usize) -> usize {
431-
input.split('\n').map(str::parse::<ConditionRecord>).map(|cr| cr.unwrap().repeat(repeat).num_arrangements()).sum()
431+
let thread_count_arc = std::sync::Arc::new((std::sync::Mutex::new(0u8), std::sync::Condvar::new()));
432+
433+
let mut v = vec![];
434+
for (id, line) in input.split('\n').enumerate() {
435+
if let Ok(cr) = line.parse::<ConditionRecord>() {
436+
let thread_count = thread_count_arc.clone();
437+
let jh = std::thread::spawn(move || {
438+
let (num, cvar) = &*thread_count;
439+
440+
let mut start = cvar
441+
.wait_while(num.lock().unwrap(), |start| *start >= 32)
442+
.unwrap();
443+
*start += 1;
444+
drop(start);
445+
446+
println!("thread {} for \"{}\" running", id, cr.conditions.iter().map(|c| *c as u8 as char).collect::<String>());
447+
let result = cr.repeat(repeat).num_arrangements();
448+
println!("thread {} for \"{}\" finished: {} arrangements", id, cr.conditions.iter().map(|c| *c as u8 as char).collect::<String>(), result);
449+
450+
start = num.lock().unwrap();
451+
*start -= 1;
452+
cvar.notify_one();
453+
454+
result
455+
});
456+
v.push(jh);
457+
}
458+
}
459+
460+
let expected = v.len();
461+
v.drain(..).enumerate().map(|(i, jh)| {
462+
let result = jh.join().unwrap();
463+
println!("joined {}, {} total", i, expected);
464+
result
465+
}).sum()
432466
}
433467

434468
pub fn main() {
435469
match std::fs::read_to_string("day12.input") {
436470
Ok(input) => {
437-
println!("num_arrangements = {}", num_arrangements(&input, 1));
471+
// println!("num_arrangements = {}", num_arrangements(&input, 1));
438472
println!("num_arrangements (part 2) = {}", num_arrangements(&input, 5));
439473
},
440474
Err(reason) => println!("error = {}", reason)

0 commit comments

Comments
 (0)