Skip to content

Commit 5ea8fbb

Browse files
feat(2021-day-04): find the last board that will score a bingo
1 parent 7f8a13f commit 5ea8fbb

File tree

3 files changed

+35
-47
lines changed

3 files changed

+35
-47
lines changed

2021/day-04/bingo.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
const boards = []
2-
3-
const callNumber = (called) => {
4-
for (let x = 0; x < boards.length; x++) {
5-
markBoard(x, called)
6-
if (checkWinner(x) === 'winner') {
7-
console.debug(`Board ${x} is the winner`)
8-
return x
9-
}
10-
}
11-
}
12-
131
const markBoard = (board, called) => {
142
for (let x = 0; x < 5; x++) {
153
for (let y = 0; y < 5; y++) {
@@ -69,7 +57,6 @@ const scoreBoard = (board) => {
6957
}
7058

7159
module.exports = {
72-
callNumber,
7360
scoreBoard,
7461
checkWinner,
7562
markBoard

2021/day-04/bingo.test.js

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { callNumber, scoreBoard, checkWinner, markBoard } = require('./bingo')
4-
const { parseData } = require('../../2018/inputParser')
3+
const { scoreBoard, checkWinner, markBoard } = require('./bingo')
4+
const { parseData, linesToArray } = require('../../2018/inputParser')
55

66
const testData = `
77
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
@@ -25,10 +25,11 @@ const testData = `
2525
2 0 12 3 7
2626
`
2727
// Deep copy to ensure we aren't mutating the original data
28-
const data = JSON.parse(JSON.stringify(testData))
28+
const data = JSON.parse(JSON.stringify(linesToArray(testData)))
2929

3030
// split up data
3131
const testDraws = parseData(data.shift())
32+
console.debug(testDraws)
3233
const testBoards = []
3334
for (let x = 0; x < data.length; x = x + 5) {
3435
testBoards.push(
@@ -56,34 +57,6 @@ describe('--- Day 4: Giant Squid ---', () => {
5657
]
5758
expect(markBoard(board, 5)).to.deep.equal(expected)
5859
})
59-
it.skip('can be used in a loop to find', () => {
60-
// callNumber(7)
61-
// callNumber(4)
62-
// callNumber(9)
63-
// callNumber(5)
64-
// callNumber(11)
65-
// for(var x = 0; x < testBoards)
66-
// expect(boards[0]).to.deep.equal(board0)
67-
// expect(boards[1]).to.deep.equal(board1)
68-
// expect(boards[2]).to.deep.equal(board2)
69-
// callNumber(17)
70-
// callNumber(23)
71-
// callNumber(2)
72-
// callNumber(0)
73-
// callNumber(14)
74-
// callNumber(21)
75-
// expect(boards[0]).to.deep.equal(board0)
76-
// expect(boards[1]).to.deep.equal(board1)
77-
// expect(boards[2]).to.deep.equal(board2)
78-
})
79-
it.skip('identifies the winner', () => {
80-
expect(callNumber(24)).to.equal(3)
81-
})
82-
})
83-
describe('findWinner()', () => {
84-
it.skip('loops through the boards and checks for a winner', () => {
85-
86-
})
8760
})
8861
describe('checkWinner()', () => {
8962
it('checks to see if a board has a horizontal bingo', () => {

2021/day-04/solution.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,37 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
5959
}
6060

6161
const part2 = () => {
62-
// const data = resetInput()
63-
// console.debug(data)
64-
return 'No answer yet'
62+
const data = resetInput()
63+
64+
let draw = -1
65+
let lastWin = []
66+
let lastDraw = 0
67+
68+
while (data.boards.length >= 1 && draw < data.draws.length) {
69+
// next draw
70+
draw++
71+
72+
// Mark each board that has the number
73+
console.debug(`Checking draw ${data.draws[draw]}`)
74+
data.boards = data.boards.map((board) => {
75+
return markBoard(board, data.draws[draw])
76+
})
77+
78+
// Filter out any winners
79+
data.boards = data.boards.filter((board) => {
80+
if (checkWinner(board) === 'winner') {
81+
lastWin = board
82+
lastDraw = data.draws[draw]
83+
return false
84+
} else {
85+
return true
86+
}
87+
})
88+
}
89+
90+
// last winner found
91+
console.debug(`Score is ${scoreBoard(lastWin)} on draw ${lastDraw}`)
92+
return scoreBoard(lastWin) * lastDraw
6593
}
6694
const answers = []
6795
answers.push(part1())

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