Skip to content

Commit bb67ea4

Browse files
feat(2018 day-03): identify non-overlapping claim
1 parent f966984 commit bb67ea4

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

2018/day-03/claims.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const unique = require('../helpers').unique
2+
13
var _conflicts = []
24
var _claims = []
35
var _cloth = []
@@ -58,9 +60,21 @@ const claimPoint = (x, y, id) => {
5860
throw new Error('Tried to set a point with an unknown type')
5961
}
6062

61-
const findNonOverlappingClaim = () => {
62-
let claim = 0
63-
return claim
63+
const findNonOverlappingClaims = () => {
64+
// locate all claim IDs that exist in conflicts
65+
let conflicts = []
66+
for (let x = 0; x < _cloth.length; x++) {
67+
for (let y = 0; y < _cloth[x].length; y++) {
68+
if (isConflict(x, y)) {
69+
conflicts = conflicts.concat(_cloth[x][y])
70+
}
71+
}
72+
}
73+
// Keep only one instance of each conflict ID
74+
conflicts = unique(conflicts)
75+
76+
// Filter the list of claims to only those that aren't in the conflicts list
77+
return _claims.filter((id) => conflicts.indexOf(id) < 0)
6478
}
6579

6680
/**
@@ -112,7 +126,7 @@ module.exports = {
112126
_conflicts,
113127
_cloth,
114128
countConflicts,
115-
findNonOverlappingClaim,
129+
findNonOverlappingClaims,
116130
getClaimedList,
117131
isClaimed,
118132
makeClaim,

2018/day-03/claims.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const expect = require('chai').expect
33
var {
44
_cloth,
55
countConflicts,
6-
findNonOverlappingClaim,
6+
findNonOverlappingClaims,
77
getClaimedList,
88
isClaimed,
99
makeClaim,
@@ -103,15 +103,15 @@ describe('--- Day 3: No Matter How You Slice It ---', () => {
103103
})
104104

105105
describe('Part 2', () => {
106-
describe('findNonOverlappingClaim()', () => {
106+
describe('findNonOverlappingClaims()', () => {
107107
it('locates the first claim that doesn\'t have overlapping claims', () => {
108108
let testClaims = claims.map(parseClaim)
109109
for (let x = 1; x < claims.length; x++) {
110110
makeClaim(testClaims[x])
111111
}
112-
const expected = 3
113-
const actual = findNonOverlappingClaim()
114-
expect(actual).to.equal(expected)
112+
const expected = [3]
113+
const actual = findNonOverlappingClaims()
114+
expect(actual).to.deep.equal(expected)
115115
})
116116
})
117117
})

2018/day-03/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => {
1010
const claimsList = inputParser.linesToArray(data)
1111
claimsList.forEach((claim) => claims.makeClaim(claims.parseClaim(claim)))
1212
const answer = claims.countConflicts()
13-
const answer2 = claims.findNonOverlappingClaim()
13+
const answer2 = claims.findNonOverlappingClaims()
1414
console.log(`-- Part 1 --`)
1515
console.log(`Answer: ${answer}`)
1616
console.log(`-- Part 2 --`)

0 commit comments

Comments
 (0)