Day 10: Part 1.
This commit is contained in:
parent
11b5812975
commit
9f614b8616
|
@ -0,0 +1,33 @@
|
||||||
|
.#..#..##.#...###.#............#.
|
||||||
|
.....#..........##..#..#####.#..#
|
||||||
|
#....#...#..#.......#...........#
|
||||||
|
.#....#....#....#.#...#.#.#.#....
|
||||||
|
..#..#.....#.......###.#.#.##....
|
||||||
|
...#.##.###..#....#........#..#.#
|
||||||
|
..#.##..#.#.#...##..........#...#
|
||||||
|
..#..#.......................#..#
|
||||||
|
...#..#.#...##.#...#.#..#.#......
|
||||||
|
......#......#.....#.............
|
||||||
|
.###..#.#..#...#..#.#.......##..#
|
||||||
|
.#...#.................###......#
|
||||||
|
#.#.......#..####.#..##.###.....#
|
||||||
|
.#.#..#.#...##.#.#..#..##.#.#.#..
|
||||||
|
##...#....#...#....##....#.#....#
|
||||||
|
......#..#......#.#.....##..#.#..
|
||||||
|
##.###.....#.#.###.#..#..#..###..
|
||||||
|
#...........#.#..#..#..#....#....
|
||||||
|
..........#.#.#..#.###...#.....#.
|
||||||
|
...#.###........##..#..##........
|
||||||
|
.###.....#.#.###...##.........#..
|
||||||
|
#.#...##.....#.#.........#..#.###
|
||||||
|
..##..##........#........#......#
|
||||||
|
..####......#...#..........#.#...
|
||||||
|
......##...##.#........#...##.##.
|
||||||
|
.#..###...#.......#........#....#
|
||||||
|
...##...#..#...#..#..#.#.#...#...
|
||||||
|
....#......#.#............##.....
|
||||||
|
#......####...#.....#...#......#.
|
||||||
|
...#............#...#..#.#.#..#.#
|
||||||
|
.#...#....###.####....#.#........
|
||||||
|
#.#...##...#.##...#....#.#..##.#.
|
||||||
|
.#....#.###..#..##.#.##...#.#..##
|
5
lib.rkt
5
lib.rkt
|
@ -12,6 +12,7 @@
|
||||||
sum
|
sum
|
||||||
neq?
|
neq?
|
||||||
nzero?
|
nzero?
|
||||||
|
negate
|
||||||
number->digits-reverse
|
number->digits-reverse
|
||||||
number->digits
|
number->digits
|
||||||
rac
|
rac
|
||||||
|
@ -50,6 +51,10 @@
|
||||||
(define (nzero? n)
|
(define (nzero? n)
|
||||||
(not (zero? n)))
|
(not (zero? n)))
|
||||||
|
|
||||||
|
;; negate : number -> number
|
||||||
|
(define (negate n)
|
||||||
|
(- 0 n))
|
||||||
|
|
||||||
;; number->digits-reverse : number -> (listof number)
|
;; number->digits-reverse : number -> (listof number)
|
||||||
;; Return the digits of the given number in reverse order (i.e. RTL)
|
;; Return the digits of the given number in reverse order (i.e. RTL)
|
||||||
(define (number->digits-reverse n)
|
(define (number->digits-reverse n)
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
#lang racket
|
||||||
|
|
||||||
|
(require "../lib.rkt")
|
||||||
|
|
||||||
|
(define input
|
||||||
|
(problem-input 10))
|
||||||
|
|
||||||
|
(define width (string-length (car input)))
|
||||||
|
(define height (length input))
|
||||||
|
|
||||||
|
(define (coprime? ij)
|
||||||
|
(= 1 (gcd (first ij)
|
||||||
|
(second ij))))
|
||||||
|
|
||||||
|
(define (offsets x y)
|
||||||
|
(define is
|
||||||
|
(range (negate x) (- width x)))
|
||||||
|
(define js
|
||||||
|
(range (negate y) (- height y)))
|
||||||
|
(filter coprime? (cartesian-product is js)))
|
||||||
|
|
||||||
|
(define (asteroid? xy)
|
||||||
|
(define row (list-ref input (second xy)))
|
||||||
|
(eq? #\# (string-ref row (first xy))))
|
||||||
|
|
||||||
|
(define (asteroid-offset? x y ij)
|
||||||
|
(define i (first ij))
|
||||||
|
(define j (second ij))
|
||||||
|
(define max-multiple
|
||||||
|
(let ([max-x
|
||||||
|
(cond
|
||||||
|
[(zero? i) width]
|
||||||
|
[(positive? i) (truncate (/ (- (sub1 width) x) i))]
|
||||||
|
[else (abs (truncate (/ x i)))])]
|
||||||
|
[max-y
|
||||||
|
(cond
|
||||||
|
[(zero? j) height]
|
||||||
|
[(positive? j) (truncate (/ (- (sub1 height) y) j))]
|
||||||
|
[else (abs (truncate (/ y j)))])])
|
||||||
|
(min max-x max-y)))
|
||||||
|
(ormap (λ (m) (asteroid? `(,(+ x (* m i))
|
||||||
|
,(+ y (* m j)))))
|
||||||
|
(range 1 (add1 max-multiple))))
|
||||||
|
|
||||||
|
(define (asteroids xy)
|
||||||
|
(define x (first xy))
|
||||||
|
(define y (second xy))
|
||||||
|
(count (curry asteroid-offset? x y) (offsets x y)))
|
||||||
|
|
||||||
|
(define-values (part1 location)
|
||||||
|
(let* ([cols (range width)]
|
||||||
|
[rows (range height)]
|
||||||
|
[locations (filter asteroid? (cartesian-product cols rows))]
|
||||||
|
[asteroid-counts (map asteroids locations)]
|
||||||
|
[maximum (apply max asteroid-counts)]
|
||||||
|
[index (index-of asteroid-counts maximum)]
|
||||||
|
[location (list-ref locations index)])
|
||||||
|
(values maximum location)))
|
||||||
|
|
||||||
|
(show-solution part1 #f)
|
Loading…
Reference in New Issue