adventofcode-2023/day_04/solution_04a.nim

27 lines
937 B
Nim
Raw Normal View History

import std/[cmdline, math, sequtils, setutils, strformat, strscans, strutils]
var result = 0
var fn = if paramCount() > 0: paramStr(1) else: "input_04.txt"
var lines = readFile(fn).strip.splitLines
for line in lines:
var card: int
let cardspec = line.split(":", 1)
if scanf(cardspec[0].strip, "Card$s$i", card):
let play = cardspec[1].split("|")[0].strip.splitWhitespace().mapIt(it.strip).mapIt(it.parseInt).mapIt(it.int8).toSet
let jackpot = cardspec[1].split("|")[1].strip.splitWhitespace().mapIt(it.strip).mapIt(it.parseInt).mapIt(it.int8).toSet
#echo &"Card {card}: {left}, {right} => {left * right}"
let wins = (play * jackpot).len
if wins == 1:
result += 1
elif wins > 1:
result += 2 ^ (wins - 1)
echo &"Result: {result}"
case fn:
of "input_04.txt":
assert result == 18519
of "sample_input_04.txt":
assert result == 13