From b9a91d78ab93c66964a964b0e8ae4cea3dd2b157 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sat, 5 Dec 2020 07:59:16 -0600 Subject: [PATCH] Naive day5 solution --- 2020/src/aoc2020.nim | 4 ++-- 2020/src/day5.nim | 30 ++++++++++++++++++++++++++++++ 2020/src/day_loader.nim | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 2020/src/day5.nim diff --git a/2020/src/aoc2020.nim b/2020/src/aoc2020.nim index 720eab8..4d5052a 100644 --- a/2020/src/aoc2020.nim +++ b/2020/src/aoc2020.nim @@ -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) diff --git a/2020/src/day5.nim b/2020/src/day5.nim new file mode 100644 index 0000000..31188ed --- /dev/null +++ b/2020/src/day5.nim @@ -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 diff --git a/2020/src/day_loader.nim b/2020/src/day_loader.nim index 05da3eb..e6fc2b4 100644 --- a/2020/src/day_loader.nim +++ b/2020/src/day_loader.nim @@ -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"