Naive day5 solution

This commit is contained in:
Daniel Flanagan 2020-12-05 07:59:16 -06:00
parent e6683932e8
commit b9a91d78ab
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
3 changed files with 33 additions and 3 deletions

View file

@ -8,5 +8,5 @@ proc solve_for_day(n: int) {.used.} =
echo solvers[n]() echo solvers[n]()
when isMainModule: when isMainModule:
solve_all() # solve_all()
# solve_for_day(4) solve_for_day(5)

30
2020/src/day5.nim Normal file
View file

@ -0,0 +1,30 @@
import streams, bitops, sets
# TODO: this one needs some refining for sure
proc row(l: string): uint64 =
for c in l:
result = result.rotateLeftBits(1)
if c == 'B': result.setBit(0)
proc col(l: string): uint64 =
for c in l:
result = result.rotateLeftBits(1)
if c == 'R': result.setBit(0)
# do I really need row * 8 + col or can I just keep rotating and shifting
proc toId(row: uint64, col: uint64): uint64 = (row*8)+col
proc part1*(s: Stream): int =
for l in s.lines():
let id = cast[int](toId(row(l[0 .. 6]), col(l[7 .. ^1])))
if id > result: result = id
proc part2*(s: Stream): int =
var ids = initHashSet[int]()
for l in s.lines():
let id = cast[int](toId(row(l[0 .. 6]), col(l[7 .. ^1])))
ids.incl(id)
for i in 1..890:
if ids.contains(i+1) and ids.contains(i-1) and not ids.contains(i):
return i

View file

@ -3,7 +3,7 @@ import input_requestor, os, macros, strformat, tables
macro loadDays(): untyped = macro loadDays(): untyped =
var solver_str = "var solvers = {\n" var solver_str = "var solvers = {\n"
result = newStmtList() result = newStmtList()
for day in 1..4: for day in 1..5:
let module = fmt"day{day}" let module = fmt"day{day}"
if fileExists joinPath("src/", &"{module}.nim"): if fileExists joinPath("src/", &"{module}.nim"):
result.add parseStmt fmt"from {module} import nil" result.add parseStmt fmt"from {module} import nil"