🎄⌨️ Advent of Code 2018

Check-in [445eb7]
Overview
Comment:Add Day 3 solutions and output
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 445eb756e55d7daa90b41e4f10f56e6de54c59559e91c7b22fbd53069fec0e83
User & Date: joel on 2018-12-03 20:29:36
Other Links: manifest | tags
Context
2018-12-04
01:43
Update README check-in: 1f136f user: joel tags: trunk
2018-12-03
20:29
Add Day 3 solutions and output check-in: 445eb7 user: joel tags: trunk
20:27
Add Day 3 input check-in: a68afe user: joel tags: trunk
Changes

Added day03-results.png version [a7f924].

cannot compute difference between binary files

Added day03.rkt version [30492b].









































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#lang racket

(require racket/draw rackunit)

(define input (file->lines "day03-input.txt"))

(define fabric-bmp (make-bitmap 1000 1000 #t))
(define fabric-dc (new bitmap-dc% [bitmap fabric-bmp]))
(define c (make-object color% 0 255 0 0.5))
(define pb (new brush% [color c]))
(define color-probe (make-object color%))

(send fabric-dc set-brush pb)
(send fabric-dc set-pen "white" 0 'transparent) ; don't draw outlines

;; Get the alpha value of pixel x y
(define (get-fabric-value x y)
  (send fabric-dc get-pixel x y color-probe)
  (send color-probe alpha))

;; Parse “claims” of the form "#1 @ 1,3: 4x4" into '(1 1 3 4 4)
(define (parse-claim str)
  (map string->number (rest (regexp-match #px"#(\\d+) @ (\\d+),(\\d+): (\\d+)x(\\d+)" str))))

;; draw a particular claim onto the fabric map
(define (draw-claim l)
  (send/apply fabric-dc draw-rectangle (rest l)))

;; Returns #t if a pixel's alpha value indicates it’s been painted on more than once
(define (is-overlap? v)
  ;; For some reason the actual alpha of a pixel that gets
  ;; painted with my brush exactly once comes out to this weird number
  (> v 0.5019607843137255))

;; Count the number of pixels with overlap values
(define (count-overlap [startx 0] [starty 0] [width 1000] [height 1000])
  (count is-overlap?
         (for*/list ([x (in-range startx (+ startx width))]
                     [y (in-range starty (+ starty height))])
                    (get-fabric-value x y))))

(define (day03-part1)
  (map draw-claim (map parse-claim input))
  (count-overlap))

(module+ test
  (check-equal? (day03-part1) 100595)) ; Correct answer for part 1

(define (claim-has-overlap? l)
  (> (apply count-overlap (rest l)) 0))

(define (day03-part2)
  (define answer
    (for/first ([c (in-list (map parse-claim input))]
              #:when (not (claim-has-overlap? c)))
             c))
  (highlight-claim answer)
  (send fabric-bmp save-file "day03-results.png" 'png)
  (first answer))

(module+ test
  (check-equal? (day03-part2) 415)) ; Correct answer for part 2

;; Extra

(define (highlight-claim c)
  (send fabric-dc set-brush "red" 'solid)
  (draw-claim c))