-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2021-day-10): score code autocompletes
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
const findMiddleScore = (scores) => { | ||
// According to specs, there's always an odd number of items in the list, | ||
// so we're safe to divide by 2 and round down to get the desired index | ||
return scores.sort((a, b) => a - b)[ | ||
Math.floor(scores.length / 2) | ||
] | ||
} | ||
|
||
// How many points each character is worth in autocomplete scoring | ||
const pointValues = { | ||
')': 1, | ||
']': 2, | ||
'}': 3, | ||
'>': 4 | ||
} | ||
|
||
const scoreAutocomplete = (suggestion) => { | ||
return [...suggestion].reduce((score, char) => { | ||
return (score * 5) + pointValues[char] | ||
}, 0) | ||
} | ||
|
||
module.exports = { | ||
findMiddleScore, | ||
scoreAutocomplete | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-env mocha */ | ||
const { expect } = require('chai') | ||
const { findMiddleScore, scoreAutocomplete } = require('./scoring') | ||
|
||
const scoreData = [ | ||
288957, | ||
5566, | ||
1480781, | ||
995444, | ||
294 | ||
] | ||
|
||
const autocompleteSuggestions = [ | ||
'}}]])})]', | ||
')}>]})', | ||
'}}>}>))))', | ||
']]}}]}]}>', | ||
'])}>' | ||
] | ||
|
||
describe('--- Day 10: Syntax Scoring ---', () => { | ||
describe('Part 2', () => { | ||
describe('scoreAutocomplete()', () => { | ||
it('takes a single autocomplete suggestion and scores it', () => { | ||
autocompleteSuggestions.forEach((suggestion, idx) => { | ||
expect(scoreAutocomplete(suggestion)).to.equal(scoreData[idx]) | ||
}) | ||
}) | ||
}) | ||
describe('findMiddleScore()', () => { | ||
it('takes a list of scores and returns the middle entry after sorting', () => { | ||
expect(findMiddleScore(scoreData)).to.equal(288957) | ||
}) | ||
}) | ||
}) | ||
}) |