Skip to content

Commit dc29bb3

Browse files
committed
Faster fixed size array instead of vec
1 parent b045d81 commit dc29bb3

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ pie
109109
"Day 18" : 1904
110110
"Day 23" : 1709
111111
"Day 19" : 933
112-
"Others" : 1941
112+
"Others" : 1916
113113
```
114114

115115
| Day | Problem | Solution | Benchmark (μs) |
116116
| --- | --- | --- | --: |
117117
| 1 | [Sonar Sweep](https://adventofcode.com/2021/day/1) | [Source](src/year2021/day01.rs) | 22 |
118118
| 2 | [Dive!](https://adventofcode.com/2021/day/2) | [Source](src/year2021/day02.rs) | 15 |
119119
| 3 | [Binary Diagnostic](https://adventofcode.com/2021/day/3) | [Source](src/year2021/day03.rs) | 20 |
120-
| 4 | [Giant Squid](https://adventofcode.com/2021/day/4) | [Source](src/year2021/day04.rs) | 45 |
120+
| 4 | [Giant Squid](https://adventofcode.com/2021/day/4) | [Source](src/year2021/day04.rs) | 20 |
121121
| 5 | [Hydrothermal Venture](https://adventofcode.com/2021/day/5) | [Source](src/year2021/day05.rs) | 202 |
122122
| 6 | [Lanternfish](https://adventofcode.com/2021/day/6) | [Source](src/year2021/day06.rs) | 2 |
123123
| 7 | [The Treachery of Whales](https://adventofcode.com/2021/day/7) | [Source](src/year2021/day07.rs) | 19 |

src/year2021/day04.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! score for each board. Sort each result by turn and the answers for part 1 and part1 are the
1414
//! first and last values respectively.
1515
use crate::util::parse::*;
16+
use std::array::from_fn;
1617

1718
pub struct Input {
1819
turn: usize,
@@ -23,23 +24,25 @@ pub fn parse(input: &str) -> Vec<Input> {
2324
let mut to_turn = [0; 100];
2425
let mut from_turn = [0; 100];
2526

26-
let chunks: Vec<Vec<usize>> =
27-
input.split("\n\n").map(|s| s.iter_unsigned().collect()).collect();
27+
let mut chunks = input.split("\n\n");
2828

29-
for (i, &n) in chunks[0].iter().enumerate() {
29+
for (i, n) in chunks.next().unwrap().iter_unsigned().enumerate() {
3030
to_turn[n] = i;
3131
from_turn[i] = n;
3232
}
3333

34-
let score = |board: &Vec<usize>| {
35-
let turns: Vec<_> = board.iter().map(|&n| to_turn[n]).collect();
34+
let score = |chunk: &str| {
35+
let mut iter = chunk.iter_unsigned();
36+
let board: [usize; 25] = from_fn(|_| iter.next().unwrap());
37+
let turns: [usize; 25] = from_fn(|i| to_turn[board[i]]);
38+
3639
let row_and_cols = [
3740
turns[0..5].iter().max().unwrap(),
3841
turns[5..10].iter().max().unwrap(),
3942
turns[10..15].iter().max().unwrap(),
4043
turns[15..20].iter().max().unwrap(),
4144
turns[20..25].iter().max().unwrap(),
42-
turns.iter().skip(0).step_by(5).max().unwrap(),
45+
turns.iter().step_by(5).max().unwrap(),
4346
turns.iter().skip(1).step_by(5).max().unwrap(),
4447
turns.iter().skip(2).step_by(5).max().unwrap(),
4548
turns.iter().skip(3).step_by(5).max().unwrap(),
@@ -48,18 +51,19 @@ pub fn parse(input: &str) -> Vec<Input> {
4851
let winning_turn = **row_and_cols.iter().min().unwrap();
4952
let unmarked: usize = board.iter().filter(|&&n| to_turn[n] > winning_turn).sum();
5053
let just_called = from_turn[winning_turn];
54+
5155
Input { turn: winning_turn, score: unmarked * just_called }
5256
};
5357

54-
let mut result: Vec<_> = chunks.iter().skip(1).map(score).collect();
55-
result.sort_unstable_by(|a, b| a.turn.cmp(&b.turn));
56-
result
58+
let mut scores: Vec<_> = chunks.map(score).collect();
59+
scores.sort_unstable_by_key(|s| s.turn);
60+
scores
5761
}
5862

5963
pub fn part1(input: &[Input]) -> usize {
60-
input[0].score
64+
input.first().unwrap().score
6165
}
6266

6367
pub fn part2(input: &[Input]) -> usize {
64-
input[input.len() - 1].score
68+
input.last().unwrap().score
6569
}

0 commit comments

Comments
 (0)