Skip to content

Commit 8d8242f

Browse files
feat(2023-01): sanitize inputs with spelled-out numbers
1 parent bb1df5d commit 8d8242f

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

2023/day-01/checksum.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
22
* Generates a checksum for a string by concatenating
33
* the first and last digits found in the string
4-
* @param string containing a single line of data
4+
* @param {string} data of a single line
55
*/
66
const checksumLine = (data) => {
7-
const parsed = data.replace(/([^0-9])/g, '') // trim non-numeric characters
7+
const parsed = data.replaceAll(/([^0-9])/g, '') // trim non-numeric characters
88
let result = ''
99
if (parsed.length === 1) { // some strings only have a single digit
1010
result = `${parsed}${parsed}`
@@ -16,12 +16,22 @@ const checksumLine = (data) => {
1616

1717
/**
1818
* Generates the checksum for an entire set
19-
* @param Arrray of lines containing data
19+
* @param {array} set of lines containing data
2020
*/
2121
const checksumSet = (set) => {
2222
return set.reduce((total, current) => {
23-
return total + checksumLine(current)
23+
return total + checksumLine(sanitizeLine(current))
2424
}, 0)
2525
}
2626

27-
module.exports = { checksumLine, checksumSet }
27+
const numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
28+
const reg = new RegExp(numbers.join('|'), 'g')
29+
/**
30+
* Sanitzizes a line by replacing spelled-out numbers with data
31+
* @param {string} data line of input to sanitize
32+
*/
33+
const sanitizeLine = (data) => {
34+
return data.replaceAll(reg, (matched) => numbers.indexOf(matched))
35+
}
36+
37+
module.exports = { checksumLine, checksumSet, sanitizeLine }

2023/day-01/checksum.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { checksumSet, checksumLine } = require('./checksum')
3+
const { checksumSet, checksumLine, sanitizeLine } = require('./checksum')
44

55
describe('--- Day 1: Trebuchet?! ---', () => {
66
describe('Part 1', () => {
@@ -24,4 +24,29 @@ describe('--- Day 1: Trebuchet?! ---', () => {
2424
})
2525
})
2626
})
27+
describe('Part 2', () => {
28+
describe('sanitizeLine', () => {
29+
const set = [
30+
'two1nine',
31+
'eightwothree',
32+
'abcone2threexyz',
33+
'xtwone3four',
34+
'4nineeightseven2',
35+
'zoneight234',
36+
'7pqrstsixteen'
37+
]
38+
const result = [29, 83, 13, 24, 42, 14, 76]
39+
it('cleans up a string when digits are spelled out', () => {
40+
expect(sanitizeLine('two1nine')).to.equal('219')
41+
42+
for (let x = 0; x < set.length; x++) {
43+
expect(checksumLine(sanitizeLine(set[x]))).to.equal(result[x])
44+
}
45+
expect(checksumSet(set)).to.equal(281)
46+
})
47+
it('handles first matches, and doesn\'t allow for multiple words to share letters', () => {
48+
expect(sanitizeLine('eightwothree')).to.equal('8wo3')
49+
})
50+
})
51+
})
2752
})

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