Skip to content

Commit

Permalink
feat(2021-day-04): find the last board that will score a bingo
Browse files Browse the repository at this point in the history
  • Loading branch information
amclin committed Dec 14, 2021
1 parent 7f8a13f commit 5ea8fbb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
13 changes: 0 additions & 13 deletions 2021/day-04/bingo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
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++) {
Expand Down Expand Up @@ -69,7 +57,6 @@ const scoreBoard = (board) => {
}

module.exports = {
callNumber,
scoreBoard,
checkWinner,
markBoard
Expand Down
35 changes: 4 additions & 31 deletions 2021/day-04/bingo.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { callNumber, scoreBoard, checkWinner, markBoard } = require('./bingo')
const { parseData } = require('../../2018/inputParser')
const { scoreBoard, checkWinner, markBoard } = require('./bingo')
const { parseData, linesToArray } = 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
Expand All @@ -25,10 +25,11 @@ const testData = `
2 0 12 3 7
`
// Deep copy to ensure we aren't mutating the original data
const data = JSON.parse(JSON.stringify(testData))
const data = JSON.parse(JSON.stringify(linesToArray(testData)))

// split up data
const testDraws = parseData(data.shift())
console.debug(testDraws)
const testBoards = []
for (let x = 0; x < data.length; x = x + 5) {
testBoards.push(
Expand Down Expand Up @@ -56,34 +57,6 @@ describe('--- Day 4: Giant Squid ---', () => {
]
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', () => {
Expand Down
34 changes: 31 additions & 3 deletions 2021/day-04/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,37 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
}

const part2 = () => {
// const data = resetInput()
// console.debug(data)
return 'No answer yet'
const data = resetInput()

let draw = -1
let lastWin = []
let lastDraw = 0

while (data.boards.length >= 1 && draw < data.draws.length) {
// next draw
draw++

// Mark each board that has the number
console.debug(`Checking draw ${data.draws[draw]}`)
data.boards = data.boards.map((board) => {
return markBoard(board, data.draws[draw])
})

// Filter out any winners
data.boards = data.boards.filter((board) => {
if (checkWinner(board) === 'winner') {
lastWin = board
lastDraw = data.draws[draw]
return false
} else {
return true
}
})
}

// last winner found
console.debug(`Score is ${scoreBoard(lastWin)} on draw ${lastDraw}`)
return scoreBoard(lastWin) * lastDraw
}
const answers = []
answers.push(part1())
Expand Down

0 comments on commit 5ea8fbb

Please sign in to comment.