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 import ./common
type Board = object type Board = object
entries: seq[string] entries: seq[string]
checked: seq[int] checked: seq[int]
let winningSets = [ proc isBoardWinning(b: Board): bool =
let winningSets: seq[HashSet[int]] = [
[0, 1, 2, 3, 4], [0, 1, 2, 3, 4],
[5, 6, 7, 8, 9], [5, 6, 7, 8, 9],
[10, 11, 12, 13, 14], [10, 11, 12, 13, 14],
@ -16,45 +17,48 @@ let winningSets = [
[2, 7, 12, 17, 22], [2, 7, 12, 17, 22],
[3, 8, 13, 18, 23], [3, 8, 13, 18, 23],
[4, 9, 14, 19, 24] [4, 9, 14, 19, 24]
].mapIt(it.toHashSet()) ].mapIt(it.toHashSet())
proc isBoardWinning(b: ref Board): bool =
let s = b.checked.toHashSet() let s = b.checked.toHashSet()
for w in winningSets: for w in winningSets:
if w < s: if w < s:
return true return true
return false 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
# construct boards # construct boards
var boards: seq[Board] var boards: seq[Board]
var board: Board = Board(entries: @[], checked: @[]) var board: Board = Board(entries: @[], checked: @[])
for l in input: for l in input:
echo l
if l.strip() == "": if l.strip() == "":
boards.add board boards.add board
echo board.entries
board = Board(entries: @[], checked: @[]) board = Board(entries: @[], checked: @[])
continue continue
board.entries.add [l[0..1], l[3..4], l[6..7], l[9..10], l[12..13]].mapIt(it.strip()) 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 n in calledNumbers:
for b in boards: echo n
for b in boards.mitems():
for i, e in b.entries: for i, e in b.entries:
if n == e: if n == e:
# TODO: why can't I add here but I can add on 46?! # TODO: why can't I add here but I can add on 46?!
b.checked.add i b.checked.add i
echo b.entries, b.checked
if b.isBoardWinning(): if b.isBoardWinning():
# TODO: get unchecked entries var entriesSum = 0
# TODO: multiple their sum with last .checked for i, v in b.entries:
return 9 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 return 0

View file

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