Skip to content

Commit e17ca18

Browse files
feat(2023-02): parse game draw data
1 parent be075d5 commit e17ca18

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

2023/day-02/game.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const parseGame = (gameString) => {
2+
const data = gameString.split(':')
3+
const id = parseInt(
4+
data[0].match(/\d+/)[0] // find the game number
5+
)
6+
const draws = data[1].split(';') // split the game into draws
7+
.map((draw) => {
8+
const result = ['red', 'green', 'blue']
9+
.map((color) => { // extract count for each color
10+
const reg = new RegExp(`\\d+(?= ${color})`)
11+
console.debug(reg)
12+
const val = draw.match(reg) || [0]
13+
console.debug(`${color} ${val}`)
14+
return parseInt(val).toString(16).padStart(2, '0') // convert to hex
15+
})
16+
17+
return result.join('') // combine into a RGB hex color string
18+
})
19+
20+
return {
21+
id,
22+
draws
23+
}
24+
}
25+
26+
module.exports = {
27+
parseGame
28+
}

2023/day-02/game.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { parseGame } = require('./game')
4+
5+
describe('--- Day 2: Cube Conundrum ---', () => {
6+
describe('Part 1', () => {
7+
describe('parseGame', () => {
8+
it('extracts a game string into a data object with RGB hex values for draws', () => {
9+
const data = [
10+
'Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green',
11+
'Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue',
12+
'Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red',
13+
'Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red',
14+
'Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green'
15+
]
16+
const result = [
17+
{
18+
id: 1,
19+
draws: [
20+
'040003',
21+
'010206',
22+
'000200'
23+
]
24+
}, {
25+
id: 2,
26+
draws: [
27+
'000201',
28+
'010304',
29+
'000101'
30+
]
31+
}, {
32+
id: 3,
33+
draws: [
34+
'140806',
35+
'040d05',
36+
'010500'
37+
]
38+
}, {
39+
id: 4,
40+
draws: [
41+
'030106',
42+
'060300',
43+
'0e030f'
44+
]
45+
}, {
46+
id: 5,
47+
draws: [
48+
'060301',
49+
'010202'
50+
]
51+
}
52+
]
53+
54+
data.forEach((game, idx) => {
55+
expect(parseGame(game)).to.deep.equal(result[idx])
56+
})
57+
})
58+
})
59+
})
60+
})

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