diff --git a/2021/four.nim b/2021/four.nim index d588d96..41645ac 100644 --- a/2021/four.nim +++ b/2021/four.nim @@ -1,60 +1,64 @@ -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 = [ - [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: Board): bool = + let winningSets: seq[HashSet[int]] = [ + [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 = 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 diff --git a/2021/readme.md b/2021/readme.md index 571f558..120fdd0 100644 --- a/2021/readme.md +++ b/2021/readme.md @@ -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