Skip to content

Commit 07b773c

Browse files
committed
feat: Day 3: No Matter How You Slice It (part 2)
1 parent 386a566 commit 07b773c

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

day-03-no-matter-how-you-slice-it/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,13 @@ The four square inches marked with `X` are claimed by **both `1` and `2`**. (Cla
5050

5151
If the Elves all proceed with their own plans, none of them will have enough fabric. **How many square inches of fabric are within two or more claims?**
5252

53+
## Part Two
54+
55+
Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!
56+
57+
For example, in the claims above, only claim `3` is intact after all claims are made.
58+
59+
**What is the ID of the only claim that doesn't overlap?**
60+
5361
## References
5462
- https://adventofcode.com/2018/day/3
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Sheet {
2+
constructor (width = 1000, height = 1000) {
3+
this._grid = Array
4+
.from({ length: height })
5+
.map(() => Array
6+
.from({ length: width })
7+
.map(() => 0));
8+
}
9+
10+
reserve (claim) {
11+
for (let y = 0; y < claim.height; y++) {
12+
for (let x = 0; x < claim.width; x++) {
13+
this._grid[y + claim.top][x + claim.left] += 1;
14+
}
15+
}
16+
}
17+
18+
hasOverlap (claim) {
19+
const content = [];
20+
21+
for (let y = 0; y < claim.height; y++) {
22+
for (let x = 0; x < claim.width; x++) {
23+
content.push(this._grid[y + claim.top][x + claim.left]);
24+
}
25+
}
26+
27+
return content.every((x) => x === 1);
28+
}
29+
}
30+
31+
const fabric = (input, width = 1000, height = 1000) => {
32+
const sheet = new Sheet(width, height);
33+
34+
const claims = input
35+
.split('\n')
36+
.map((x) => {
37+
const parts = x.match(/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/);
38+
39+
return {
40+
id: +parts[1],
41+
top: +parts[3],
42+
left: +parts[2],
43+
width: +parts[4],
44+
height: +parts[5],
45+
};
46+
});
47+
48+
claims.forEach((claim) => sheet.reserve(claim));
49+
50+
for (let i = 0; i < claims.length; i++) {
51+
if (sheet.hasOverlap(claims[i])) {
52+
return claims[i].id;
53+
}
54+
}
55+
};
56+
57+
module.exports = fabric;

day-03-no-matter-how-you-slice-it/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert');
22

33
const fabric = require('./fabric');
4+
const fabric2 = require('./fabric2');
45

56
describe('Day 3: No Matter How You Slice It', () => {
67
it('should calculate overlapping area', () => {
@@ -11,4 +12,15 @@ describe('Day 3: No Matter How You Slice It', () => {
1112

1213
assert.strictEqual(fabric(claims, 11, 9), 4);
1314
});
15+
16+
describe('Part Two', () => {
17+
it('should determine id of lone claim', () => {
18+
const claims =
19+
`#1 @ 1,3: 4x4
20+
#2 @ 3,1: 4x4
21+
#3 @ 5,5: 2x2`;
22+
23+
assert.strictEqual(fabric2(claims, 11, 9), 3);
24+
});
25+
});
1426
});

0 commit comments

Comments
 (0)