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 49af580

Browse files
Year 2017 speed and code quality improvements
1 parent 73acfe5 commit 49af580

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

‎README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,14 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
302302
| 1 | [Inverse Captcha](https://adventofcode.com/2017/day/1) | [Source](src/year2017/day01.rs) | 1 |
303303
| 2 | [Corruption Checksum](https://adventofcode.com/2017/day/2) | [Source](src/year2017/day02.rs) | 2 |
304304
| 3 | [Spiral Memory](https://adventofcode.com/2017/day/3) | [Source](src/year2017/day03.rs) | 2 |
305-
| 4 | [High-Entropy Passphrases](https://adventofcode.com/2017/day/4) | [Source](src/year2017/day04.rs) | 98 |
305+
| 4 | [High-Entropy Passphrases](https://adventofcode.com/2017/day/4) | [Source](src/year2017/day04.rs) | 86 |
306306
| 5 | [A Maze of Twisty Trampolines, All Alike](https://adventofcode.com/2017/day/5) | [Source](src/year2017/day05.rs) | 22000 |
307307
| 6 | [Memory Reallocation](https://adventofcode.com/2017/day/6) | [Source](src/year2017/day06.rs) | 81 |
308308
| 7 | [Recursive Circus](https://adventofcode.com/2017/day/7) | [Source](src/year2017/day07.rs) | 93 |
309309
| 8 | [I Heard You Like Registers](https://adventofcode.com/2017/day/8) | [Source](src/year2017/day08.rs) | 47 |
310310
| 9 | [Stream Processing](https://adventofcode.com/2017/day/9) | [Source](src/year2017/day09.rs) | 23 |
311311
| 10 | [Knot Hash](https://adventofcode.com/2017/day/10) | [Source](src/year2017/day10.rs) | 66 |
312-
| 11 | [Hex Ed](https://adventofcode.com/2017/day/11) | [Source](src/year2017/day11.rs) | 18 |
312+
| 11 | [Hex Ed](https://adventofcode.com/2017/day/11) | [Source](src/year2017/day11.rs) | 15 |
313313
| 12 | [Digital Plumber](https://adventofcode.com/2017/day/12) | [Source](src/year2017/day12.rs) | 61 |
314314
| 13 | [Packet Scanners](https://adventofcode.com/2017/day/13) | [Source](src/year2017/day13.rs) | 1 |
315315
| 14 | [Disk Defragmentation](https://adventofcode.com/2017/day/14) | [Source](src/year2017/day14.rs) | 438 |
@@ -321,7 +321,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
321321
| 20 | [Particle Swarm](https://adventofcode.com/2017/day/20) | [Source](src/year2017/day20.rs) | 245 |
322322
| 21 | [Fractal Art](https://adventofcode.com/2017/day/21) | [Source](src/year2017/day21.rs) | 5 |
323323
| 22 | [Sporifica Virus](https://adventofcode.com/2017/day/22) | [Source](src/year2017/day22.rs) | 36000 |
324-
| 23 | [Coprocessor Conflagration](https://adventofcode.com/2017/day/23) | [Source](src/year2017/day23.rs) | 19 |
324+
| 23 | [Coprocessor Conflagration](https://adventofcode.com/2017/day/23) | [Source](src/year2017/day23.rs) | 16 |
325325
| 24 | [Electromagnetic Moat](https://adventofcode.com/2017/day/24) | [Source](src/year2017/day24.rs) | 275 |
326326
| 25 | [The Halting Problem](https://adventofcode.com/2017/day/25) | [Source](src/year2017/day25.rs) | 3698 |
327327

‎src/year2017/day02.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ pub fn parse(input: &str) -> Input {
1919
}
2020

2121
pub fn part1(input: &Input) -> u32 {
22-
input.iter().map(|values| values[values.len() - 1] - values[0]).sum()
22+
input.iter().map(|values| values.last().unwrap() - values.first().unwrap()).sum()
2323
}
2424

2525
pub fn part2(input: &Input) -> u32 {
2626
input
2727
.iter()
2828
.map(|values| {
29-
for i in 0..values.len() {
30-
for j in i + 1..values.len() {
31-
if values[j].is_multiple_of(values[i]) {
32-
return values[j] / values[i];
29+
for (i,&smaller)in values.iter().enumerate() {
30+
for &larger in &values[i + 1..] {
31+
if larger.is_multiple_of(smaller) {
32+
return larger / smaller;
3333
}
3434
}
3535
}

‎src/year2017/day04.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ pub fn part1(input: &Input<'_>) -> usize {
3232
}
3333

3434
pub fn part2(input: &Input<'_>) -> usize {
35-
// Calculate the frequency of each letter as anagrams will have the same values.
36-
fn convert(token: &str) -> [u8; 26] {
37-
let mut freq = [0; 26];
35+
// Only 26 elements are needed but 32 is faster to hash.
36+
fn convert(token: &str) -> [u8; 32] {
37+
let mut freq = [0; 32];
3838
for b in token.bytes() {
3939
freq[(b - b'a') as usize] += 1;
4040
}

‎src/year2017/day11.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,32 @@ pub fn parse(input: &str) -> Input {
1616
let mut part_two = 0;
1717

1818
while let Some(first) = iter.next() {
19-
match first {
20-
b'n' => match iter.next().unwrap_or(0) {
19+
if first == b'n' {
20+
match iter.next().unwrap_or(0) {
2121
b'e' => {
22+
iter.next(); // Consume trailing delimeter.
2223
q += 1;
2324
r -= 1;
2425
}
25-
b'w' => q -= 1,
26+
b'w' => {
27+
iter.next(); // Consume trailing delimeter.
28+
q -= 1;
29+
}
2630
_ => r -= 1,
27-
},
28-
b's' => match iter.next().unwrap_or(0) {
29-
b'e' => q += 1,
31+
}
32+
} else {
33+
match iter.next().unwrap_or(0) {
34+
b'e' => {
35+
iter.next(); // Consume trailing delimeter.
36+
q += 1;
37+
}
3038
b'w' => {
39+
iter.next(); // Consume trailing delimeter.
3140
q -= 1;
3241
r += 1;
3342
}
3443
_ => r += 1,
35-
},
36-
_ => (),
44+
}
3745
}
3846

3947
// q + r + s = 0, so we can always determine s given the other two.

‎src/year2017/day23.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,30 @@ pub fn parse(input: &str) -> u32 {
5858

5959
/// The number of `mul` operations is `(n - 2)2`
6060
pub fn part1(input: &u32) -> u32 {
61-
(input - 2) * (input - 2)
61+
let n = input - 2;
62+
n * n
6263
}
6364

6465
/// Count the number of composite numbers in a range calculated from the input number.
6566
pub fn part2(input: &u32) -> usize {
66-
(0..=1000).filter_map(|n| composite(100_000 + 100 * input + 17 * n)).count()
67+
let start = 100_000 + 100 * input;
68+
let end = start + 17001;
69+
(start..end).step_by(17).filter(|&n| is_composite(n)).count()
6770
}
6871

6972
/// Simple [prime number check](https://en.wikipedia.org/wiki/Primality_test)
7073
/// of all factors from 2 to √n inclusive.
71-
fn composite(n: u32) -> Option<u32> {
74+
fn is_composite(n: u32) -> bool {
7275
if n.is_multiple_of(2) {
73-
return Some(n);
76+
return true;
7477
}
75-
for f in (3..).step_by(2).take_while(|m| m * m <= n) {
78+
79+
let root = n.isqrt() + 1;
80+
for f in (3..root).step_by(2) {
7681
if n.is_multiple_of(f) {
77-
return Some(n);
82+
return true;
7883
}
7984
}
8085

81-
None
86+
false
8287
}

0 commit comments

Comments
(0)

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