Skip to content

Commit 6a3500c

Browse files
author
mflinn-broad
committed
day15
1 parent 99fcfce commit 6a3500c

File tree

1 file changed

+62
-32
lines changed

1 file changed

+62
-32
lines changed

src/days/day15.rs

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,62 @@ fn part_1(input: Vec<Vec<i32>>) -> i32 {
1616

1717
dijkstra::dijkstra(
1818
&start,
19-
|(x, y)| {
20-
SURROUNDING_POINTS
21-
.iter()
22-
.map(|(x_move, y_move)| {
23-
input
24-
.get((x + x_move) as usize)
25-
.and_then(|row| row.get((y + y_move) as usize))
26-
.map(|risk| ((x + x_move, y + y_move), *risk))
27-
})
28-
.flatten()
29-
.collect::<Vec<_>>()
30-
},
19+
|(x, y)| successors_small((x, y), &input),
3120
|&node| node == goal,
3221
)
3322
.unwrap()
3423
.1
3524
}
3625

26+
fn successors_small(current: (&i32, &i32), grid: &[Vec<i32>]) -> Vec<((i32, i32), i32)> {
27+
let (x, y) = current;
28+
SURROUNDING_POINTS
29+
.iter()
30+
.map(|(x_move, y_move)| {
31+
grid.get((x + x_move) as usize)
32+
.and_then(|row| row.get((y + y_move) as usize))
33+
.map(|risk| ((x + x_move, y + y_move), *risk))
34+
})
35+
.flatten()
36+
.collect::<Vec<_>>()
37+
}
38+
3739
fn part_2(input: Vec<Vec<i32>>) -> i32 {
3840
let square_size = input.len();
3941
let start = (0, 0);
4042
let goal = (square_size as i32 * 5 - 1, square_size as i32 * 5 - 1);
4143

4244
dijkstra::dijkstra(
4345
&start,
44-
|(x, y)| {
45-
SURROUNDING_POINTS
46-
.iter()
47-
.map(|(x_move, y_move)| ((x + x_move) as usize, (y + y_move) as usize))
48-
.filter(|(x, y)| (x / 5 < square_size && y / 5 < square_size))
49-
.map(|(x, y)| {
50-
input
51-
.get(x % square_size)
52-
.and_then(|row| row.get(y % square_size))
53-
.map(|risk| {
54-
(
55-
(x as i32, y as i32),
56-
((*risk as usize + (x / square_size) + (y / square_size) - 1) % 9
57-
+ 1) as i32,
58-
)
59-
})
60-
})
61-
.flatten()
62-
.collect::<Vec<_>>()
63-
},
46+
|(x, y)| successors_large((x, y), &input),
6447
|&node| node == goal,
6548
)
6649
.unwrap()
6750
.1
6851
}
6952

53+
fn successors_large(current: (&i32, &i32), grid: &[Vec<i32>]) -> Vec<((i32, i32), i32)> {
54+
let (x, y) = current;
55+
let square_size = grid.len();
56+
SURROUNDING_POINTS
57+
.iter()
58+
.map(|(x_move, y_move)| ((x + x_move) as usize, (y + y_move) as usize))
59+
.filter(|(x, y)| (x / 5 < square_size && y / 5 < square_size))
60+
.map(|(x, y)| {
61+
grid.get(x % square_size)
62+
.and_then(|row| row.get(y % square_size))
63+
.map(|risk| {
64+
(
65+
(x as i32, y as i32),
66+
((*risk as usize + (x / square_size) + (y / square_size) - 1) % 9 + 1)
67+
as i32,
68+
)
69+
})
70+
})
71+
.flatten()
72+
.collect::<Vec<_>>()
73+
}
74+
7075
fn process(input: &str) -> Vec<Vec<i32>> {
7176
input
7277
.lines()
@@ -78,3 +83,28 @@ fn process(input: &str) -> Vec<Vec<i32>> {
7883
})
7984
.collect()
8085
}
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use super::*;
90+
extern crate test;
91+
use test::Bencher;
92+
93+
#[bench]
94+
fn bench_part_1(b: &mut Bencher) {
95+
let raw_input = util::read_input("inputs/day15.txt").unwrap();
96+
b.iter(|| {
97+
let input = process(&raw_input);
98+
part_1(input);
99+
});
100+
}
101+
102+
#[bench]
103+
fn bench_part_2(b: &mut Bencher) {
104+
let raw_input = util::read_input("inputs/day15.txt").unwrap();
105+
b.iter(|| {
106+
let input = process(&raw_input);
107+
part_2(input);
108+
});
109+
}
110+
}

0 commit comments

Comments
 (0)