-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |