-
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.
feat(2021-day-04): find the first board with a bingo
- Loading branch information
Showing
6 changed files
with
888 additions
and
1 deletion.
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,76 @@ | ||
const boards = [] | ||
|
||
const callNumber = (called) => { | ||
for (let x = 0; x < boards.length; x++) { | ||
markBoard(x, called) | ||
if (checkWinner(x) === 'winner') { | ||
console.debug(`Board ${x} is the winner`) | ||
return x | ||
} | ||
} | ||
} | ||
|
||
const markBoard = (board, called) => { | ||
for (let x = 0; x < 5; x++) { | ||
for (let y = 0; y < 5; y++) { | ||
if (board[x][y] === called) { | ||
board[x][y] = 'x' | ||
// TODO: speed up break the loop, since only one of a number on each board | ||
// x = 6 | ||
// y = 6 | ||
} | ||
} | ||
} | ||
return board | ||
} | ||
|
||
const checkWinner = (board) => { | ||
// TODO: This can be sped up by doing a check for at least 5 "x" before | ||
// validating horizontal/vertical explicitly. Another speedup would be to | ||
// zig-zag check parse through the array and break/resolve when there | ||
// isn't a match instead of checking all columns then checking all rows | ||
|
||
// Look for a horizontal bingo | ||
for (let y = 0; y < 5; y++) { | ||
if (board[y].filter((val) => val === 'x').length === 5) { | ||
return 'winner' | ||
} | ||
} | ||
|
||
// Look for a vertical bingo | ||
let match = 0 | ||
for (let x = 0; x < 5; x++) { | ||
for (let y = 0; y < 5; y++) { | ||
if (board[y][x] === 'x') { | ||
match++ | ||
|
||
if (match === 5) { | ||
return 'winner' | ||
} | ||
} | ||
} | ||
match = 0 // reset so next row has a clean count | ||
} | ||
|
||
// No bingo | ||
return 'no win' | ||
} | ||
|
||
const scoreBoard = (board) => { | ||
return board.reduce((tally, row) => { | ||
tally += row.reduce((colTally, cell) => { | ||
if (cell !== 'x') { | ||
colTally += cell | ||
} | ||
return colTally | ||
}, 0) | ||
return tally | ||
}, 0) | ||
} | ||
|
||
module.exports = { | ||
callNumber, | ||
scoreBoard, | ||
checkWinner, | ||
markBoard | ||
} |
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,133 @@ | ||
/* eslint-env mocha */ | ||
const { expect } = require('chai') | ||
const { callNumber, scoreBoard, checkWinner, markBoard } = require('./bingo') | ||
const { parseData } = require('../../2018/inputParser') | ||
|
||
const testData = ` | ||
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 | ||
22 13 17 11 0 | ||
8 2 23 4 24 | ||
21 9 14 16 7 | ||
6 10 3 18 5 | ||
1 12 20 15 19 | ||
3 15 0 2 22 | ||
9 18 13 17 5 | ||
19 8 7 25 23 | ||
20 11 10 24 4 | ||
14 21 16 12 6 | ||
14 21 17 24 4 | ||
10 16 15 9 19 | ||
18 8 23 26 20 | ||
22 11 13 6 5 | ||
2 0 12 3 7 | ||
` | ||
// Deep copy to ensure we aren't mutating the original data | ||
const data = JSON.parse(JSON.stringify(testData)) | ||
|
||
// split up data | ||
const testDraws = parseData(data.shift()) | ||
const testBoards = [] | ||
for (let x = 0; x < data.length; x = x + 5) { | ||
testBoards.push( | ||
data.slice(x, x + 5).map(parseData) | ||
) | ||
} | ||
|
||
describe('--- Day 4: Giant Squid ---', () => { | ||
describe('Part 1', () => { | ||
describe('markBoard()', () => { | ||
it('checks a board for a match and marks it', () => { | ||
const board = [ | ||
[1, 2, 3, 4, 5], | ||
[9, 8, 7, 6, 5], | ||
['x', 'x', 'x', 'x', 'x'], | ||
[3, 6, 9, 1, 0], | ||
[1, 3, 5, 7, 9] | ||
] | ||
const expected = [ | ||
[1, 2, 3, 4, 'x'], | ||
[9, 8, 7, 6, 'x'], | ||
['x', 'x', 'x', 'x', 'x'], | ||
[3, 6, 9, 1, 0], | ||
[1, 3, 'x', 7, 9] | ||
] | ||
expect(markBoard(board, 5)).to.deep.equal(expected) | ||
}) | ||
it.skip('can be used in a loop to find', () => { | ||
// callNumber(7) | ||
// callNumber(4) | ||
// callNumber(9) | ||
// callNumber(5) | ||
// callNumber(11) | ||
// for(var x = 0; x < testBoards) | ||
// expect(boards[0]).to.deep.equal(board0) | ||
// expect(boards[1]).to.deep.equal(board1) | ||
// expect(boards[2]).to.deep.equal(board2) | ||
// callNumber(17) | ||
// callNumber(23) | ||
// callNumber(2) | ||
// callNumber(0) | ||
// callNumber(14) | ||
// callNumber(21) | ||
// expect(boards[0]).to.deep.equal(board0) | ||
// expect(boards[1]).to.deep.equal(board1) | ||
// expect(boards[2]).to.deep.equal(board2) | ||
}) | ||
it.skip('identifies the winner', () => { | ||
expect(callNumber(24)).to.equal(3) | ||
}) | ||
}) | ||
describe('findWinner()', () => { | ||
it.skip('loops through the boards and checks for a winner', () => { | ||
|
||
}) | ||
}) | ||
describe('checkWinner()', () => { | ||
it('checks to see if a board has a horizontal bingo', () => { | ||
const board = [ | ||
[1, 2, 3, 4, 5], | ||
[9, 8, 7, 6, 5], | ||
['x', 'x', 'x', 'x', 'x'], | ||
[3, 6, 9, 1, 0], | ||
[1, 3, 5, 7, 9] | ||
] | ||
expect(checkWinner(board)).to.equal('winner') | ||
}) | ||
it('checks to see if a board has a vertical bingo', () => { | ||
const board = [ | ||
[1, 2, 3, 'x', 5], | ||
[9, 8, 7, 'x', 5], | ||
[2, 4, 6, 'x', 8], | ||
[3, 6, 9, 'x', 0], | ||
[1, 3, 5, 'x', 7] | ||
] | ||
expect(checkWinner(board)).to.equal('winner') | ||
}) | ||
it('identifies a board is not yet a winner', () => { | ||
const board = [ | ||
[1, 'x', 3, 4, 5], | ||
[9, 8, 7, 'x', 5], | ||
['x', 'x', 3, 7, 11], | ||
[3, 6, 9, 'x', 'x'], | ||
[1, 3, 5, 7, 'x'] | ||
] | ||
expect(checkWinner(board)).to.equal('no win') | ||
}) | ||
}) | ||
describe('scoreBoard()', () => { | ||
it('gets the sum of the unmarked squares on the board', () => { | ||
const board = [ | ||
['x', 'x', 'x', 'x', 'x'], | ||
[10, 16, 15, 'x', 19], | ||
[18, 8, 'x', 26, 20], | ||
[22, 'x', 13, 6, 'x'], | ||
['x', 'x', 12, 3, 'x'] | ||
] | ||
expect(scoreBoard(board)).to.equal(188) | ||
}) | ||
}) | ||
}) | ||
}) |
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,3 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const console = require('../helpers') | ||
require('./solution') |
Oops, something went wrong.