Skip to content

Commit b7570b3

Browse files
committed
Check matching range remnants against other ranges
1 parent a5143fe commit b7570b3

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pie
103103
| 2 | [Cube Conundrum](https://adventofcode.com/2023/day/2) | [Source](src/year2023/day02.rs) | 10 |
104104
| 3 | [Gear Ratios](https://adventofcode.com/2023/day/3) | [Source](src/year2023/day03.rs) | 55 |
105105
| 4 | [Scratchcards](https://adventofcode.com/2023/day/4) | [Source](src/year2023/day04.rs) | 18 |
106-
| 5 | [If You Give A Seed A Fertilizer](https://adventofcode.com/2023/day/5) | [Source](src/year2023/day05.rs) | 13 |
106+
| 5 | [If You Give A Seed A Fertilizer](https://adventofcode.com/2023/day/5) | [Source](src/year2023/day05.rs) | 19 |
107107
| 6 | [Wait For It](https://adventofcode.com/2023/day/6) | [Source](src/year2023/day06.rs) | 1 |
108108
| 7 | [Camel Cards](https://adventofcode.com/2023/day/7) | [Source](src/year2023/day07.rs) | 97 |
109109
| 8 | [Haunted Wasteland](https://adventofcode.com/2023/day/8) | [Source](src/year2023/day08.rs) | 34 |

src/year2023/day05.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,43 @@ pub fn part1(input: &Input) -> u64 {
4747
pub fn part2(input: &Input) -> u64 {
4848
let mut current = &mut Vec::new();
4949
let mut next = &mut Vec::new();
50+
let next_stage = &mut Vec::new();
5051

5152
// Convert input pairs to ranges.
5253
for [start, length] in input.seeds.iter().copied().chunk::<2>() {
5354
current.push([start, start + length]);
5455
}
5556

5657
for stage in &input.stages {
57-
'outer: for &[s1, e1] in current.iter() {
58-
// Split ranges that overlap into 1, 2 or 3 new ranges.
59-
// Assumes that seed ranges will only overlap with a single range in each stage.
60-
for &[dest, s2, e2] in stage {
58+
for &[dest, s2, e2] in stage {
59+
while let Some([s1, e1]) = current.pop() {
60+
// Split ranges that overlap into 1, 2 or 3 new ranges.
6161
// x1 and x2 are the possible overlap.
6262
let x1 = s1.max(s2);
6363
let x2 = e1.min(e2);
6464

65-
if x1 < x2 {
65+
if x1 >= x2 {
66+
// No overlap.
67+
next.push([s1, e1]);
68+
} else {
69+
// Move overlap to new destination. Only compare with next range.
70+
next_stage.push([x1 - s2 + dest, x2 - s2 + dest]);
71+
72+
// Check remnants with remaining ranges.
6673
if s1 < x1 {
6774
next.push([s1, x1]);
6875
}
6976
if x2 < e1 {
7077
next.push([x2, e1]);
7178
}
72-
// Move overlap to new destination.
73-
next.push([x1 - s2 + dest, x2 - s2 + dest]);
74-
continue 'outer;
7579
}
7680
}
77-
// No intersection with any range so pass to next stage unchanged.
78-
next.push([s1, e1]);
81+
82+
(current, next) = (next, current);
7983
}
8084

81-
(current, next) = (next, current);
82-
next.clear();
85+
// Combine elements for the next stage.
86+
current.append(next_stage);
8387
}
8488

8589
current.iter().map(|r| r[0]).min().unwrap()

0 commit comments

Comments
 (0)