From ec8ec310635095cd15ec115587af8e277a0475e3 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Sun, 17 Dec 2023 15:30:02 +0100 Subject: [PATCH] multithread --- 2023/16/Cargo.toml | 9 ++++++++- 2023/16/src/main.rs | 47 ++++++++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/2023/16/Cargo.toml b/2023/16/Cargo.toml index 523f9da..bef978f 100644 --- a/2023/16/Cargo.toml +++ b/2023/16/Cargo.toml @@ -2,7 +2,14 @@ name = "sixteenth" version = "0.1.0" edition = "2021" - # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] + +[profile.release] +opt-level = 3 + +[target.x86_64-unknown-linux-gnu] +rustflags = [ + "-C", "target-feature=+crt-static" +] \ No newline at end of file diff --git a/2023/16/src/main.rs b/2023/16/src/main.rs index 67519cc..e1eb6df 100644 --- a/2023/16/src/main.rs +++ b/2023/16/src/main.rs @@ -1,4 +1,4 @@ -use std::{fs::File, io::{BufReader, BufRead, Write}, ops::Add, fmt::Debug, hash::Hasher}; +use std::{fs::File, io::{BufReader, BufRead, Write}, ops::Add, fmt::Debug, hash::Hasher, sync::Arc}; fn main() { part1(); @@ -294,21 +294,38 @@ fn part1() { fn part2() { let (max_point, rows, columns) = setup(); - let mut count = 0; + let mut threads = Vec::with_capacity(4); + let rows = Arc::new(rows); + let columns = Arc::new(columns); for vector in [VEC_LEFT, VEC_RIGHT, VEC_UP, VEC_DOWN] { - let is_vert = vector.is_vertical(); - let is_pos = vector.is_positive(); - let num2 = if !is_pos { max_point.x - 1 } else { 0 }; - for num in 0..max_point.x { - let beam = Beam { - point: if is_vert { Point { x: num, y: num2 } } else { Point { x: num2, y: num } }, - vector - }; - let count_here = get_count(max_point, &rows, &columns, beam); - if count_here > count { - count = count_here; - println!("New max: {} at {},{}", count, num, num2); - } + let rows = rows.clone(); + let columns = columns.clone(); + threads.push( + std::thread::spawn(move || { + let mut count = 0; + let is_vert = vector.is_vertical(); + let is_pos = vector.is_positive(); + let num2 = if !is_pos { max_point.x - 1 } else { 0 }; + for num in 0..max_point.x { + let beam = Beam { + point: if is_vert { Point { x: num, y: num2 } } else { Point { x: num2, y: num } }, + vector + }; + let count_here = get_count(max_point, &rows, &columns, beam); + if count_here > count { + count = count_here; + println!("New max: {} at {},{}", count, num, num2); + } + } + count + }) + ) + } + let mut count = 0; + for thread in threads { + let count_here = thread.join().unwrap(); + if count_here > count { + count = count_here; } } println!("Max count: {}", count);