Day 4 part 1 done

This commit is contained in:
Daniel Flanagan 2021-12-05 09:01:48 -06:00
parent fe68c24b57
commit c62e76023a
2 changed files with 29 additions and 25 deletions

View file

@ -1,11 +1,12 @@
import std/[strutils, sequtils, options, math, parseutils, sets]
import std/[strutils, sequtils, options, math, parseutils, sets, strformat]
import ./common
type Board = object
entries: seq[string]
checked: seq[int]
let winningSets = [
proc isBoardWinning(b: Board): bool =
let winningSets: seq[HashSet[int]] = [
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
@ -18,43 +19,46 @@ let winningSets = [
[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 =
var input: seq[string] = inputLines
let calledNumbers: seq[string] = input[0].split ','
input.delete 0..1
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())
boards.add board
echo boards
for n in calledNumbers:
for b in boards:
echo n
for b in boards.mitems():
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
echo b.entries, b.checked
if b.isBoardWinning():
# TODO: get unchecked entries
# TODO: multiple their sum with last .checked
return 9
var entriesSum = 0
for i, v in b.entries:
if not (i in b.checked): entriesSum += v.parseInt()
else: echo &"{i} {v}"
echo &"{entriesSum} * {n} = {entriesSum * n.parseInt()}"
return entriesSum * n.parseInt()
return 0

View file

@ -40,7 +40,7 @@ Or
- [x] [Day 1](./1.ts)
- [x] [Day 2](./2.ts)
- [ ] Day 3
- [x] [Day 3](./3.ts)
- [ ] Day 4
- [ ] Day 5
- [ ] Day 6