Skip to content

Commit

Permalink
feat(2023-02): parse game draw data
Browse files Browse the repository at this point in the history
  • Loading branch information
amclin committed Dec 15, 2023
1 parent be075d5 commit e17ca18
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 2023/day-02/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const parseGame = (gameString) => {
const data = gameString.split(':')
const id = parseInt(
data[0].match(/\d+/)[0] // find the game number
)
const draws = data[1].split(';') // split the game into draws
.map((draw) => {
const result = ['red', 'green', 'blue']
.map((color) => { // extract count for each color
const reg = new RegExp(`\\d+(?= ${color})`)
console.debug(reg)
const val = draw.match(reg) || [0]
console.debug(`${color} ${val}`)
return parseInt(val).toString(16).padStart(2, '0') // convert to hex
})

return result.join('') // combine into a RGB hex color string
})

return {
id,
draws
}
}

module.exports = {
parseGame
}
60 changes: 60 additions & 0 deletions 2023/day-02/game.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { parseGame } = require('./game')

describe('--- Day 2: Cube Conundrum ---', () => {
describe('Part 1', () => {
describe('parseGame', () => {
it('extracts a game string into a data object with RGB hex values for draws', () => {
const data = [
'Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green',
'Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue',
'Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red',
'Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red',
'Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green'
]
const result = [
{
id: 1,
draws: [
'040003',
'010206',
'000200'
]
}, {
id: 2,
draws: [
'000201',
'010304',
'000101'
]
}, {
id: 3,
draws: [
'140806',
'040d05',
'010500'
]
}, {
id: 4,
draws: [
'030106',
'060300',
'0e030f'
]
}, {
id: 5,
draws: [
'060301',
'010202'
]
}
]

data.forEach((game, idx) => {
expect(parseGame(game)).to.deep.equal(result[idx])
})
})
})
})
})

0 comments on commit e17ca18

Please sign in to comment.