Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e8c94f

Browse files
Year 2024 speed and code quality improvements
1 parent c22a128 commit 2e8c94f

File tree

5 files changed

+22
-38
lines changed

5 files changed

+22
-38
lines changed

‎README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7676
| Day | Problem | Solution | Benchmark (μs) |
7777
| --- | --- | --- | --: |
7878
| 1 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | [Source](src/year2024/day01.rs) | 21 |
79-
| 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [Source](src/year2024/day02.rs) | 43 |
79+
| 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [Source](src/year2024/day02.rs) | 40 |
8080
| 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [Source](src/year2024/day03.rs) | 8 |
8181
| 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [Source](src/year2024/day04.rs) | 77 |
8282
| 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [Source](src/year2024/day05.rs) | 18 |
@@ -85,15 +85,15 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8585
| 8 | [Resonant Collinearity](https://adventofcode.com/2024/day/8) | [Source](src/year2024/day08.rs) | 8 |
8686
| 9 | [Disk Fragmenter](https://adventofcode.com/2024/day/9) | [Source](src/year2024/day09.rs) | 106 |
8787
| 10 | [Hoof It](https://adventofcode.com/2024/day/10) | [Source](src/year2024/day10.rs) | 38 |
88-
| 11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | [Source](src/year2024/day11.rs) | 248 |
88+
| 11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | [Source](src/year2024/day11.rs) | 227 |
8989
| 12 | [Garden Groups](https://adventofcode.com/2024/day/12) | [Source](src/year2024/day12.rs) | 289 |
9090
| 13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [Source](src/year2024/day13.rs) | 14 |
9191
| 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [Source](src/year2024/day14.rs) | 74 |
9292
| 15 | [Warehouse Woes](https://adventofcode.com/2024/day/15) | [Source](src/year2024/day15.rs) | 303 |
9393
| 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [Source](src/year2024/day16.rs) | 390 |
9494
| 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [Source](src/year2024/day17.rs) | 2 |
9595
| 18 | [RAM Run](https://adventofcode.com/2024/day/18) | [Source](src/year2024/day18.rs) | 42 |
96-
| 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [Source](src/year2024/day19.rs) | 118 |
96+
| 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [Source](src/year2024/day19.rs) | 110 |
9797
| 20 | [Race Condition](https://adventofcode.com/2024/day/20) | [Source](src/year2024/day20.rs) | 1038 |
9898
| 21 | [Keypad Conundrum](https://adventofcode.com/2024/day/21) | [Source](src/year2024/day21.rs) | 19 |
9999
| 22 | [Monkey Market](https://adventofcode.com/2024/day/22) | [Source](src/year2024/day22.rs) | 727 |

‎src/year2024/day02.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@ type Input = (u32, u32);
1919
/// Minimize allocation to only a single `vec` reused for each report.
2020
pub fn parse(input: &str) -> Input {
2121
let mut report = Vec::new();
22-
let mut part_one = 0;
23-
let mut part_two = 0;
2422

25-
for line in input.lines() {
23+
input.lines().fold((0, 0), |(part_one, part_two), line| {
24+
report.clear();
2625
report.extend(line.iter_signed::<i32>());
2726

2827
let (p1, p2) = check(&report);
29-
part_one += p1;
30-
part_two += p2;
31-
32-
report.clear();
33-
}
34-
35-
(part_one, part_two)
28+
(part_one + p1, part_two + p2)
29+
})
3630
}
3731

3832
pub fn part1(input: &Input) -> u32 {
@@ -45,7 +39,7 @@ pub fn part2(input: &Input) -> u32 {
4539

4640
fn check(report: &[i32]) -> (u32, u32) {
4741
let size = report.len();
48-
let score: i32 = (1..size).map(|i| delta(report[i - 1], report[i])).sum();
42+
let score: i32 = report.windows(2).map(|w| delta(w[0], w[1])).sum();
4943

5044
if score.abs() == (size - 1) as i32 {
5145
return (1, 1);
@@ -76,6 +70,5 @@ fn check(report: &[i32]) -> (u32, u32) {
7670
/// Convert each pair of levels to either +1 for increase, -1 for decrease or 0 for invalid range.
7771
fn delta(a: i32, b: i32) -> i32 {
7872
let diff = b - a;
79-
80-
if diff.abs() <= 3 { diff.signum() } else { 0 }
73+
(diff.abs() <= 3) as i32 * diff.signum()
8174
}

‎src/year2024/day07.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,20 @@ type Input = (u64, u64);
4848

4949
pub fn parse(input: &str) -> Input {
5050
let mut equation = Vec::new();
51-
let mut part_one = 0;
52-
let mut part_two = 0;
5351

54-
for line in input.lines() {
52+
input.lines().fold((0, 0), |(part_one, part_two), line| {
53+
equation.clear();
5554
equation.extend(line.iter_unsigned::<u64>());
5655

5756
// If an equation is valid for part one then it's also valid for part two.
5857
if valid(&equation, equation[0], equation.len() - 1, false) {
59-
part_one += equation[0];
60-
part_two += equation[0];
58+
(part_one + equation[0], part_two + equation[0])
6159
} else if valid(&equation, equation[0], equation.len() - 1, true) {
62-
part_two += equation[0];
60+
(part_one, part_two + equation[0])
61+
} else {
62+
(part_one, part_two)
6363
}
64-
65-
equation.clear();
66-
}
67-
68-
(part_one, part_two)
64+
})
6965
}
7066

7167
pub fn part1(input: &Input) -> u64 {

‎src/year2024/day11.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn count(input: &[u64], blinks: usize) -> u64 {
2727
let mut indices = FastMap::with_capacity(5000);
2828
// Numbers of any new stones generated during the previous blink.
2929
let mut todo = Vec::new();
30+
let mut numbers = Vec::new();
3031
// Amount of each stone of a particular number.
3132
let mut current = Vec::new();
3233

@@ -44,8 +45,7 @@ fn count(input: &[u64], blinks: usize) -> u64 {
4445
for _ in 0..blinks {
4546
// If a stone number has already been seen then return its index,
4647
// otherwise queue it for processing during the next blink.
47-
let numbers = todo;
48-
todo = Vec::with_capacity(200);
48+
(numbers, todo) = (todo, numbers);
4949

5050
let mut index_of = |number| {
5151
let size = indices.len();
@@ -56,7 +56,7 @@ fn count(input: &[u64], blinks: usize) -> u64 {
5656
};
5757

5858
// Apply the transformation logic to stones added in the previous blink.
59-
for number in numbers {
59+
for number in numbers.drain(..) {
6060
let (first, second) = if number == 0 {
6161
(index_of(1), usize::MAX)
6262
} else {

‎src/year2024/day19.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ pub fn parse(input: &str) -> Input {
4040
trie[i].set_towel();
4141
}
4242

43-
let mut part_one = 0;
44-
let mut part_two = 0;
4543
let mut ways = Vec::with_capacity(100);
4644

47-
for design insuffix.lines().map(str::as_bytes) {
45+
suffix.lines().map(str::as_bytes).fold((0,0), |(part_one, part_two), design| {
4846
let size = design.len();
4947

5048
// Reset state.
@@ -77,11 +75,8 @@ pub fn parse(input: &str) -> Input {
7775

7876
// Last element is the total possible combinations.
7977
let total = ways[size];
80-
part_one += (total > 0) as usize;
81-
part_two += total;
82-
}
83-
84-
(part_one, part_two)
78+
(part_one + (total > 0) as usize, part_two + total)
79+
})
8580
}
8681

8782
pub fn part1(input: &Input) -> usize {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /