2020-12-14 05:27:19 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
2020-12-14 06:10:58 +00:00
|
|
|
;; data/bit-vector is hella nerfed so.... rosette uwu
|
|
|
|
;; rosette is slow af for this but whatever
|
|
|
|
(require (only-in rosette bv bvand bvor bvxor rotate-left concat bitvector zero-extend
|
|
|
|
bitvector->natural))
|
2020-12-14 05:27:19 +00:00
|
|
|
|
|
|
|
;; solution for day 14
|
2020-12-14 06:10:58 +00:00
|
|
|
(define (mask->bv bits select)
|
|
|
|
(apply concat (map #{if (equal? % select) (bv select 1) (bv (- 1 select) 1)} bits)))
|
|
|
|
|
|
|
|
(define (pos->bv pos)
|
|
|
|
(rotate-left (- 35 pos) (zero-extend (bv 1 1) (bitvector 36))))
|
2020-12-14 05:27:19 +00:00
|
|
|
|
|
|
|
(define (part1 input)
|
2020-12-14 06:10:58 +00:00
|
|
|
(define orval #f)
|
|
|
|
(define andval #f)
|
2020-12-14 05:28:48 +00:00
|
|
|
(define mem (make-hash))
|
|
|
|
(for ([ins (in-list input)])
|
|
|
|
(match ins
|
2020-12-14 06:10:58 +00:00
|
|
|
[(list 'mask bits ...)
|
|
|
|
(set! orval (mask->bv bits 1))
|
|
|
|
(set! andval (mask->bv bits 0))]
|
|
|
|
[(list 'set addr val)
|
|
|
|
(hash-set! mem addr
|
|
|
|
(bitvector->natural (bvor (bvand (bv val 36) andval) orval)))]))
|
2020-12-14 05:28:48 +00:00
|
|
|
(for/sum ([(k v) (in-hash mem)])
|
|
|
|
v))
|
2020-12-14 05:27:19 +00:00
|
|
|
|
|
|
|
(define (part2 input)
|
|
|
|
(define mem (make-hash))
|
|
|
|
(define mask #f)
|
|
|
|
(for ([ins (in-list input)])
|
|
|
|
(match ins
|
2020-12-14 06:10:58 +00:00
|
|
|
[(list 'mask bits ...) (set! mask bits)]
|
|
|
|
[(list 'set addr val)
|
|
|
|
(define addr-bits (bvor (bv addr 36) (mask->bv mask 1)))
|
2020-12-14 05:27:19 +00:00
|
|
|
(define addrs-set (list addr-bits))
|
2020-12-14 06:10:58 +00:00
|
|
|
(for ([item (in-list mask)] [i (in-naturals)] #:when (equal? 'x item))
|
|
|
|
(set! addrs-set (append addrs-set (map #{bvxor (pos->bv i) %} addrs-set))))
|
2020-12-14 05:27:19 +00:00
|
|
|
(for ([addr (in-list addrs-set)])
|
2020-12-14 06:10:58 +00:00
|
|
|
(hash-set! mem (bitvector->natural addr) val))]))
|
2020-12-14 05:27:19 +00:00
|
|
|
(for/sum ([(k v) (in-hash mem)])
|
|
|
|
v))
|
|
|
|
|
|
|
|
;; parse input file
|
|
|
|
(define (parse fname)
|
|
|
|
(define input (file->lines fname))
|
|
|
|
(for/list ([i (in-list input)])
|
|
|
|
(match i
|
2020-12-14 06:10:58 +00:00
|
|
|
[(pregexp #px"^mask = (.*?)$" (list _ mask))
|
|
|
|
(cons 'mask (for/list ([chr (in-string mask)])
|
|
|
|
(match chr
|
|
|
|
[#\1 1]
|
|
|
|
[#\0 0]
|
|
|
|
[#\X 'x])))]
|
2020-12-14 05:27:19 +00:00
|
|
|
[(pregexp #px"^mem\\[([0-9]+)\\] = ([0-9]+)$" (list _ addr val))
|
2020-12-14 06:10:58 +00:00
|
|
|
(list 'set (string->number addr) (string->number val))]
|
2020-12-14 05:27:19 +00:00
|
|
|
[_ (error "not shonks i")])))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(require rackunit)
|
|
|
|
;; tests here
|
|
|
|
(displayln "no tests :("))
|
|
|
|
|
|
|
|
(module+ main
|
|
|
|
(define input (parse "inputs/14"))
|
|
|
|
(answer 14 1 (time (part1 input)))
|
|
|
|
(answer 14 2 (time (part2 input)))
|
|
|
|
(displayln "meow"))
|