11/* eslint-env mocha */
22const { expect } = require ( 'chai' )
3- const { checksumSet, checksumUnSanitizedSet , checksumLine, sanitizeLine } = require ( './checksum' )
3+ const { checksumSet, checksumLine, sanitizeLine, lazyChecksumLine } = require ( './checksum' )
44const fs = require ( 'fs' )
55const path = require ( 'path' )
66const filePath = path . join ( __dirname , 'input.txt' )
@@ -30,7 +30,7 @@ describe('--- Day 1: Trebuchet?! ---', () => {
3030 } )
3131 describe ( 'Part 2' , ( ) => {
3232 describe ( 'sanitizeLine' , ( ) => {
33- const set = [
33+ const data = [
3434 'two1nine' ,
3535 'eightwothree' ,
3636 'abcone2threexyz' ,
@@ -41,40 +41,119 @@ describe('--- Day 1: Trebuchet?! ---', () => {
4141 ]
4242 const result = [ 29 , 83 , 13 , 24 , 42 , 14 , 76 ]
4343 it ( 'cleans up a string when digits are spelled out' , ( ) => {
44+ const set = JSON . parse ( JSON . stringify ( data ) )
4445 for ( let x = 0 ; x < set . length ; x ++ ) {
4546 expect ( checksumLine ( sanitizeLine ( set [ x ] ) ) ) . to . equal ( result [ x ] )
47+ // expect(checksumLine(sanitizeLine(set[x], 'sanitizeByRegex'))).to.equal(result[x])
48+ // expect(checksumLine(sanitizeLine(set[x], 'sanitizeFirstLast'))).to.equal(result[x])
4649 }
4750 } )
48- it ( 'handles first matches, and doesn\'t allow for multiple words to share letters' , ( ) => {
49- expect ( sanitizeLine ( 'eightwothree' ) ) . to . equal ( '8wo3' )
51+ it ( 'allows for skipping sanitation' , ( ) => {
52+ const set = JSON . parse ( JSON . stringify ( data ) )
53+ for ( let x = 0 ; x < set . length ; x ++ ) {
54+ expect ( sanitizeLine ( set [ x ] , 'none' ) ) . to . equal ( data [ x ] )
55+ }
5056 } )
5157 } )
52- describe ( 'checksumUnSanitizedSet ' , ( ) => {
53- it ( 'calculates the checksum for a set of lines by summing the checksum of each sanitized line' , ( ) => {
54- const set = [
55- 'two1nine ',
56- 'eightwothree ',
57- 'abcone2threexyz ',
58- 'xtwone3four ',
59- '4nineeightseven2 ',
60- 'zoneight234' ,
61- '7pqrstsixteen'
62- ]
63- expect ( checksumUnSanitizedSet ( set ) ) . to . equal ( 281 )
58+ describe ( 'checksumSet ' , ( ) => {
59+ const data = [
60+ 'two1nine' ,
61+ 'eightwothree ',
62+ 'abcone2threexyz ',
63+ 'xtwone3four ',
64+ '4nineeightseven2 ',
65+ 'zoneight234 ',
66+ '7pqrstsixteen'
67+ ]
68+ it ( 'can sanitize' , ( ) => {
69+ expect ( checksumSet ( data , true ) ) . to . equal ( 281 )
6470 } )
6571 } )
72+ describe ( 'lazyChecksumLine' , ( ) => {
73+ const data = [
74+ 'two1nine' ,
75+ 'eightwothree' ,
76+ 'abcone2threexyz' ,
77+ 'xtwone3four' ,
78+ '4nineeightseven2' ,
79+ 'zoneight234' ,
80+ '7pqrstsixteen'
81+ ]
82+ const result = [ 29 , 83 , 13 , 24 , 42 , 14 , 76 ]
83+ it ( 'can match text or numeric for checksum calcs' , ( ) => {
84+ const set = JSON . parse ( JSON . stringify ( data ) )
85+ for ( let x = 0 ; x < set . length ; x ++ ) {
86+ expect ( lazyChecksumLine ( set [ x ] ) ) . to . equal ( result [ x ] )
87+ }
88+ } )
89+ } )
90+
91+ // it('doesn`t sanitize by default', () => {
92+ // const set = [
93+ // 'two1nine',
94+ // 'eightwothree',
95+ // 'abcone2threexyz',
96+ // 'xtwone3four',
97+ // '4nineeightseven2',
98+ // 'zoneight234',
99+ // '7pqrstsixteen'
100+ // ]
101+ // expect(checksumSet(set)).to.be.NaN
102+ // })
103+ // it('allows for sanitizing to be explicitly disabled', () => {
104+ // const set = [
105+ // 'two1nine',
106+ // 'eightwothree',
107+ // 'abcone2threexyz',
108+ // 'xtwone3four',
109+ // '4nineeightseven2',
110+ // 'zoneight234',
111+ // '7pqrstsixteen'
112+ // ]
113+ // expect(checksumSet(set, 'none')).to.be.NaN
114+ // })
115+ // // it('calculates the checksum for a set of lines by summing the checksum of each line', () => {
116+ // // const set = [
117+ // // 'two1nine',
118+ // // 'eightwothree',
119+ // // 'abcone2threexyz',
120+ // // 'xtwone3four',
121+ // // '4nineeightseven2',
122+ // // 'zoneight234',
123+ // // '7pqrstsixteen'
124+ // // ]
125+ // // expect(checksumSet(set)).to.equal(281)
126+ // // })
127+ // })
66128 describe ( 'integeration' , ( ) => {
67- it ( '53853 is too low for part 2' , ( done ) => {
68- let data
69- fs . readFile ( filePath , { encoding : 'utf8' } , ( err , initData ) => {
129+ let initData
130+ before ( ( done ) => {
131+ fs . readFile ( filePath , { encoding : 'utf8' } , ( err , rawData ) => {
70132 if ( err ) throw err
71- initData = inputToArray ( initData . trim ( ) )
133+ initData = inputToArray ( rawData . trim ( ) )
72134 // Deep copy to ensure we aren't mutating the original data
73- data = JSON . parse ( JSON . stringify ( initData ) )
74- expect ( checksumUnSanitizedSet ( data ) ) . to . be . greaterThan ( 53853 )
135+ // data = JSON.parse(JSON.stringify(initData))
75136 done ( )
76137 } )
77138 } )
139+
140+ it ( 'is not done without sanitation, since that matches part 1' , ( ) => {
141+ const data = JSON . parse ( JSON . stringify ( initData ) )
142+ const result = checksumSet ( data , true )
143+ expect ( result ) . to . not . equal ( 54953 )
144+ } )
145+
146+ it ( 'is not done with a one-time regex' , ( ) => {
147+ const data = JSON . parse ( JSON . stringify ( initData ) )
148+ const result = checksumSet ( data , true )
149+ expect ( result ) . to . not . equal ( 53885 ) // result of one-time regex
150+ } )
151+
152+ it ( 'is not done by sanitizing just the first and last strings' , ( ) => {
153+ const data = JSON . parse ( JSON . stringify ( initData ) )
154+ const result = checksumSet ( data , true )
155+ expect ( result ) . to . not . equal ( 53853 ) // result of first/last substitution onlye
156+ } )
78157 } )
79158 } )
80159} )
0 commit comments