Skip to content

Commit

Permalink
fix(2023-02): validate games correctly
Browse files Browse the repository at this point in the history
cubes stay out of the bag after each draw
  • Loading branch information
amclin committed Dec 15, 2023
1 parent 97069a3 commit 5416c66
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
38 changes: 27 additions & 11 deletions 2023/day-02/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,34 @@ const parseHex = (hex) => {
}
}

const validateGame = (game, limit) => {
const validateDraw = (draw, limit) => {
const data = parseHex(draw)
const lim = parseHex(limit)
return (data.r <= lim.r && data.g <= lim.g && data.b <= lim.b)
}

const validateGame = (game, limit) => {
// const lim = parseHex(limit)
// const tally = game.draws.reduce((acc, draw) => {
// const drawData = parseHex(draw)
// return {
// r: acc.r + drawData.r,
// g: acc.g + drawData.g,
// b: acc.b + drawData.b
// }
// }, { r: 0, g: 0, b: 0 })

const tally = game.draws.reduce((acc, draw) => {
const drawData = parseHex(draw)
return {
r: acc.r + drawData.r,
g: acc.g + drawData.g,
b: acc.b + drawData.b
}
}, { r: 0, g: 0, b: 0 })
// const result = (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
// console.debug(`Game ${game.id} ${(result) ? 'passes' : 'fails'}`)
// if (!result) {
// console.debug(tally)
// }

return (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
// If any draw fails, the full game fails
const result = game.draws.reduce((res, draw) => {
return (res && validateDraw(draw, limit))
}, true)
return result
}

const checksumGameSet = (games, limit) => {
Expand All @@ -56,5 +71,6 @@ const checksumGameSet = (games, limit) => {
module.exports = {
parseGame,
validateGame,
checksumGameSet
checksumGameSet,
validateDraw
}
21 changes: 16 additions & 5 deletions 2023/day-02/game.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { parseGame, validateGame, checksumGameSet } = require('./game')
const { parseGame, validateGame, checksumGameSet, validateDraw } = require('./game')
const { linesToArray } = require('../../2018/inputParser')
const fs = require('fs')
const path = require('path')
Expand Down Expand Up @@ -153,6 +153,17 @@ describe('--- Day 2: Cube Conundrum ---', () => {
})
})

describe('validateDraw', () => {
it('validates an individual draw is within limits', () => {
const limit = '0c0d0e'
expect(validateDraw('010206', limit)).to.equal(true)
expect(validateDraw('060301', limit)).to.equal(true)
expect(validateDraw('040d05', limit)).to.equal(true)
expect(validateDraw('140806', limit)).to.equal(false) // game 3 draw 1 has 20 reds
expect(validateDraw('0e030f', limit)).to.equal(false) // game 4 draw 3 has 15 blues
})
})

describe('integration test', () => {
let initData
before((done) => {
Expand All @@ -165,13 +176,13 @@ describe('--- Day 2: Cube Conundrum ---', () => {
})
})

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

// Solution set for
expect(checksumGameSet(initData, limit)).to.be.gt(1452)
expect(checksumGameSet(initData, limit)).to.be.gt(177) // 177 is too low
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
})
})
})
Expand Down

0 comments on commit 5416c66

Please sign in to comment.