Skip to content

Commit 3b78527

Browse files
feat(2018 day-03): track claims to pieces of cloth
1 parent c5207fb commit 3b78527

File tree

2 files changed

+89
-24
lines changed

2 files changed

+89
-24
lines changed

2018/day-03/claims.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
11
var _conflicts = []
2+
var _claims = []
3+
4+
/**
5+
* Check if a value is within the designated range (inclusive)
6+
* @param {number} s1 start of first range
7+
* @param {number} e1 end of first range
8+
* @param {number} s2 start of first range
9+
* @param {number} e2 end of first range
10+
*/
11+
const _isOverlap = (s1, e1, s2, e2) => {
12+
return Math.max(s1, e1) <= Math.min(s2, e2)
13+
}
14+
15+
/**
16+
* Compares claims to see if they overlap
17+
* @param {Object} c1 claim to compare
18+
* @param {Object*} c2 claim to compare
19+
*/
20+
const _isOverlappingClaim = (c1, c2) => {
21+
// x range overlaps
22+
const x = _isOverlap(
23+
c1.x,
24+
c1.x + c1.width,
25+
c2.x,
26+
c2.x + c2.width
27+
)
28+
// y range overlaps
29+
const y = _isOverlap(
30+
c1.y,
31+
c1.y + c1.height,
32+
c2.y,
33+
c2.y + c2.height
34+
)
35+
return (x && y)
36+
}
37+
38+
/**
39+
* Iterates through the list of claims and checks to see if any cover the specified point
40+
* @param {number} x point x value
41+
* @param {number} y point y value
42+
* @returns {Boolean}
43+
*/
44+
const isClaimed = (x, y) => {
45+
const matches = _claims.filter((claim) =>
46+
x >= claim.x &&
47+
x <= claim.x + claim.width &&
48+
y >= claim.y &&
49+
y <= claim.y + claim.height
50+
)
51+
return (matches.length > 0)
52+
}
253

354
/**
455
* Logs a conflict about multiple claims for a singel point
@@ -17,6 +68,23 @@ const logConflict = (x, y, claims) => {
1768
}
1869
}
1970

71+
const makeClaim = (claim) => {
72+
// Check if there's any overlap with an exisiting claim
73+
const overlaps = _claims.filter((existing) => _isOverlappingClaim(existing, claim))
74+
if (overlaps.length > 0) {
75+
console.log(`Claim ${claim.id} overlaps ${overlaps.length} already existing claims.`)
76+
// step through individual points to log conflicts
77+
for (let x = claim.x; x < claim.x + claim.width; x++) {
78+
for (let y = claim.y; y < claim.y + claim.height; y++) {
79+
if (isClaimed(x, y)) {
80+
logConflict(x, y, [claim.id])
81+
}
82+
}
83+
}
84+
}
85+
_claims.push(claim)
86+
}
87+
2088
/**
2189
* Parses a claim into a usable object
2290
* @param {String} str formatted claim
@@ -43,6 +111,9 @@ const parseClaim = (str) => {
43111

44112
module.exports = {
45113
_conflicts,
114+
_claims,
115+
isClaimed,
46116
logConflict,
117+
makeClaim,
47118
parseClaim
48119
}

2018/day-03/claims.test.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const expect = require('chai').expect
33
let {
44
_conflicts,
5-
cloth,
5+
_claims,
66
isClaimed,
77
logConflict,
88
makeClaim,
@@ -37,35 +37,29 @@ describe('--- Day 3: No Matter How You Slice It ---', () => {
3737
})
3838

3939
describe('makeClaim(claim)', () => {
40-
beforeEach(() => {
41-
// Clear the stateful claims
42-
cloth = {}
43-
})
44-
45-
afterEach(() => {
46-
// Clear the stateful claims
47-
cloth = {}
48-
})
49-
5040
it('claims a piece of cloth for the specified elf', () => {
5141
const claim = parseClaim(claims[0])
5242
makeClaim(claim)
53-
const expected = claim.id
54-
for (let x = claim.x; x < claim.x + claim.width; x++) {
55-
for (let y = claim.y; x < claim.y + claim.height; y++) {
56-
expect(cloth[x][y]).to.equal(expected)
57-
}
58-
}
43+
const actual = _claims.find((el) => el.x === claim.x && el.y === claim.y)
44+
expect(actual).to.deep.equal(claim)
5945
})
6046

61-
it('does not claim points outside the region', () => {
62-
const claim = parseClaim(claims[0])
63-
makeClaim(claim)
64-
expect(cloth.length).to.equal(claim.width)
65-
cloth.forEach((col) => {
66-
expect(col.length).to.equal(claim.height)
67-
})
47+
it('logs an overlap when one is encountered', () => {
48+
const claim1 = claims[1]
49+
const claim2 = claims[2]
50+
makeClaim(claim1)
51+
makeClaim(claim2)
52+
expect(_conflicts)
6853
})
54+
55+
// it('does not claim points outside the region', () => {
56+
// const claim = parseClaim(claims[0])
57+
// makeClaim(claim)
58+
// expect(_cloth.length).to.equal(claim.width)
59+
// _cloth.forEach((col) => {
60+
// expect(col.length).to.equal(claim.height)
61+
// })
62+
// })
6963
})
7064

7165
describe('isClaimed(x,y)', () => {

0 commit comments

Comments
 (0)