Skip to content

Commit e436ac3

Browse files
feat(2023-01): generate checksums for trebuchet data
1 parent 5124db0 commit e436ac3

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

2023/day-01/checksum.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Generates a checksum for a string by concatenating
3+
* the first and last digits found in the string
4+
* @param string containing a single line of data
5+
*/
6+
const checksumLine = (data) => {
7+
data.replace(/([^0-9])+/g, '') // trim non-numeric characters
8+
const checksumString = `${data[0]}${data[-1]}`
9+
return parseInt(checksumString)
10+
}
11+
12+
/**
13+
* Generates the checksum for an entire set
14+
* @param Arrray of lines containing data
15+
*/
16+
const checksumSet = (set) => {
17+
return set.reduce((total, current) => {
18+
return total + checksumLine(current)
19+
}, 0)
20+
}
21+
22+
module.exports = { checksumLine, checksumSet }

2023/day-01/checksum.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { checksumSet, checksumLine } = require('./checksum')
4+
5+
describe('--- Day 1: Trebuchet?! ---', () => {
6+
describe('Part 1', () => {
7+
describe('checksum', () => {
8+
it('calculates the checksum for a string by concatentating the first and last number', () => {
9+
// provided
10+
expect(checksumLine('1abc2')).to.equal(12)
11+
expect(checksumLine('pqr3stu8vwx')).to.equal(18)
12+
expect(checksumLine('a1b2c3d4e5f')).to.equal(15)
13+
expect(checksumLine('treb7uchet')).to.equal(77)
14+
})
15+
})
16+
describe('checksumSet', () => {
17+
it('calculates the checksum for a set of lines by summing the checksum of each line', () => {
18+
// provided
19+
const set = ['1abc2', 'pqr3stu8vwx', 'a1b2c3d4e5f', 'treb7uchet']
20+
expect(checksumSet(set)).to.equal(142)
21+
})
22+
})
23+
})
24+
})

2023/day-01/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const console = require('../helpers')
3+
require('./solution')

2023/day-01/input.txt

Whitespace-only changes.

2023/day-01/solution.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const filePath = path.join(__dirname, 'input.txt')
4+
const { inputToArray } = require('../../2018/inputParser')
5+
6+
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
7+
if (err) throw err
8+
9+
initData = inputToArray(initData.trim())
10+
11+
const resetInput = () => {
12+
// Deep copy to ensure we aren't mutating the original data
13+
return JSON.parse(JSON.stringify(initData))
14+
}
15+
16+
const part1 = () => {
17+
const data = resetInput()
18+
console.debug(data)
19+
return 'No answer yet'
20+
}
21+
22+
const part2 = () => {
23+
const data = resetInput()
24+
console.debug(data)
25+
return 'No answer yet'
26+
}
27+
const answers = []
28+
answers.push(part1())
29+
answers.push(part2())
30+
31+
answers.forEach((ans, idx) => {
32+
console.info(`-- Part ${idx + 1} --`)
33+
console.info(`Answer: ${ans}`)
34+
})
35+
})

0 commit comments

Comments
 (0)