Skip to content

Commit

Permalink
fun (unnecessary) parralel optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed Dec 8, 2024
1 parent e7d4a5f commit 88593b3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 35 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ chrono = { version = "0.4.38", optional = true }
dhat = { version = "0.3.3", optional = true }
itertools = "0.13.0"
pico-args = "0.5.0"
rayon = "1.10.0"
regex = "1.11.1"
tinyjson = "2.5.1"

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 3](./src/bin/03.rs) | `115.1µs` | `148.7µs` |
| [Day 4](./src/bin/04.rs) | `81.9µs` | `51.3µs` |
| [Day 5](./src/bin/05.rs) | `379.0µs` | `816.9µs` |
| [Day 6](./src/bin/06.rs) | `331.5µs` | `311.0ms` |
| [Day 7](./src/bin/07.rs) | `900.4µs` | `2.8ms` |
| [Day 6](./src/bin/06.rs) | `358.0µs` | `31.5ms` |
| [Day 7](./src/bin/07.rs) | `487.3µs` | `862.0µs` |
| [Day 8](./src/bin/08.rs) | `36.5µs` | `116.8µs` |

**Total: 317.20ms**
**Total: 35.37ms**
<!--- benchmarking table --->
2 changes: 1 addition & 1 deletion src/bin/05.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
cmp::Ordering,
collections::{HashMap, HashSet, VecDeque},
collections::{HashMap, HashSet},
};

use itertools::Itertools;
Expand Down
21 changes: 13 additions & 8 deletions src/bin/06.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashSet;

use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

advent_of_code::solution!(6);

#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
Expand Down Expand Up @@ -196,12 +198,15 @@ pub fn part_two(input: &str) -> Option<u32> {
};
}

let mut res = 0;
for block in potential_blocker_positions {
if check_for_loop(&rocks, x_max, y_max, block, start_guard) {
res += 1
};
}
let res = potential_blocker_positions
.par_iter()
.fold_with(0, |acc, block| {
if check_for_loop(&rocks, x_max, y_max, block, start_guard) {
return acc + 1;
};
acc
})
.sum();

Some(res)
}
Expand Down Expand Up @@ -236,12 +241,12 @@ fn check_for_loop(
rocks: &HashSet<Pos>,
x_max: i32,
y_max: i32,
new_block: Pos,
new_block: &Pos,
start_guard: Guard,
) -> bool {
let mut guard = start_guard;
let mut new_rocks = rocks.clone();
new_rocks.insert(new_block);
new_rocks.insert(new_block.to_owned());

let mut visited = HashSet::new();
loop {
Expand Down
54 changes: 31 additions & 23 deletions src/bin/07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashSet;

use itertools::Itertools;

use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

advent_of_code::solution!(7);

#[derive(Debug, Eq, Hash, PartialEq, Clone)]
Expand All @@ -26,13 +28,16 @@ pub fn part_one(input: &str) -> Option<i64> {
})
.collect_vec();

let result: i64 = equations.into_iter().fold(0, |acc, eq| {
if solve_equation(&eq).is_some() {
acc + eq.target
} else {
acc
}
});
let result: i64 = equations
.par_iter()
.fold_with(0, |acc, eq| {
if solve_equation(eq).is_some() {
acc + eq.target
} else {
acc
}
})
.sum();

Some(result)
}
Expand All @@ -41,11 +46,11 @@ fn solve_equation(equation: &Equation) -> Option<HashSet<Vec<char>>> {
let mut solutions = HashSet::new();
if equation.numbers.len() == 2 {
if equation.numbers[0] + equation.numbers[1] == equation.target {
solutions.insert(vec!['+'].into());
solutions.insert(vec!['+']);
};

if equation.numbers[0] * equation.numbers[1] == equation.target {
solutions.insert(vec!['*'].into());
solutions.insert(vec!['*']);
};

return if solutions.is_empty() {
Expand Down Expand Up @@ -113,33 +118,36 @@ pub fn part_two(input: &str) -> Option<i64> {
})
.collect_vec();

let result: i64 = equations.into_iter().fold(0, |acc, eq| {
if solve_equation_2(&eq).is_some() {
acc + eq.target
} else {
acc
}
});
let result: i64 = equations
.par_iter()
.fold_with(0, |acc, eq| {
if solve_equation_2(eq).is_some() {
acc + eq.target
} else {
acc
}
})
.sum();

Some(result)
}

fn solve_equation_2(equation: &Equation) -> Option<HashSet<VecDeque<char>>> {
fn solve_equation_2(equation: &Equation) -> Option<HashSet<Vec<char>>> {
let mut solutions = HashSet::new();
if equation.numbers.len() == 2 {
if equation.numbers[0] + equation.numbers[1] == equation.target {
solutions.insert(vec!['+'].into());
solutions.insert(vec!['+']);
};

if equation.numbers[0] * equation.numbers[1] == equation.target {
solutions.insert(vec!['*'].into());
solutions.insert(vec!['*']);
};

let concat = format!("{}{}", equation.numbers[0], equation.numbers[1])
.parse::<i64>()
.unwrap();
if concat == equation.target {
solutions.insert(vec!['|'].into());
solutions.insert(vec!['|']);
};

return if solutions.is_empty() {
Expand All @@ -162,7 +170,7 @@ fn solve_equation_2(equation: &Equation) -> Option<HashSet<VecDeque<char>>> {
if let Some(sols) = add_smaller_solutions {
for sol in sols {
let mut new_sol = sol.clone();
new_sol.push_back('+');
new_sol.push('+');
solutions.insert(new_sol);
}
}
Expand All @@ -178,7 +186,7 @@ fn solve_equation_2(equation: &Equation) -> Option<HashSet<VecDeque<char>>> {
if let Some(sols) = mult_smaller_solutions {
for sol in sols {
let mut new_sol = sol.clone();
new_sol.push_back('*');
new_sol.push('*');
solutions.insert(new_sol);
}
};
Expand All @@ -196,7 +204,7 @@ fn solve_equation_2(equation: &Equation) -> Option<HashSet<VecDeque<char>>> {
if let Some(sols) = concat_smaller_solutions {
for sol in sols {
let mut new_sol = sol.clone();
new_sol.push_back('|');
new_sol.push('|');
solutions.insert(new_sol);
}
};
Expand Down

0 comments on commit 88593b3

Please sign in to comment.