Skip to content

Commit 97bb107

Browse files
Merge pull request #203 from amclin/feat/2021-day-05
Feat/2021 day 05
2 parents a6d6fc1 + 9421e73 commit 97bb107

File tree

11 files changed

+1637
-1
lines changed

11 files changed

+1637
-1
lines changed

2021/day-04/bingo.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const markBoard = (board, called) => {
2+
for (let x = 0; x < 5; x++) {
3+
for (let y = 0; y < 5; y++) {
4+
if (board[x][y] === called) {
5+
board[x][y] = 'x'
6+
// TODO: speed up break the loop, since only one of a number on each board
7+
// x = 6
8+
// y = 6
9+
}
10+
}
11+
}
12+
return board
13+
}
14+
15+
const checkWinner = (board) => {
16+
// TODO: This can be sped up by doing a check for at least 5 "x" before
17+
// validating horizontal/vertical explicitly. Another speedup would be to
18+
// zig-zag check parse through the array and break/resolve when there
19+
// isn't a match instead of checking all columns then checking all rows
20+
21+
// Look for a horizontal bingo
22+
for (let y = 0; y < 5; y++) {
23+
if (board[y].filter((val) => val === 'x').length === 5) {
24+
return 'winner'
25+
}
26+
}
27+
28+
// Look for a vertical bingo
29+
let match = 0
30+
for (let x = 0; x < 5; x++) {
31+
for (let y = 0; y < 5; y++) {
32+
if (board[y][x] === 'x') {
33+
match++
34+
35+
if (match === 5) {
36+
return 'winner'
37+
}
38+
}
39+
}
40+
match = 0 // reset so next row has a clean count
41+
}
42+
43+
// No bingo
44+
return 'no win'
45+
}
46+
47+
const scoreBoard = (board) => {
48+
return board.reduce((tally, row) => {
49+
tally += row.reduce((colTally, cell) => {
50+
if (cell !== 'x') {
51+
colTally += cell
52+
}
53+
return colTally
54+
}, 0)
55+
return tally
56+
}, 0)
57+
}
58+
59+
module.exports = {
60+
scoreBoard,
61+
checkWinner,
62+
markBoard
63+
}

2021/day-04/bingo.test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { scoreBoard, checkWinner, markBoard } = require('./bingo')
4+
const { parseData, linesToArray } = require('../../2018/inputParser')
5+
6+
const testData = `
7+
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
8+
9+
22 13 17 11 0
10+
8 2 23 4 24
11+
21 9 14 16 7
12+
6 10 3 18 5
13+
1 12 20 15 19
14+
15+
3 15 0 2 22
16+
9 18 13 17 5
17+
19 8 7 25 23
18+
20 11 10 24 4
19+
14 21 16 12 6
20+
21+
14 21 17 24 4
22+
10 16 15 9 19
23+
18 8 23 26 20
24+
22 11 13 6 5
25+
2 0 12 3 7
26+
`
27+
// Deep copy to ensure we aren't mutating the original data
28+
const data = JSON.parse(JSON.stringify(linesToArray(testData)))
29+
30+
// split up data
31+
const testDraws = parseData(data.shift())
32+
console.debug(testDraws)
33+
const testBoards = []
34+
for (let x = 0; x < data.length; x = x + 5) {
35+
testBoards.push(
36+
data.slice(x, x + 5).map(parseData)
37+
)
38+
}
39+
40+
describe('--- Day 4: Giant Squid ---', () => {
41+
describe('Part 1', () => {
42+
describe('markBoard()', () => {
43+
it('checks a board for a match and marks it', () => {
44+
const board = [
45+
[1, 2, 3, 4, 5],
46+
[9, 8, 7, 6, 5],
47+
['x', 'x', 'x', 'x', 'x'],
48+
[3, 6, 9, 1, 0],
49+
[1, 3, 5, 7, 9]
50+
]
51+
const expected = [
52+
[1, 2, 3, 4, 'x'],
53+
[9, 8, 7, 6, 'x'],
54+
['x', 'x', 'x', 'x', 'x'],
55+
[3, 6, 9, 1, 0],
56+
[1, 3, 'x', 7, 9]
57+
]
58+
expect(markBoard(board, 5)).to.deep.equal(expected)
59+
})
60+
})
61+
describe('checkWinner()', () => {
62+
it('checks to see if a board has a horizontal bingo', () => {
63+
const board = [
64+
[1, 2, 3, 4, 5],
65+
[9, 8, 7, 6, 5],
66+
['x', 'x', 'x', 'x', 'x'],
67+
[3, 6, 9, 1, 0],
68+
[1, 3, 5, 7, 9]
69+
]
70+
expect(checkWinner(board)).to.equal('winner')
71+
})
72+
it('checks to see if a board has a vertical bingo', () => {
73+
const board = [
74+
[1, 2, 3, 'x', 5],
75+
[9, 8, 7, 'x', 5],
76+
[2, 4, 6, 'x', 8],
77+
[3, 6, 9, 'x', 0],
78+
[1, 3, 5, 'x', 7]
79+
]
80+
expect(checkWinner(board)).to.equal('winner')
81+
})
82+
it('identifies a board is not yet a winner', () => {
83+
const board = [
84+
[1, 'x', 3, 4, 5],
85+
[9, 8, 7, 'x', 5],
86+
['x', 'x', 3, 7, 11],
87+
[3, 6, 9, 'x', 'x'],
88+
[1, 3, 5, 7, 'x']
89+
]
90+
expect(checkWinner(board)).to.equal('no win')
91+
})
92+
})
93+
describe('scoreBoard()', () => {
94+
it('gets the sum of the unmarked squares on the board', () => {
95+
const board = [
96+
['x', 'x', 'x', 'x', 'x'],
97+
[10, 16, 15, 'x', 19],
98+
[18, 8, 'x', 26, 20],
99+
[22, 'x', 13, 6, 'x'],
100+
['x', 'x', 12, 3, 'x']
101+
]
102+
expect(scoreBoard(board)).to.equal(188)
103+
})
104+
})
105+
})
106+
})

2021/day-04/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const console = require('../helpers')
3+
require('./solution')

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