Skip to content

Commit 27b17b1

Browse files
committed
Year 2018 Day 2
1 parent e3296a3 commit 27b17b1

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
238238
| Day | Problem | Solution | Benchmark (μs) |
239239
| --- | --- | --- | --: |
240240
| 1 | [Chronal Calibration](https://adventofcode.com/2018/day/1) | [Source](src/year2018/day01.rs) | 15 |
241+
| 2 | [Inventory Management System](https://adventofcode.com/2018/day/2) | [Source](src/year2018/day02.rs) | 77 |
241242

242243
## 2017
243244

benches/benchmark.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ mod year2017 {
130130

131131
mod year2018 {
132132
benchmark!(year2018, day01);
133+
benchmark!(year2018, day02);
133134
}
134135

135136
mod year2019 {

src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ pub mod year2017 {
110110
pub mod day25;
111111
}
112112

113-
/// # Travel through time to restore the seasonal timeline.
113+
/// # Travel through time to restore the festive timeline.
114114
pub mod year2018 {
115115
pub mod day01;
116+
pub mod day02;
116117
}
117118

118119
/// # Rescue Santa from deep space with a solar system adventure.

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn year2017() -> Vec<Solution> {
178178
}
179179

180180
fn year2018() -> Vec<Solution> {
181-
vec![solution!(year2018, day01)]
181+
vec![solution!(year2018, day01), solution!(year2018, day02)]
182182
}
183183

184184
fn year2019() -> Vec<Solution> {

src/year2018/day02.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! # Inventory Management System
2+
use crate::util::hash::*;
3+
4+
pub fn parse(input: &str) -> Vec<&[u8]> {
5+
input.lines().map(str::as_bytes).collect()
6+
}
7+
8+
pub fn part1(input: &[&[u8]]) -> u32 {
9+
let mut total_twos = 0;
10+
let mut total_threes = 0;
11+
12+
for &id in input {
13+
// Ids are lowercase ASCII only with cardinality of 26.
14+
let mut freq = [0; 26];
15+
let mut twos = 0;
16+
let mut threes = 0;
17+
18+
for &b in id {
19+
let index = (b - b'a') as usize;
20+
let current = freq[index];
21+
22+
match current {
23+
0 => (),
24+
1 => twos += 1,
25+
2 => {
26+
twos -= 1;
27+
threes += 1;
28+
}
29+
_ => threes -= 1,
30+
}
31+
32+
freq[index] += 1;
33+
}
34+
35+
if twos > 0 {
36+
total_twos += 1;
37+
}
38+
if threes > 0 {
39+
total_threes += 1;
40+
}
41+
}
42+
43+
total_twos * total_threes
44+
}
45+
46+
pub fn part2(input: &[&[u8]]) -> String {
47+
let width = input[0].len();
48+
49+
let mut seen = FastSet::with_capacity(input.len());
50+
let mut buffer = [0; 32];
51+
52+
// Use a set to check for duplicates after replacing a single character with '*' in each column.
53+
for column in 0..width {
54+
for &id in input {
55+
buffer[0..width].copy_from_slice(id);
56+
buffer[column] = b'*';
57+
58+
if !seen.insert(buffer) {
59+
// Convert to String
60+
return buffer
61+
.iter()
62+
.filter(|&&b| b.is_ascii_lowercase())
63+
.map(|&b| b as char)
64+
.collect();
65+
}
66+
}
67+
68+
seen.clear();
69+
}
70+
71+
unreachable!()
72+
}

tests/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ mod year2017 {
114114

115115
mod year2018 {
116116
mod day01_test;
117+
mod day02_test;
117118
}
118119

119120
mod year2019 {

tests/year2018/day02_test.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use aoc::year2018::day02::*;
2+
3+
const FIRST_EXAMPLE: &str = "\
4+
abcdef
5+
bababc
6+
abbcde
7+
abcccd
8+
aabcdd
9+
abcdee
10+
ababab";
11+
12+
const SECOND_EXAMPLE: &str = "\
13+
abcde
14+
fghij
15+
klmno
16+
pqrst
17+
fguij
18+
axcye
19+
wvxyz";
20+
21+
#[test]
22+
fn part1_test() {
23+
let input = parse(FIRST_EXAMPLE);
24+
assert_eq!(part1(&input), 12);
25+
}
26+
27+
#[test]
28+
fn part2_test() {
29+
let input = parse(SECOND_EXAMPLE);
30+
assert_eq!(part2(&input), "fgij");
31+
}

0 commit comments

Comments
 (0)