|
| 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 | +}) |
0 commit comments