Skip to content

Commit 4e7a14a

Browse files
committed
perf(11/2015): skip ambiguous letters during increment
1 parent d692b0c commit 4e7a14a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
| [Day 8: Matchsticks](src/solutions/year2015/day08.rs) | ⭐⭐ | 0.052 | 0.129 |
9898
| [Day 9: All in a Single Night](src/solutions/year2015/day09.rs) | ⭐⭐ | 34.320 | 34.850 |
9999
| [Day 10: Elves Look, Elves Say](src/solutions/year2015/day10.rs) | ⭐⭐ | 2.099 | 32.258 |
100-
| [Day 11: Corporate Policy](src/solutions/year2015/day11.rs) | ⭐⭐ | 0.543 | 19.202 |
100+
| [Day 11: Corporate Policy](src/solutions/year2015/day11.rs) | ⭐⭐ | 0.543 | 14.384 |
101101

102102
# TODO
103103

src/solutions/year2015/day11.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ impl<const N: usize> Password<N> {
7272
i -= 1;
7373
continue;
7474
} else {
75-
//fixme: if current letter is ambiguous we can skip to the next (optimization)
76-
chars[i] += 1;
75+
if Self::is_letter_ambiguous(chars[i] + 1) {
76+
chars[i] += 2;
77+
} else {
78+
chars[i] += 1;
79+
}
7780
break;
7881
}
7982
}
@@ -94,14 +97,18 @@ impl<const N: usize> Password<N> {
9497

9598
fn not_contains_ambiguous_letters(&self) -> bool {
9699
for i in 0..N {
97-
if self.chars[i] == b'i' || self.chars[i] == b'o' || self.chars[i] == b'l' {
100+
if Self::is_letter_ambiguous(self.chars[i]) {
98101
return false;
99102
}
100103
}
101104

102105
true
103106
}
104107

108+
fn is_letter_ambiguous(char: u8) -> bool {
109+
char == b'i' || char == b'o' || char == b'l'
110+
}
111+
105112
fn contains_nonoverlapping_two_pairs(&self) -> bool {
106113
let mut i = 0;
107114
let mut pairs = 0;

0 commit comments

Comments
 (0)