From 1cf553f55b4dc09c2136b5f92c1ad0aca799d51d Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 14 Dec 2020 13:01:58 -0600 Subject: [PATCH] Day 14 progress --- 2020/src/aoc2020.nim | 2 +- 2020/src/day14.nim | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 2020/src/day14.nim diff --git a/2020/src/aoc2020.nim b/2020/src/aoc2020.nim index 95f7268..b50243b 100644 --- a/2020/src/aoc2020.nim +++ b/2020/src/aoc2020.nim @@ -9,4 +9,4 @@ proc solve_for_day(n: int) {.used.} = when isMainModule: # solve_all() - solve_for_day(13) + solve_for_day(14) diff --git a/2020/src/day14.nim b/2020/src/day14.nim new file mode 100644 index 0000000..3ed191b --- /dev/null +++ b/2020/src/day14.nim @@ -0,0 +1,46 @@ +import streams, strutils, bitops, tables + +proc asMasks(s: string): (uint64, uint64) = + var ones, zeroes = 0'u64 + for c in s: + ones = rotateLeftBits(ones, 1'u8) + zeroes = rotateLeftBits(zeroes, 1'u8) + case c: + of '1': ones.setBit(0'u8) + of '0': zeroes.setBit(0'u8) + else: discard + echo ("Masks:", ones, zeroes) + return (ones, zeroes) + +doAssert "01X".asMasks() == (2'u64, 4'u64) + +proc withMasks(n: uint64, mask: (uint64, uint64)): uint64 = + echo n.toBin + n.bitand(mask[1]).bitnot.bitand(mask[0].bitnot).bitnot + +proc part1*(s: Stream): int = + var masks: (uint64, uint64) + var memory = newTable[uint64, uint64]() + var i = 0 + for l in s.lines: + if l.startsWith("mask = "): + masks = l.split(" = ")[1].asMasks + elif l.startsWith("mem["): + let ll = l.split("[")[1].split("] = ") + memory[ll[0].parseUInt] = ll[1].parseUInt.withMasks(masks) + inc i + echo memory + for v in memory.values: result += int v + + # 9,639,694,085,526 is too high + +let n = """ +mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X +mem[8] = 11 +mem[7] = 101 +mem[8] = 0 +""".newStringStream.part1() +echo n +doAssert n == 165 + +proc part2*(s: Stream): int =9