Skip to content

Commit

Permalink
Optimise 06 (~6.5s -> ~1.6s)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekuskus committed Dec 6, 2024
1 parent 6d47740 commit 6e7d9a7
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ fn part2(lines: &Vec<String>) -> usize {
.iter()
.map(|s| s.chars().collect())
.collect::<Vec<Vec<char>>>();

let mut startx = None;
let mut starty = None;

'search: for (y, line) in matrix.iter().enumerate() {
for (x, c) in line.iter().enumerate() {
if *c == '^' {
Expand All @@ -208,14 +210,36 @@ fn part2(lines: &Vec<String>) -> usize {

matrix[p.y][p.x] = 'X';

iter::repeat_n(&matrix, matrix.len() * matrix[0].len())
let mut check_matrix = matrix.clone();
let (mut loop_p, mut loop_d) = (p.clone(), d.clone());
// first pass to find viable points
while let Some((newp, newd)) = matrix_step(&check_matrix, &loop_p, loop_d, None) {
loop_p = newp;
loop_d = newd;
check_matrix[loop_p.y][loop_p.x] = 'X';
}

let visited = check_matrix
.iter()
.enumerate()
.filter(|(idx, matrix)| {
let ignore = Point {
x: idx % matrix[0].len(),
y: idx / matrix.len(),
};
.map(|(y, line)| {
line.iter()
.enumerate()
.filter_map(|(x, &c)| {
if c == 'X' {
Some(Point::new(x, y))
} else {
None
}
})
.collect::<Vec<Point>>()
})
.flatten()
.collect::<HashSet<Point>>();

iter::repeat_n(&matrix, visited.len())
.zip(visited.iter())
.filter(|(matrix, &ignore)| {
if matrix[ignore.y][ignore.x] == 'X' {
// don't replace starting position
return false;
Expand Down

0 comments on commit 6e7d9a7

Please sign in to comment.