diff options
Diffstat (limited to '2020')
-rw-r--r-- | 2020/day-07/day-07.rkt | 16 | ||||
-rw-r--r-- | 2020/day-08/day-08.ipynb | 114 |
2 files changed, 123 insertions, 7 deletions
diff --git a/2020/day-07/day-07.rkt b/2020/day-07/day-07.rkt index 6ad63e5..f2a1ffe 100644 --- a/2020/day-07/day-07.rkt +++ b/2020/day-07/day-07.rkt @@ -1,10 +1,10 @@ #lang racket -(require "../../jj-aoc.rkt" +(require advent-of-code threading rebellion/collection/entry rebellion/collection/multidict) -(define raw-rules (~> (open-day 7 2020) (port->string) (string-split "\n"))) +(define raw-rules (~> (open-aoc-input (find-session) 2020 7) (port->string) (string-split "\n"))) (define (split-rule r) (match-define (list head tail) (string-split (string-trim r #px" bags?.") " bags contain ")) @@ -20,10 +20,9 @@ (define rules-multidict (for*/multidict ([ln (in-list raw-rules)] #:do [(match-define (list* from tos) (split-rule ln))] [to (in-list tos)]) - (entry from to))) + (entry from to))) ;; part 1 - (define (bags-that-eventually-contain target) (for/fold ([holders (set)]) ([rule (in-multidict-entries rules-multidict)]) (match rule @@ -31,14 +30,17 @@ (set-union (set-add holders outside) (bags-that-eventually-contain outside))] [_ holders]))) -(time (set-count (bags-that-eventually-contain '|shiny gold|))) +(define part-1 (set-count (bags-that-eventually-contain '|shiny gold|))) +(~a "Part 1: " part-1) +;; (aoc-submit (find-session) 2020 7 1 part-1) ;; part 2 - (define (bags-that-are-contained-by target) (for/sum ([holding (in-multidict-entries rules-multidict)]) (match holding [(entry (== target) (list held n)) (+ n (* n (bags-that-are-contained-by held)))] [_ 0]))) -(time (bags-that-are-contained-by '|shiny gold|)) +(define part-2 (bags-that-are-contained-by '|shiny gold|)) +(~a "Part 2: " part-2) +;; (aoc-submit (find-session) 2020 7 1 part-2) diff --git a/2020/day-08/day-08.ipynb b/2020/day-08/day-08.ipynb new file mode 100644 index 0000000..39d4b8e --- /dev/null +++ b/2020/day-08/day-08.ipynb @@ -0,0 +1,114 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "vscode": { + "languageId": "racket" + } + }, + "outputs": [], + "source": [ + "#lang iracket/lang #:require racket\n", + "(require advent-of-code\n", + " threading\n", + " rebellion/collection/entry\n", + " rebellion/collection/multidict)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "vscode": { + "languageId": "racket" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "<code>'(acc . 49)</code>" + ], + "text/plain": [ + "'(acc . 49)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(define raw-instructions (~> (fetch-aoc-input (find-session) 2020 8) (string-split \"\\n\")))\n", + "\n", + "(define instruction-set (for/hash ([instruction (in-list raw-instructions)] [i (in-naturals)])\n", + " (match-define (list op val) (string-split instruction))\n", + " (values i (cons (string->symbol op) (string->number val)))))\n", + "\n", + "(hash-ref instruction-set 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "vscode": { + "languageId": "racket" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "<code>1949</code>" + ], + "text/plain": [ + "1949" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(define (execute [instruction (hash-ref instruction-set 0)] [acc 0] [line 0] [visited (set)])\n", + " (match instruction\n", + " [_\n", + " #:when (set-member? visited line)\n", + " acc]\n", + " [(cons 'acc n)\n", + " (define new-line (add1 line))\n", + " (execute (hash-ref instruction-set new-line) (+ acc n) new-line (set-add visited line))]\n", + " [(cons 'jmp n)\n", + " (define new-line (+ n line))\n", + " (execute (hash-ref instruction-set new-line) acc new-line (set-add visited line))]\n", + " [(cons 'nop _)\n", + " (define new-line (add1 line))\n", + " (execute (hash-ref instruction-set new-line) acc new-line (set-add visited line))]))\n", + "\n", + "(execute)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Racket (trusted)", + "language": "racket", + "name": "racket-trusted" + }, + "language_info": { + "codemirror_mode": "scheme", + "file_extension": ".rkt", + "mimetype": "text/x-racket", + "name": "Racket", + "pygments_lexer": "racket", + "version": "8.7" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} |