-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10.ss
More file actions
executable file
Β·235 lines (193 loc) Β· 5.73 KB
/
10.ss
File metadata and controls
executable file
Β·235 lines (193 loc) Β· 5.73 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env scheme --script
(load "utils.ss")
;; Line is Pair
;; interp. line equation of slope-intercept form
;; y = ax + b
;; where '(a . b) are the parameters; used as key
;; Point is Pair
;; interp. point '(x . y) components;
;; all points are on an inverted plane -- origin is top left
;; Pair -> Integer
;; produce symbol hash of pair of integers by converting it to
;; string, joining, then converting to symbol
(define (pair-hash p)
(symbol-hash
(string->symbol
(string-append
(number->string (car p))
(if (number? (cdr p))
(number->string (cdr p))
(symbol->string (cdr p)))))))
;; Void -> Hashtable<Line, (listof Point)>
(define (make-ht)
(make-hashtable pair-hash equal?))
;; some test entries
;; Hashtable<Line, (listof Point)>
(define __test-ht (make-hashtable pair-hash equal?))
(hashtable-cell __test-ht '(3 . 2) '('(5 . 1)))
(hashtable-cell __test-ht '(3 . 2) '('(5 . 2))) ;; stays '(5 . 1)
(hashtable-cell __test-ht '(1 . 0) '('(4 . 4)))
;; Hashtable<Line, (listof Point)> Line Point -> void | (cons Line Point)
;; associate point with line (points not ordered)
(define (add-point ht line pt)
(let [(current (hashtable-ref ht line #f))]
(if current
(hashtable-set! ht line (if (member pt current)
current
(cons pt current)))
(hashtable-cell ht line (list pt)))))
;; Point Point -> Line
;; produce Line (slope-intercept form) from two Points
(define (slope-intercept pt1 pt2)
(let [(Ξx (- (car pt2) (car pt1)))]
(if (zero? Ξx)
(cons (car pt1) 'undefined)
(cons (/ (- (cdr pt2) (cdr pt1)) Ξx)
(- (cdr pt1)
(* (/ (- (cdr pt2) (cdr pt1)) Ξx)
(car pt1)))))))
(test (slope-intercept '(2 . 2) '(3 . 4)) '(2 . -2))
(test (slope-intercept '(3 . 4) '(2 . 2)) '(2 . -2))
(test (slope-intercept '(2 . 2) '(1 . 0)) '(2 . -2))
;; Hashtable<Line, (listof Point)> (listof Point) -> Void
;; find slope-intercept line form between each point and every other
;; point in pts, and add them to hashtable ht
(define (add-points ht pts)
(for-each
(lambda (pt1)
(for-each
(lambda (pt2)
(if (equal? pt1 pt2)
(void)
(let [(line (slope-intercept pt1 pt2))]
(begin
(add-point ht line pt1)
(add-point ht line pt2)))))
pts))
pts))
;; Map is String
;; interp. a 2D map of asteroids; origin is top left
;; - #: asteroid
;; - .: not asteroid
;; following are examples from puzzle:
(define m1
".#..#
.....
#####
....#
...##")
;; same as above
(define ex1
(list '(1 . 0) '(4 . 0) '(0 . 2) '(1 . 2) '(2 . 2) '(3 . 2) '(4 . 2)
'(4 . 3) '(3 . 4) '(4 . 4)))
(test (map->points m1) ex1)
(define m2
"......#.#.
#..#.#....
..#######.
.#.#.###..
.#..#.....
..#....#.#
#..#....#.
.##.#..###
##...#..#.
.#....####")
(define m3
"#.#...#.#.
.###....#.
.#....#...
##.#.#.#.#
....#.#.#.
.##..###.#
..#...##..
..##....##
......#...
.####.###.")
(define m4
".#..#..###
####.###.#
....###.#.
..###.##.#
##.##.#.#.
....###..#
..#.#..#.#
#..#.#.###
.##...##.#
.....#.#..")
(define m5
".#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##")
;; Map -> (listof Point)
;; produce map from list of points
(define (map->points m)
(define (map->points x y acc chars)
(cond [(empty? chars) (reverse acc)]
[(char=? #\. (car chars))
(map->points (add1 x) y acc (cdr chars))]
[(char=? #\# (car chars))
(map->points (add1 x) y (cons (cons x y) acc) (cdr chars))]
[(char=? #\newline (car chars))
(map->points 0 (add1 y) acc (cdr chars))]))
(map->points 0 0 '() (string->list m)))
;; Point Point -> Boolean
;; <= comparison for two Points
(define (pair<= p1 p2)
(and (<= (car p1) (car p2))
(<= (cdr p1) (cdr p2))))
;; (listof Point) -> (listof Point)
;; sort pts by <=
(define (sort-pts pts)
(sort pair<= pts))
;; Hashtable<Line, (listof Point)> -> Point -> Integer
;; Produce thunk which calculates how many other asteroids (Points) visible
;; from its position, taking into account line of sight. For each two asteroids:
;; - 0 if no line from one to another asteroid
;; - 1 if on end of line of sight
;; - 2 otherwise, if somewhere else in same line
(define (asteroids-visible ht)
(let [(vals (map sort-pts (vector->list (hashtable-values ht))))]
(lambda (pt)
(apply + (map
(lambda (pts)
(cond [(not (member pt pts)) 0]
[(equal? pt (car pts)) 1]
[(equal? pt (car (reverse pts))) 1]
[else 2]))
vals)))))
;; Map -> Pair<Point, Integer>
;; produce pair which represents the asteroid from which most other asteroids
;; are visible, and the number of asteroids visible from it
(define (most-asteroids m)
(let [(ht (make-ht))
(pts (map->points m))]
(begin
(add-points ht pts)
(let [(fn (asteroids-visible ht))]
(car
(sort
(lambda (q r) (> (cdr q) (cdr r)))
(map cons pts (map fn pts))))))))
;; Solve Part 1
(let [(f (read-file "inputs/10.txt"))]
(display
(most-asteroids
(apply string-append
(map string-append f (map (lambda (_) "\n") (iota (length f))))))))