mirror of
https://github.com/danbulant/adventOfCode
synced 2026-05-24 12:30:16 +00:00
multithread
This commit is contained in:
parent
825238d105
commit
ec8ec31063
2 changed files with 40 additions and 16 deletions
|
|
@ -2,7 +2,14 @@
|
||||||
name = "sixteenth"
|
name = "sixteenth"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
opt-level = 3
|
||||||
|
|
||||||
|
[target.x86_64-unknown-linux-gnu]
|
||||||
|
rustflags = [
|
||||||
|
"-C", "target-feature=+crt-static"
|
||||||
|
]
|
||||||
|
|
@ -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() {
|
fn main() {
|
||||||
part1();
|
part1();
|
||||||
|
|
@ -294,8 +294,15 @@ fn part1() {
|
||||||
|
|
||||||
fn part2() {
|
fn part2() {
|
||||||
let (max_point, rows, columns) = setup();
|
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] {
|
for vector in [VEC_LEFT, VEC_RIGHT, VEC_UP, VEC_DOWN] {
|
||||||
|
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_vert = vector.is_vertical();
|
||||||
let is_pos = vector.is_positive();
|
let is_pos = vector.is_positive();
|
||||||
let num2 = if !is_pos { max_point.x - 1 } else { 0 };
|
let num2 = if !is_pos { max_point.x - 1 } else { 0 };
|
||||||
|
|
@ -310,6 +317,16 @@ fn part2() {
|
||||||
println!("New max: {} at {},{}", count, num, num2);
|
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);
|
println!("Max count: {}", count);
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue