-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rkt
166 lines (140 loc) · 4.74 KB
/
08.rkt
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#|
Day 8
https://adventofcode.com/2024/day/08
Easy day.
Figured out that the problem was really just asking you
to calculate the slope formed by each pair of the same letters.
The first part was just finding the next point on both sides of the lined
formed between the pair of letters.
The second part ask you to find every point in the line
(within bounds).
|#
#lang racket/base
(require racket/string)
(require racket/list)
(require racket/set)
(require "../solution.rkt")
(require "../utils/point.rkt")
(require "../utils/matrix.rkt")
(require "../utils/lists.rkt")
(provide run)
;; Antennas = [Hashof Character -> [Listof Point]]
;; String -> Antennas Number Number
(define (parse-antennas input)
(define matrix (map string->list (string-split input "\n")))
(define (add-point h l p) (hash-set h l (cons p (hash-ref h l list))))
;; Antennas [Listof Character] Point -> Antennas
;; parses each point in row
;; adds to 'as'
(define (parse-row-point as row p)
(cond
[(empty? row) as]
[else
(define cur (car row))
(define remaining (cdr row))
(define is-antenna? (not (equal? #\. (car row))))
(define updated-as (if is-antenna? (add-point as cur p) as))
(parse-row-point updated-as
remaining
(translate p 1 0))]))
;; parses each row in the matrix 'm'
(define (parse-row as m p)
(if (empty? m) as
(parse-row
(parse-row-point as (car m) p)
(cdr m)
(translate p 0 1))))
(values
;; return the atnenna map
(parse-row (hash) matrix (point 0 0))
;; Max X
(length (car matrix))
;; Max Y
(length matrix)))
;; Number Number -> [Point Point -> [Listof Point]]
;; Returns a function that for two points, determines
;; all anti-nodes following the 'part 1' descrption
(define ((part1-antinode max-x max-y) p1 p2)
;; find slope of two points
(define s (slope-of p1 p2))
;; using slope find the two points
;; before and after the pair, if connected
;; by a line
(define after (translate-slope p2 s))
(define before (translate-slope p1 (negate-slope s)))
(filter
;; filter out any if they are out of bounds
(λ (p) (point-within? p max-x max-y))
(list before after)))
;; Number Number -> [Point Point -> [Listof Point]]
;; Returns a function that for two points, determines
;; all anti-nodes following the 'part 2' descrption
(define ((part2-antinode max-x max-y) p1 p2)
(define s (slope-of p1 p2))
;; using slope find the all points (that are in bounds)
;; before and after the pair, if connected
;; by a line
(define (points-to-bounds p s acc)
(define next (translate-slope p s))
(cond [(not (point-within? next max-x max-y)) acc]
[else (points-to-bounds next s (cons next acc))]))
(append (list p1 p2)
(points-to-bounds p2 s '())
(points-to-bounds p1 (negate-slope s) '())))
;; [Point Point -> [Listof Point]] Antennas -> [Setof Point]
;; finds all antinodes for a table of Antennas
;; using the function 'an-func' to determine the antinodes
;; generated from two points of the same antenna type
(define (find-antinodes an-func am)
(define all-points (hash-values am))
(define all-antinodes (foldl
(λ (p acc) (append acc (map-pair an-func p)))
'()
all-points))
(apply set all-antinodes))
(define (run input)
(define-values (antennas max-x max-y) (parse-antennas input))
(define part1-antinode-func (part1-antinode max-x max-y))
(define part2-antinode-func (part2-antinode max-x max-y))
(full-solution
(set-count (find-antinodes part1-antinode-func antennas))
(set-count (find-antinodes part2-antinode-func antennas))))
(module+ test
(require rackunit)
(define example-input #<<EOF
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
EOF
)
(define-values (example-antennas max-x max-y) (parse-antennas example-input))
(check-equal? example-antennas
(hash #\0 `(,(point 4 4)
,(point 7 3)
,(point 5 2)
,(point 8 1))
#\A `(,(point 9 9)
,(point 8 8)
,(point 6 5))
))
(check-equal? max-x 12)
(check-equal? max-y 12)
(define part1-an-f (part1-antinode max-x max-y))
(define points (find-antinodes part1-an-f example-antennas))
;; (print points)
(check-equal? (set-count points)
14)
(define part2-an-f (part2-antinode max-x max-y))
(define points-2 (find-antinodes part2-an-f example-antennas))
(check-equal? (set-count points-2)
34)
)