Skip to content

Commit

Permalink
feat(2023-01): sanitize inputs with spelled-out numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
amclin committed Dec 4, 2023
1 parent bb1df5d commit 8d8242f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
20 changes: 15 additions & 5 deletions 2023/day-01/checksum.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Generates a checksum for a string by concatenating
* the first and last digits found in the string
* @param string containing a single line of data
* @param {string} data of a single line
*/
const checksumLine = (data) => {
const parsed = data.replace(/([^0-9])/g, '') // trim non-numeric characters
const parsed = data.replaceAll(/([^0-9])/g, '') // trim non-numeric characters
let result = ''
if (parsed.length === 1) { // some strings only have a single digit
result = `${parsed}${parsed}`
Expand All @@ -16,12 +16,22 @@ const checksumLine = (data) => {

/**
* Generates the checksum for an entire set
* @param Arrray of lines containing data
* @param {array} set of lines containing data
*/
const checksumSet = (set) => {
return set.reduce((total, current) => {
return total + checksumLine(current)
return total + checksumLine(sanitizeLine(current))
}, 0)
}

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

module.exports = { checksumLine, checksumSet, sanitizeLine }
27 changes: 26 additions & 1 deletion 2023/day-01/checksum.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { checksumSet, checksumLine } = require('./checksum')
const { checksumSet, checksumLine, sanitizeLine } = require('./checksum')

describe('--- Day 1: Trebuchet?! ---', () => {
describe('Part 1', () => {
Expand All @@ -24,4 +24,29 @@ describe('--- Day 1: Trebuchet?! ---', () => {
})
})
})
describe('Part 2', () => {
describe('sanitizeLine', () => {
const set = [
'two1nine',
'eightwothree',
'abcone2threexyz',
'xtwone3four',
'4nineeightseven2',
'zoneight234',
'7pqrstsixteen'
]
const result = [29, 83, 13, 24, 42, 14, 76]
it('cleans up a string when digits are spelled out', () => {
expect(sanitizeLine('two1nine')).to.equal('219')

for (let x = 0; x < set.length; x++) {
expect(checksumLine(sanitizeLine(set[x]))).to.equal(result[x])
}
expect(checksumSet(set)).to.equal(281)
})
it('handles first matches, and doesn\'t allow for multiple words to share letters', () => {
expect(sanitizeLine('eightwothree')).to.equal('8wo3')
})
})
})
})

0 comments on commit 8d8242f

Please sign in to comment.