Day 4 part 2
This commit is contained in:
parent
7c663d41e6
commit
e9fa1ac9cb
|
@ -1,4 +1,4 @@
|
||||||
import std/[strutils, sequtils, options, math, parseutils, sets, strformat]
|
import std/[strutils, sequtils, options, math, parseutils, sets, strformat, algorithm]
|
||||||
import ./common
|
import ./common
|
||||||
|
|
||||||
type Board = object
|
type Board = object
|
||||||
|
@ -38,7 +38,7 @@ proc squidBingo(inputLines: seq[string]): int =
|
||||||
boards.add board
|
boards.add board
|
||||||
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.split(' ').filterIt(it != "").mapIt(it.strip())
|
||||||
|
|
||||||
boards.add board
|
boards.add board
|
||||||
|
|
||||||
|
@ -68,38 +68,33 @@ proc loseSquidBingo(inputLines: seq[string]): int =
|
||||||
boards.add board
|
boards.add board
|
||||||
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.split(' ').filterIt(it != "").mapIt(it.strip())
|
||||||
|
|
||||||
boards.add board
|
boards.add board
|
||||||
|
|
||||||
for n in calledNumbers:
|
for n in calledNumbers:
|
||||||
echo &"Calling {n}..."
|
|
||||||
var removeBoards: seq[int] = @[]
|
var removeBoards: seq[int] = @[]
|
||||||
for bbi, b in boards.mpairs():
|
for bbi, b in boards.mpairs():
|
||||||
for i, e in b.entries:
|
for i, e in b.entries:
|
||||||
if n == e:
|
if n == e:
|
||||||
b.checked.add i
|
b.checked.add i
|
||||||
if b.isBoardWinning():
|
if b.isBoardWinning():
|
||||||
echo "Board Win"
|
|
||||||
if (boards.len() - removeBoards.len()) < 2:
|
if (boards.len() - removeBoards.len()) < 2:
|
||||||
echo &"Last Board: {board}"
|
|
||||||
var entriesSum = 0
|
var entriesSum = 0
|
||||||
for i, v in b.entries:
|
for i, v in b.entries:
|
||||||
if not (i in b.checked): entriesSum += v.parseInt()
|
if not (i in b.checked): entriesSum += v.parseInt()
|
||||||
return entriesSum * n.parseInt()
|
return entriesSum * n.parseInt()
|
||||||
else:
|
else:
|
||||||
removeBoards.add(bbi)
|
removeBoards.add(bbi)
|
||||||
echo &"Removing {removeBoards.len()} board(s) of {boards.len()}..."
|
for i in removeBoards.reversed():
|
||||||
for i in removeBoards:
|
boards.delete(i..i)
|
||||||
boards.delete(i, i)
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
let input = 4.loadInput()
|
let input = 4.loadInput()
|
||||||
# time("squidBingo part 1"): echo input.squidBingo()
|
time("squidBingo part 1"): echo input.squidBingo()
|
||||||
# time("loseSquidBingo part 2"): echo input.loseSquidBingo()
|
time("loseSquidBingo part 2"): echo input.loseSquidBingo()
|
||||||
|
|
||||||
when not defined(release):
|
when not defined(release):
|
||||||
static:
|
|
||||||
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
|
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
|
||||||
|
|
||||||
22 13 17 11 0
|
22 13 17 11 0
|
||||||
|
@ -119,7 +114,5 @@ when not defined(release):
|
||||||
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""".split('\n')
|
2 0 12 3 7""".split('\n')
|
||||||
# doAssert testInput.squidBingo() == 4512
|
doAssert testInput.squidBingo() == 4512
|
||||||
let r = testInput.loseSquidBingo()
|
doAssert testInput.loseSquidBingo() == 1924
|
||||||
echo r
|
|
||||||
doAssert r == 1924
|
|
||||||
|
|
Loading…
Reference in a new issue