Skip to content

Commit 42fd519

Browse files
feat(2023-01): split sanitized and unsanitized checksums of sets
Allows for solving parts 1 and 2 independently
1 parent 8d8242f commit 42fd519

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

2023/day-01/checksum.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ const checksumLine = (data) => {
1919
* @param {array} set of lines containing data
2020
*/
2121
const checksumSet = (set) => {
22+
return set.reduce((total, current) => {
23+
return total + checksumLine((current))
24+
}, 0)
25+
}
26+
27+
/**
28+
* Generates the checksum for an entire set when data is not sanitized
29+
* @param {array} set of lines containing data
30+
*/
31+
const checksumUnSanitizedSet = (set) => {
2232
return set.reduce((total, current) => {
2333
return total + checksumLine(sanitizeLine(current))
2434
}, 0)
@@ -34,4 +44,4 @@ const sanitizeLine = (data) => {
3444
return data.replaceAll(reg, (matched) => numbers.indexOf(matched))
3545
}
3646

37-
module.exports = { checksumLine, checksumSet, sanitizeLine }
47+
module.exports = { checksumLine, checksumSet, checksumUnSanitizedSet, sanitizeLine }

2023/day-01/checksum.test.js

Lines changed: 15 additions & 4 deletions
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, sanitizeLine } = require('./checksum')
3+
const { checksumSet, checksumUnSanitizedSet, checksumLine, sanitizeLine } = require('./checksum')
44

55
describe('--- Day 1: Trebuchet?! ---', () => {
66
describe('Part 1', () => {
@@ -37,16 +37,27 @@ describe('--- Day 1: Trebuchet?! ---', () => {
3737
]
3838
const result = [29, 83, 13, 24, 42, 14, 76]
3939
it('cleans up a string when digits are spelled out', () => {
40-
expect(sanitizeLine('two1nine')).to.equal('219')
41-
4240
for (let x = 0; x < set.length; x++) {
4341
expect(checksumLine(sanitizeLine(set[x]))).to.equal(result[x])
4442
}
45-
expect(checksumSet(set)).to.equal(281)
4643
})
4744
it('handles first matches, and doesn\'t allow for multiple words to share letters', () => {
4845
expect(sanitizeLine('eightwothree')).to.equal('8wo3')
4946
})
5047
})
48+
describe('checksumUnsanitizedSet', () => {
49+
it('calculates the checksum for a set of lines by summing the checksum of each sanitized line', () => {
50+
const set = [
51+
'two1nine',
52+
'eightwothree',
53+
'abcone2threexyz',
54+
'xtwone3four',
55+
'4nineeightseven2',
56+
'zoneight234',
57+
'7pqrstsixteen'
58+
]
59+
expect(checksumUnSanitizedSet(set)).to.equal(281)
60+
})
61+
})
5162
})
5263
})

0 commit comments

Comments
 (0)