Skip to content

Commit 5416c66

Browse files
fix(2023-02): validate games correctly
cubes stay out of the bag after each draw
1 parent 97069a3 commit 5416c66

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

2023/day-02/game.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,34 @@ const parseHex = (hex) => {
3131
}
3232
}
3333

34-
const validateGame = (game, limit) => {
34+
const validateDraw = (draw, limit) => {
35+
const data = parseHex(draw)
3536
const lim = parseHex(limit)
37+
return (data.r <= lim.r && data.g <= lim.g && data.b <= lim.b)
38+
}
39+
40+
const validateGame = (game, limit) => {
41+
// const lim = parseHex(limit)
42+
// const tally = game.draws.reduce((acc, draw) => {
43+
// const drawData = parseHex(draw)
44+
// return {
45+
// r: acc.r + drawData.r,
46+
// g: acc.g + drawData.g,
47+
// b: acc.b + drawData.b
48+
// }
49+
// }, { r: 0, g: 0, b: 0 })
3650

37-
const tally = game.draws.reduce((acc, draw) => {
38-
const drawData = parseHex(draw)
39-
return {
40-
r: acc.r + drawData.r,
41-
g: acc.g + drawData.g,
42-
b: acc.b + drawData.b
43-
}
44-
}, { r: 0, g: 0, b: 0 })
51+
// const result = (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
52+
// console.debug(`Game ${game.id} ${(result) ? 'passes' : 'fails'}`)
53+
// if (!result) {
54+
// console.debug(tally)
55+
// }
4556

46-
return (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
57+
// If any draw fails, the full game fails
58+
const result = game.draws.reduce((res, draw) => {
59+
return (res && validateDraw(draw, limit))
60+
}, true)
61+
return result
4762
}
4863

4964
const checksumGameSet = (games, limit) => {
@@ -56,5 +71,6 @@ const checksumGameSet = (games, limit) => {
5671
module.exports = {
5772
parseGame,
5873
validateGame,
59-
checksumGameSet
74+
checksumGameSet,
75+
validateDraw
6076
}

2023/day-02/game.test.js

Lines changed: 16 additions & 5 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 } = require('./game')
3+
const { parseGame, validateGame, checksumGameSet, validateDraw } = require('./game')
44
const { linesToArray } = require('../../2018/inputParser')
55
const fs = require('fs')
66
const path = require('path')
@@ -153,6 +153,17 @@ describe('--- Day 2: Cube Conundrum ---', () => {
153153
})
154154
})
155155

156+
describe('validateDraw', () => {
157+
it('validates an individual draw is within limits', () => {
158+
const limit = '0c0d0e'
159+
expect(validateDraw('010206', limit)).to.equal(true)
160+
expect(validateDraw('060301', limit)).to.equal(true)
161+
expect(validateDraw('040d05', limit)).to.equal(true)
162+
expect(validateDraw('140806', limit)).to.equal(false) // game 3 draw 1 has 20 reds
163+
expect(validateDraw('0e030f', limit)).to.equal(false) // game 4 draw 3 has 15 blues
164+
})
165+
})
166+
156167
describe('integration test', () => {
157168
let initData
158169
before((done) => {
@@ -165,13 +176,13 @@ describe('--- Day 2: Cube Conundrum ---', () => {
165176
})
166177
})
167178

168-
it('result is larger than 1452', () => {
179+
it('result matches what we know about the answer', () => {
169180
const limit = [12, 13, 14] // 12 red, 13 green, 14 blue
170-
.map((num) => parseInt(num, 16))
181+
.map((num) => num.toString(16).padStart(2, '0'))
171182
.join('')
172183

173-
// Solution set for
174-
expect(checksumGameSet(initData, limit)).to.be.gt(1452)
184+
expect(checksumGameSet(initData, limit)).to.be.gt(177) // 177 is too low
185+
expect(checksumGameSet(initData, limit)).to.be.gt(1452) // 1452 (from creating the limit in hex wrong, and assuming cubes are not returned to the bag after each draw) is too low
175186
})
176187
})
177188
})

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