This commit is contained in:
Jonathan Chan 2019-12-18 23:23:22 -08:00
parent 5d4d9686b8
commit 160655eecc
2 changed files with 78 additions and 0 deletions

1
input/19.txt Normal file
View File

@ -0,0 +1 @@
109,424,203,1,21101,11,0,0,1105,1,282,21102,18,1,0,1105,1,259,2102,1,1,221,203,1,21102,1,31,0,1106,0,282,21101,0,38,0,1106,0,259,20102,1,23,2,21202,1,1,3,21101,0,1,1,21102,57,1,0,1105,1,303,2101,0,1,222,20102,1,221,3,20101,0,221,2,21102,259,1,1,21101,0,80,0,1106,0,225,21102,135,1,2,21101,0,91,0,1105,1,303,2102,1,1,223,21001,222,0,4,21102,259,1,3,21102,1,225,2,21101,0,225,1,21101,118,0,0,1106,0,225,20101,0,222,3,21101,0,12,2,21101,0,133,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21102,1,148,0,1105,1,259,1202,1,1,223,21002,221,1,4,20102,1,222,3,21101,0,17,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,1,195,0,105,1,109,20207,1,223,2,21001,23,0,1,21101,0,-1,3,21101,214,0,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1202,-4,1,249,21201,-3,0,1,22102,1,-2,2,22102,1,-1,3,21102,250,1,0,1106,0,225,21202,1,1,-4,109,-5,2106,0,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2105,1,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21201,-2,0,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22102,1,-2,3,21101,0,343,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,22101,0,-4,1,21102,384,1,0,1105,1,303,1106,0,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22102,1,1,-4,109,-5,2106,0,0

77
src/19.rkt Normal file
View File

@ -0,0 +1,77 @@
#lang racket
(require "../lib.rkt"
"IntCode.rkt")
(define input
(string->program (car (problem-input 19))))
(define space-hash (make-hash '((0 . #\.) (1 . #\#))))
(define-values (part1 hash-grid)
(let* ([coords (cartesian-product (range 0 50) (range 0 50))]
[readings (append* (map (λ (coord) (resume-with-io (exec input) coord)) coords))]
[hash-grid (make-hash)])
(for ([coord coords] [reading readings])
(match-let ([(list x y) coord])
(hash-set! hash-grid (cons x y) reading)))
(values (sum readings) hash-grid)))
;; The bottom bounding diagonal follows the following pattern:
;; - 4, 4, 4, 4, 4, 4, 4, 5,
;; 4, 4, 4, 4, 4, 4, 5 for all subsequent rows
;; The top bounding diagonal follows the following pattern:
;; - 3, 3, 3, 2,
;; 3, 3, 3, 2, 3, 3, 2 for all subsequent rows
;; Where n1, ... indicates we go 1 across, then n1 down, etc.
;; The bottom diagonal has a slope of around 29/7;
;; The top diagonal has a slope of around 19/7.
;; The diagonal of the 100x100 square is given by x+c for some c.
;; We solve for the intersection points of the square diagonal
;; with the top and bottom diagonals by solving the equations
;; - xb - c = -27/9 xb
;; - xt - c = -19/7 xt
;; - xt - xb = 100
;; Where (xb, yb) is the lower left corner and (xt, yt) is the top right:
;; (xb, yt) .... (xt, yt)
;; .... ....
;; (xb, yb) .... (xt, yb)
;; This gives us
;; - xb = 260, yb = 1077
;; - xt = 360, yt = 977
;; But the ranges [260, 360] and [977, 1077] contain 101 points,
;; when we only need to fit 100 points. We have two options to test:
;; - (360, 977) and (261, 1076), or
;; - (359, 978) and (260, 1077).
(define corner-360x977? ; #f
(= 1 (first (resume-with-io (exec input) '(360 977)))))
(define corner-261x1076? ; #t
(= 1 (first (resume-with-io (exec input) '(261 1076)))))
(define corner-359x978? ; #t
(= 1 (first (resume-with-io (exec input) '(359 978)))))
(define corner-260x1077? ; #f
(= 1 (first (resume-with-io (exec input) '(260 1077)))))
;; Neither of the pairs simultaneously have both points in the beam!
;; We need to shift right and move down.
(define xb ; 261
(let loop ([xb 261])
(if (= 1 (first (resume-with-io (exec input) (list xb 1077))))
xb (loop (add1 xb)))))
(define yt ; 980
(let loop ([yt 978])
(if (= 1 (first (resume-with-io (exec input) (list 360 yt))))
yt (loop (add1 yt)))))
;; x ∈ [261, 360] contains 100 points. y ∈ [980, 1077] doesn't.
;; We can't expand upwards; we can only expand downwards.
;; Then the bottom right corner is (360, 1079),
;; and the top left corner is (261, 980).
(define part2 (+ (* 261 10000) 980))
(show-solution part1 part2)