Skip to content

Commit 4e3f60c

Browse files
feat(2023-02): solution for day 2 part 1
1 parent 5416c66 commit 4e3f60c

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

2023/day-02/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-02/solution.js

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

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('./2023/day-01/solution')
1+
require('./2023/day-02/solution')

0 commit comments

Comments
 (0)