Naive day5 solution
This commit is contained in:
parent
e6683932e8
commit
b9a91d78ab
3 changed files with 33 additions and 3 deletions
|
@ -8,5 +8,5 @@ proc solve_for_day(n: int) {.used.} =
|
|||
echo solvers[n]()
|
||||
|
||||
when isMainModule:
|
||||
solve_all()
|
||||
# solve_for_day(4)
|
||||
# solve_all()
|
||||
solve_for_day(5)
|
||||
|
|
30
2020/src/day5.nim
Normal file
30
2020/src/day5.nim
Normal 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
|
|
@ -3,7 +3,7 @@ import input_requestor, os, macros, strformat, tables
|
|||
macro loadDays(): untyped =
|
||||
var solver_str = "var solvers = {\n"
|
||||
result = newStmtList()
|
||||
for day in 1..4:
|
||||
for day in 1..5:
|
||||
let module = fmt"day{day}"
|
||||
if fileExists joinPath("src/", &"{module}.nim"):
|
||||
result.add parseStmt fmt"from {module} import nil"
|
||||
|
|
Loading…
Reference in a new issue