Skip to content

Commit bb212f3

Browse files
feat(2021-day-10): score code autocompletes
1 parent 5264b69 commit bb212f3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

2021/day-10/scoring.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
const findMiddleScore = (scores) => {
3+
// According to specs, there's always an odd number of items in the list,
4+
// so we're safe to divide by 2 and round down to get the desired index
5+
return scores.sort((a, b) => a - b)[
6+
Math.floor(scores.length / 2)
7+
]
8+
}
9+
10+
// How many points each character is worth in autocomplete scoring
11+
const pointValues = {
12+
')': 1,
13+
']': 2,
14+
'}': 3,
15+
'>': 4
16+
}
17+
18+
const scoreAutocomplete = (suggestion) => {
19+
return [...suggestion].reduce((score, char) => {
20+
return (score * 5) + pointValues[char]
21+
}, 0)
22+
}
23+
24+
module.exports = {
25+
findMiddleScore,
26+
scoreAutocomplete
27+
}

2021/day-10/scoring.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { findMiddleScore, scoreAutocomplete } = require('./scoring')
4+
5+
const scoreData = [
6+
288957,
7+
5566,
8+
1480781,
9+
995444,
10+
294
11+
]
12+
13+
const autocompleteSuggestions = [
14+
'}}]])})]',
15+
')}>]})',
16+
'}}>}>))))',
17+
']]}}]}]}>',
18+
'])}>'
19+
]
20+
21+
describe('--- Day 10: Syntax Scoring ---', () => {
22+
describe('Part 2', () => {
23+
describe('scoreAutocomplete()', () => {
24+
it('takes a single autocomplete suggestion and scores it', () => {
25+
autocompleteSuggestions.forEach((suggestion, idx) => {
26+
expect(scoreAutocomplete(suggestion)).to.equal(scoreData[idx])
27+
})
28+
})
29+
})
30+
describe('findMiddleScore()', () => {
31+
it('takes a list of scores and returns the middle entry after sorting', () => {
32+
expect(findMiddleScore(scoreData)).to.equal(288957)
33+
})
34+
})
35+
})
36+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy