This commit is contained in:
Jonathan Chan 2019-12-10 23:16:39 -08:00
parent 3c4f817729
commit ce29e2e206
2 changed files with 83 additions and 0 deletions

1
input/11.txt Normal file
View File

@ -0,0 +1 @@
3,8,1005,8,311,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,28,1,1104,0,10,1006,0,71,2,1002,5,10,2,1008,5,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,66,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,87,1006,0,97,2,1002,6,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,116,1006,0,95,1,1009,10,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,145,1,1002,19,10,2,1109,7,10,1006,0,18,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,179,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,200,1,1105,14,10,1,1109,14,10,2,1109,11,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,235,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,257,2,101,9,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,282,2,1109,19,10,1,105,0,10,101,1,9,9,1007,9,1033,10,1005,10,15,99,109,633,104,0,104,1,21102,937268368140,1,1,21102,328,1,0,1106,0,432,21102,1,932700599052,1,21101,0,339,0,1105,1,432,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,209421601831,1,21102,1,386,0,1106,0,432,21102,235173604443,1,1,21102,1,397,0,1106,0,432,3,10,104,0,104,0,3,10,104,0,104,0,21101,825439855372,0,1,21102,1,420,0,1106,0,432,21101,0,988220907880,1,21102,431,1,0,1106,0,432,99,109,2,22101,0,-1,1,21101,40,0,2,21102,1,463,3,21102,453,1,0,1106,0,496,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,458,459,474,4,0,1001,458,1,458,108,4,458,10,1006,10,490,1102,1,0,458,109,-2,2106,0,0,0,109,4,2102,1,-1,495,1207,-3,0,10,1006,10,513,21102,0,1,-3,22102,1,-3,1,21202,-2,1,2,21102,1,1,3,21101,532,0,0,1105,1,537,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,560,2207,-4,-2,10,1006,10,560,21201,-4,0,-4,1106,0,628,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,1,579,0,1106,0,537,21202,1,1,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,598,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,620,21201,-1,0,1,21102,1,620,0,105,1,495,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0

82
src/11.rkt Normal file
View File

@ -0,0 +1,82 @@
#lang plai
(require "../lib.rkt"
"IntCode.rkt")
(define input
(string->program (car (problem-input 11))))
;; The hull as a hash (x . y) -> c
;; x increases rightward
;; y increases downward
(define hull (make-hash))
;; The hull as a vector of row vectors of c
;; i.e. x = index of inner vectors,
;; y = index of outer vector
(define (make-grid xrange yrange)
(build-vector
(add1 yrange)
(λ (_) (make-vector (add1 xrange) #\ ))))
;; Turn CCW if δ = 0, CW if δ = 1
(define (update-dir dir δ)
(match dir
['U (if (zero? δ) 'L 'R)]
['D (if (zero? δ) 'R 'L)]
['L (if (zero? δ) 'D 'U)]
['R (if (zero? δ) 'U 'D)]))
;; Move in given direction (up/down/left/right)
(define (update-pos dir xy)
(let ([x (car xy)]
[y (cdr xy)])
(match dir
['U (cons x (sub1 y))]
['D (cons x (add1 y))]
['L (cons (sub1 x) y)]
['R (cons (add1 x) y)])))
;; Deploy the emergency hull painting robot!
(define (deploy st dir xy)
(define clr (hash-ref hull xy 0))
(type-case state st
[halt (_) (void)]
[in (resume)
(let*-values
([(clr* st) (resume-with-output (resume clr))]
[(δ st) (resume-with-output st)])
(cond [(neq? clr clr*)
(hash-set! hull xy clr*)])
(let* ([dir (update-dir dir δ)]
[xy (update-pos dir xy)])
(deploy st dir xy)))]
[else (error "Unexpected program state.")]))
(define (part1)
(deploy (exec input) 'U '(0 . 0))
(hash-count hull))
(define (part2)
(set! hull (make-hash '(((0 . 0) . 1))))
(deploy (exec input) 'U '(0 . 0))
(let* ([positions (hash-keys hull)]
[xs (map car positions)]
[ys (map cdr positions)]
[xmin (apply min xs)] [xmax (apply max xs)]
[ymin (apply min ys)] [ymax (apply max ys)]
[xrange (- xmax xmin)] [yrange (- ymax ymin)]
[grid (make-grid xrange yrange)])
(for-each
(λ (kv)
(let* ([x (- (caar kv) xmin)]
[y (- (cdar kv) ymin)]
[c (cdr kv)]
[row (vector-ref grid y)])
(cond [(= c 1) (vector-set! row x #\█)])))
(hash->list hull))
(for-each
(λ (row) (displayln (list->string (vector->list row))))
(vector->list grid))))
(show-solution (part1) (part2))