Skip to content

Commit a3205bc

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

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

2023/day-02/game.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,29 @@ const checksumGameSet = (games, limit) => {
6868
}, 0)
6969
}
7070

71+
const countCubesNeeded = (game) => {
72+
const max = game.draws.reduce((acc, draw) => {
73+
const drawData = parseHex(draw)
74+
return {
75+
r: Math.max(acc.r, drawData.r),
76+
g: Math.max(acc.g, drawData.g),
77+
b: Math.max(acc.b, drawData.b)
78+
}
79+
}, { r: 0, g: 0, b: 0 })
80+
81+
return max
82+
}
83+
84+
const power = (game) => {
85+
const needed = countCubesNeeded(game)
86+
return needed.r * needed.g * needed.b
87+
}
88+
7189
module.exports = {
7290
parseGame,
7391
validateGame,
7492
checksumGameSet,
75-
validateDraw
93+
validateDraw,
94+
countCubesNeeded,
95+
power
7696
}

2023/day-02/game.test.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { parseGame, validateGame, checksumGameSet, validateDraw } = require('./game')
3+
const { parseGame, validateGame, checksumGameSet, validateDraw, countCubesNeeded, power } = require('./game')
44
const { linesToArray } = require('../../2018/inputParser')
55
const fs = require('fs')
66
const path = require('path')
@@ -164,7 +164,7 @@ describe('--- Day 2: Cube Conundrum ---', () => {
164164
})
165165
})
166166

167-
describe('integration test', () => {
167+
describe.skip('integration test', () => {
168168
let initData
169169
before((done) => {
170170
fs.readFile(filePath, { encoding: 'utf8' }, (err, rawData) => {
@@ -186,4 +186,43 @@ describe('--- Day 2: Cube Conundrum ---', () => {
186186
})
187187
})
188188
})
189+
190+
describe('Part 2', () => {
191+
describe('countCubesNeeded', () => {
192+
it('counts how many cubes are needed for a game', () => {
193+
const data = [
194+
'Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green',
195+
'Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue',
196+
'Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red',
197+
'Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red',
198+
'Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green'
199+
]
200+
const result = [
201+
{ r: 4, g: 2, b: 6 },
202+
{ r: 1, g: 3, b: 4 },
203+
{ r: 20, g: 13, b: 6 },
204+
{ r: 14, g: 3, b: 15 },
205+
{ r: 6, g: 3, b: 2 }
206+
]
207+
data.forEach((game, idx) => {
208+
expect(countCubesNeeded(parseGame(game))).to.deep.equal(result[idx])
209+
})
210+
})
211+
})
212+
describe('power', () => {
213+
it('calculates the power for a game', () => {
214+
const data = [
215+
'Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green',
216+
'Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue',
217+
'Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red',
218+
'Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red',
219+
'Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green'
220+
]
221+
const result = [48, 12, 1560, 630, 36]
222+
data.forEach((game, idx) => {
223+
expect(power(parseGame(game))).to.equal(result[idx])
224+
})
225+
})
226+
})
227+
})
189228
})

2023/day-02/solution.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
44
const { linesToArray } = require('../../2018/inputParser')
5-
const { parseGame, checksumGameSet } = require('./game')
5+
const { parseGame, checksumGameSet, power } = require('./game')
66

77
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
88
if (err) throw err
@@ -27,8 +27,11 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
2727

2828
const part2 = () => {
2929
const data = resetInput()
30-
console.debug(data)
31-
return 'No answer yet'
30+
.map(parseGame)
31+
32+
return data.reduce((acc, game) => {
33+
return acc + power(game)
34+
}, 0)
3235
}
3336
const answers = []
3437
answers.push(part1())

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