From 1daa7758cc4eba6d353282042bbfd0efc2074dc4 Mon Sep 17 00:00:00 2001 From: haskal Date: Tue, 8 Dec 2020 00:17:32 -0500 Subject: [PATCH] jesus christ i am legit ashamed that i wrote this --- 8.rkt | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 8.rkt diff --git a/8.rkt b/8.rkt new file mode 100644 index 0000000..e320ee8 --- /dev/null +++ b/8.rkt @@ -0,0 +1,66 @@ +#lang curly-fn racket + +(require "scripts/aoc.rkt") + +;; solution for day 8 + +(struct insn [type arg] #:transparent) + +;; helper functions here + +(define (part1 input) + (define (execute pc acc [seen (mutable-set)]) + (if (set-member? seen pc) + acc + (let ([in (list-ref input pc)]) + (set-add! seen pc) + (match (insn-type in) + ["nop" (execute (add1 pc) acc seen)] + ["jmp" (execute (+ pc (insn-arg in)) acc seen)] + ["acc" (execute (add1 pc) (+ acc (insn-arg in)) seen)] + [x (error "not shonks" x)])))) + (execute 0 0)) + +(define (part2 input) + (define (execute-actual input) + (define (execute pc acc [seen (mutable-set)]) + (if (>= pc (length input)) + acc + (let ([in (list-ref input pc)]) + (set-add! seen pc) + (match (insn-type in) + ["nop" (execute (add1 pc) acc seen)] + ["jmp" (execute (+ pc (insn-arg in)) acc seen)] + ["acc" (execute (add1 pc) (+ acc (insn-arg in)) seen)] + [x (error "not shonks" x)])))) + (execute 0 0)) + + (define target-pc (length input)) + (for ([in (in-list input)] [i (in-naturals)] #:unless (string=? "acc" (insn-type in))) + (define new-type (if (string=? (insn-type in) "jmp") "nop" "jmp")) + (define new-insn (insn new-type (insn-arg in))) + ;; aaaa + (define vec (vector-copy (list->vector input))) + (vector-set! vec i new-insn) + (define new-prg (vector->list vec)) + (with-handlers ([exn? (lambda (x) (answer 8 2 (execute-actual new-prg)))]) + (part1 new-prg)))) + +(module+ test + (require rackunit) + ;; tests here + (displayln "no tests :(")) + +(module+ main + (define input + (for/list ([line (in-list (file->lines "inputs/8"))]) + (match (string-split line) + [(list a b) (insn a (string->number b))] + [x (error "not shonks" x)]))) + ;; part 1 + (answer 8 1 (part1 input)) + + ;; part 2 + (answer 8 2 (part2 input)) + + (displayln "meow"))