diff --git a/2019/10.rkt b/2019/10.rkt new file mode 100644 index 0000000..5436cb9 --- /dev/null +++ b/2019/10.rkt @@ -0,0 +1,76 @@ +#lang br +(require racket/file rackunit) + +(define (string->asteroids str) + (for*/list ([(row ridx) (in-indexed (string-split str))] + [(col cidx) (in-indexed row)] + #:when (char=? #\# col)) + (+ cidx (* +i ridx)))) + +(define ((count-visible roids) roid-origin) + (length (remove-duplicates (for/list ([roid (in-list roids)] + #:unless (= roid-origin roid)) + (angle (- roid roid-origin)))))) + +(define (find-best roids) + (argmax (count-visible roids) roids)) + +(check-equal? (find-best (string->asteroids "......#.#. +#..#.#.... +..#######. +.#.#.###.. +.#..#..... +..#....#.# +#..#....#. +.##.#..### +##...#..#. +.#....####")) 5+8i) + +(check-equal? (find-best (string->asteroids "#.#...#.#. +.###....#. +.#....#... +##.#.#.#.# +....#.#.#. +.##..###.# +..#...##.. +..##....## +......#... +.####.###.")) 1+2i) + +(check-equal? (find-best (string->asteroids ".#..#..### +####.###.# +....###.#. +..###.##.# +##.##.#.#. +....###..# +..#.#..#.# +#..#.#.### +.##...##.# +.....#.#..")) 6+3i) + +(check-equal? (find-best (string->asteroids ".#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..##")) 11+13i) + +;; 1 +(define roids (string->asteroids (file->string "10.rktd"))) +(check-eq? + ((count-visible roids) (find-best roids)) + 214) diff --git a/2019/10.rktd b/2019/10.rktd new file mode 100644 index 0000000..bb27111 --- /dev/null +++ b/2019/10.rktd @@ -0,0 +1,20 @@ +.###.###.###.#####.# +#####.##.###..###..# +.#...####.###.###### +######.###.####.#### +#####..###..######## +#.##.###########.#.# +##.###.######..#.#.# +.#.##.###.#.####.### +##..#.#.##.######### +###.#######.###..##. +###.###.##.##..####. +.##.####.##########. +#######.##.###.##### +#####.##..####.##### +##.#.#####.##.#.#..# +###########.#######. +#.##..#####.#####..# +#####..#####.###.### +####.#.############. +####.#.#.##########. \ No newline at end of file