aboutsummaryrefslogtreecommitdiff
path: root/2020/fourteen.nim
blob: e333cf9a34e1b48acdaeedbdf7116cb5b63e1f48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Day Fourteen: Docking Data
import os, strutils, sequtils

let input: string = paramStr(1)
let program: seq[tuple[address, value: string]] = map(split(strip(readFile(input)), '\n'),
  func (param: string): (string, string) =
    (split(param, " = ")[0], split(param, " = ")[1]))

var mask: string = repeat('X', 36)
var memory: seq[int] = newSeq[int](99999)
for line in program:
  if line.address == "mask":
    mask = line.value
  elif line.address[0..2] == "mem":
    var value: string = toBin(parseInt(line.value), 36)
    for i, bit in mask:
      if bit != 'X':
        value[i] = bit
    memory[parseInt(line.address[4..^2])] = fromBin[int](value)

echo foldl(memory, a + b)