2021-12-04 15:14:16 +01:00
|
|
|
# Copyright 2021 Christian Ulrich
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# usage: ./part1 < input
|
|
|
|
|
|
|
|
import sequtils
|
|
|
|
import strutils
|
|
|
|
|
|
|
|
type
|
2021-12-04 15:42:45 +01:00
|
|
|
Board = array[5, array[5, int8]]
|
2021-12-04 15:14:16 +01:00
|
|
|
|
2021-12-04 15:42:45 +01:00
|
|
|
iterator lines(board: Board): array[5, int8] =
|
2021-12-04 15:14:16 +01:00
|
|
|
for i in 0 .. high(Board):
|
|
|
|
yield board[i]
|
|
|
|
|
2021-12-04 15:42:45 +01:00
|
|
|
iterator columns(board: Board): array[5, int8] =
|
2021-12-04 15:14:16 +01:00
|
|
|
for i in 0 .. high(board[0]):
|
2021-12-04 15:42:45 +01:00
|
|
|
var column: array[5, int8]
|
2021-12-04 15:14:16 +01:00
|
|
|
for j in 0 .. high(Board):
|
|
|
|
column[j] = board[j][i]
|
|
|
|
yield column
|
|
|
|
|
2021-12-04 15:42:45 +01:00
|
|
|
proc check(board: var Board, val: int8) =
|
2021-12-04 15:14:16 +01:00
|
|
|
for i in 0 .. high(Board):
|
|
|
|
for cellIndex, cell in board[i].pairs():
|
2021-12-04 15:42:45 +01:00
|
|
|
if cell == val:
|
|
|
|
board[i][cellIndex] = -1
|
2021-12-04 15:14:16 +01:00
|
|
|
|
2021-12-04 15:42:45 +01:00
|
|
|
proc readDrawnNumbers(): seq[int8] =
|
|
|
|
result = readLine(stdin).split(',').mapIt(int8(parseUint(it)))
|
2021-12-04 15:14:16 +01:00
|
|
|
|
|
|
|
proc readBoards(): seq[Board] =
|
|
|
|
var
|
|
|
|
line: string
|
|
|
|
board: Board
|
|
|
|
while true:
|
|
|
|
if not readLine(stdin, line):
|
|
|
|
return
|
|
|
|
for i in 0 .. high(Board):
|
|
|
|
if not readLine(stdin, line):
|
|
|
|
return
|
|
|
|
board[i][0 .. high(Board)] = line.splitWhitespace(4)
|
2021-12-04 15:42:45 +01:00
|
|
|
.mapIt(int8(parseUint(it)))
|
2021-12-04 15:14:16 +01:00
|
|
|
result.add(board)
|
|
|
|
|
2021-12-04 15:42:45 +01:00
|
|
|
proc bingoScore(board: Board, latestNumber: int8): int =
|
2021-12-04 15:14:16 +01:00
|
|
|
var unmarkedSum = 0
|
|
|
|
for line in board.lines():
|
2021-12-04 15:42:45 +01:00
|
|
|
unmarkedSum.inc(line.foldl(if b >= 0: a + int(b) else: a, 0))
|
2021-12-04 15:14:16 +01:00
|
|
|
result = unmarkedSum * int(latestNumber)
|
|
|
|
|
|
|
|
proc bingo(): int =
|
|
|
|
let drawnNumbers = readDrawnNumbers()
|
|
|
|
var boards = readBoards()
|
|
|
|
for number in drawnNumbers:
|
|
|
|
for board in boards.mitems():
|
|
|
|
board.check(number)
|
|
|
|
for line in board.lines():
|
2021-12-04 15:42:45 +01:00
|
|
|
if line.allIt(it < 0):
|
2021-12-04 15:14:16 +01:00
|
|
|
return board.bingoScore(number)
|
|
|
|
for column in board.columns():
|
2021-12-04 15:42:45 +01:00
|
|
|
if column.allIt(it < 0):
|
2021-12-04 15:14:16 +01:00
|
|
|
return board.bingoScore(number)
|
2021-12-04 15:16:50 +01:00
|
|
|
raise newException(ValueError, "there is no winner")
|
2021-12-04 15:14:16 +01:00
|
|
|
|
|
|
|
proc main(): int =
|
|
|
|
try:
|
|
|
|
echo bingo()
|
|
|
|
except Exception as error:
|
|
|
|
echo error.msg
|
|
|
|
result = -1
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
quit(main())
|