This commit is contained in:
Daniel Flanagan 2021-12-05 08:04:39 -06:00
parent 96597c15c8
commit fe68c24b57
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -1,11 +1,61 @@
import std/[strutils, sequtils, options, math, parseutils] import std/[strutils, sequtils, options, math, parseutils, sets]
import ./common import ./common
type Board = object
entries: seq[string]
checked: seq[int]
let winningSets = [
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[0, 5, 10, 15, 20],
[1, 6, 11, 16, 21],
[2, 7, 12, 17, 22],
[3, 8, 13, 18, 23],
[4, 9, 14, 19, 24]
].mapIt(it.toHashSet())
proc isBoardWinning(b: ref Board): bool =
let s = b.checked.toHashSet()
for w in winningSets:
if w < s:
return true
return false
proc squidBingo(inputLines: seq[string]): int = proc squidBingo(inputLines: seq[string]): int =
var input: seq[string] = inputLines var input: seq[string] = inputLines
let calledNumbers: seq[string] = input[0].split(',') let calledNumbers: seq[string] = input[0].split ','
input.delete(0..1) input.delete 0..1
echo calledNumbers echo calledNumbers
# construct boards
var boards: seq[Board]
var board: Board = Board(entries: @[], checked: @[])
for l in input:
echo l
if l.strip() == "":
boards.add board
echo board.entries
board = Board(entries: @[], checked: @[])
continue
board.entries.add [l[0..1], l[3..4], l[6..7], l[9..10], l[12..13]].mapIt(it.strip())
for n in calledNumbers:
for b in boards:
for i, e in b.entries:
if n == e:
# TODO: why can't I add here but I can add on 46?!
b.checked.add i
if b.isBoardWinning():
# TODO: get unchecked entries
# TODO: multiple their sum with last .checked
return 9
return 0 return 0
let input = 4.loadInput() let input = 4.loadInput()
@ -13,25 +63,23 @@ time("squidBingo part 1"): echo input.squidBingo()
when not defined(release): when not defined(release):
static: static:
let testInput = """ let testInput = """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
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 22 13 17 11 0
8 2 23 4 24 8 2 23 4 24
21 9 14 16 7 21 9 14 16 7
6 10 3 18 5 6 10 3 18 5
1 12 20 15 19 1 12 20 15 19
3 15 0 2 22 3 15 0 2 22
9 18 13 17 5 9 18 13 17 5
19 8 7 25 23 19 8 7 25 23
20 11 10 24 4 20 11 10 24 4
14 21 16 12 6 14 21 16 12 6
14 21 17 24 4 14 21 17 24 4
10 16 15 9 19 10 16 15 9 19
18 8 23 26 20 18 8 23 26 20
22 11 13 6 5 22 11 13 6 5
2 0 12 3 7 2 0 12 3 7""".split('\n')
""".strip().split('\n')
doAssert testInput.squidBingo() == 4512 doAssert testInput.squidBingo() == 4512