Skip to content

Commit

Permalink
feat(2021-day-10): score code autocompletes
Browse files Browse the repository at this point in the history
  • Loading branch information
amclin committed Dec 17, 2021
1 parent 5264b69 commit bb212f3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 2021/day-10/scoring.js
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
}
36 changes: 36 additions & 0 deletions 2021/day-10/scoring.test.js
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)
})
})
})
})

0 comments on commit bb212f3

Please sign in to comment.