Skip to content

Commit 7d49e67

Browse files
committed
feat: Day 2: Inventory Management System (part 2)
1 parent 539970c commit 7d49e67

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

day-02-inventory-management-system/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,25 @@ Of these box IDs, four of them contain a letter which appears exactly twice, and
2424

2525
**What is the checksum** for your list of box IDs?
2626

27+
## Part Two
28+
29+
Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.
30+
31+
The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:
32+
33+
```
34+
abcde
35+
fghij
36+
klmno
37+
pqrst
38+
fguij
39+
axcye
40+
wvxyz
41+
```
42+
43+
The IDs `abcde` and `axcye` are close, but they differ by two characters (the second and fourth). However, the IDs `fghij` and `fguij` differ by exactly one character, the third (`h` and `u`). Those must be the correct boxes.
44+
45+
**What letters are common between the two correct box IDs?** (In the example above, this is found by removing the differing character from either ID, producing `fgij`.)
46+
2747
## References
2848
- https://adventofcode.com/2018/day/2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// get array without element at index i
2+
const without = (array, i) => [
3+
...array.slice(0, i),
4+
...array.slice(i + 1, array.length),
5+
];
6+
7+
const common = (input) => {
8+
const boxIds = input
9+
.split('\n')
10+
.map((x) => x.trim().split(''));
11+
12+
for (let i = 0; i < boxIds.length; i++) {
13+
const boxId = boxIds[i];
14+
15+
for (let n = 0; n < boxId.length; n++) {
16+
const boxForComparison = without(boxId, n).join('');
17+
18+
const ids = without(boxIds, i)
19+
.map((id) => without(id, n).join(''));
20+
21+
if (ids.some((id) => id === boxForComparison)) {
22+
return boxForComparison;
23+
}
24+
}
25+
}
26+
};
27+
28+
module.exports = common;

day-02-inventory-management-system/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert');
22

33
const checksum = require('./checksum');
4+
const common = require('./common');
45

56
describe('Day 2: Inventory Management System', () => {
67
it('should calculate checksum from box ids', () => {
@@ -15,4 +16,19 @@ describe('Day 2: Inventory Management System', () => {
1516

1617
assert.strictEqual(checksum(boxIds), 12);
1718
});
19+
20+
describe('Part Two', () => {
21+
it('should find common characters', () => {
22+
const boxIds =
23+
`abcde
24+
fghij
25+
klmno
26+
pqrst
27+
fguij
28+
axcye
29+
wvxyz`;
30+
31+
assert.strictEqual(common(boxIds), 'fgij');
32+
});
33+
});
1834
});

0 commit comments

Comments
 (0)