Overview
Context
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))
|
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |