File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments