7 votes

Day 15: Beacon Exclusion Zone

Today's problem description: https://adventofcode.com/2022/day/15

Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python with any of the "short names" listed in this page of supported languages):

<details>
<summary>Part 1</summary>

```python
Your code here.
```

</details>

2 comments

  1. bhrgunatha
    (edited )
    Link
    I struggled for hours :(. I suck so badly and my code is ugly. Part 1 (define P1-ROW 2000000) (define P2-MAX 4000000) (define (part-01 input [row P1-ROW]) (define report (map signed-integers...

    I struggled for hours :(. I suck so badly and my code is ugly.

    Part 1
    (define P1-ROW 2000000)
    (define P2-MAX 4000000)
    
    (define (part-01 input [row P1-ROW])
      (define report (map signed-integers input))
      (define ranges (merge-ranges (beacon-free report row)))
      (for/sum ([r (in-list (merge-ranges ranges))])
        (- (second r) (first r))))
    
    (define (beacon-free input row)
      (for/fold ([ranges null])
                ([report (in-list input)])
        (match-define (list sx sy) (take report 2))
        (match-define (list bx by) (drop report 2))
        (define m (manhattan (list sx sy) (list bx by)))
        (cond [(<= (- sy m) row (+ sy m))
               (define dx (- m (abs (- sy row))))
               (define range (list (- sx dx) (+ sx dx)))
               (cons range ranges)]
              [else ranges])))
    
    (define (merge-ranges ranges)
      (define ordered (sort ranges range<?))
      (for/fold ([current (first ordered)]
                 [merged null]
                 #:result (reverse (cons current merged)))
                ([r (in-list (rest ordered))])
        (cond [(overlap? current r)
               (values (list (min (first current) (first r))
                             (max (second current) (second r)))
                       merged)]
              [(< (second current) (first r))
               (values r (cons current merged))]
              [else
               (values current (cons r merged))])))
    
    (define (overlap? a b)
      (define-values (l r)
        (if (> (first a) (first b))
            (values b a)
            (values a b)))
      (and (<= (first l) (second r))
           (<= (first r) (second l))))
    
    (define (range<? a b)
      (match-define (list ax ay) a)
      (match-define (list bx by) b)
      (cond [(< ax bx) #t]
            [(> ax bx) #f]
            [(< ay by) #t]
            [else #f]))
    
    (define (manhattan a b)
      (match-define (list ax ay) a)
      (match-define (list bx by) b)
      (+ (abs (- ax bx)) (abs (- ay by))))
    
    Part 2

    The idea is to walk one step outside the perimeter of a particular scanner and test all if any of the other scanners can see it.

    (define (part-02 input [max-bound P2-MAX])
      (define report (map signed-integers input))
      (define sensors
        (for/list ([bs (in-list report)])
          (match-define (list sx sy) (take bs 2))
          (match-define (list bx by) (drop bs 2))
          (define m (manhattan (list sx sy) (list bx by)))
          (list sx sy m)))
      (define outie
        (extend-perimeter sensors max-bound))
      (and outie (+ (* P2-MAX (first outie)) (second outie))))
    
    (define (extend-perimeter sensors max-bound)
      (for*/fold ([found? #f])
                 ([s (in-list sensors)]
                  #:do [(match-define (list sx sy m) s)]
                  [dx (in-range (add1 m))]
                  #:break found?)
        (define dy (- (add1 m) dx))
        (for/or ([p (in-list (perimeter sx dx sy dy))]
                 #:when (in-bounds? p max-bound))
          ;; dx + dy is 1 step beyond the sensor
          (and (invisible? sensors p) p))))
    
    (define (perimeter sx dx sy dy)
      (list (list (+ sx dx) (- sy dy))  ; ne
            (list (- sx dx) (- sy dy))  ; nw
            (list (+ sx dx) (+ sy dy))  ; se
            (list (- sx dx) (+ sy dy))))
    
    ;; do other sensors see perimiter points
    (define (invisible? sensors p)
      (for/and ([s (in-list sensors)])
        (match-define (list sx sy m) s)
        (> (manhattan p (list sx sy)) m)))
    
    (define (in-bounds? p max-bound)
      (match-define (list x y) p)
      (and (<= 0 x max-bound)
           (<= 0 y max-bound)))
    

    this little thing helped me a lot

    ;; -10	----------▓--------------------------
    ;; -9	---------▓▓▓-------------------------
    ;; -8	--------▓▓▓▓▓------------------------
    ;; -7	-------▓▓▓▓▓▓▓-----------------------
    ;; -6	------▓▓▓▓▓▓▓▓▓-------------▓--------
    ;; -5	-----▓▓▓▓▓▓▓▓▓▓▓-----------▓▓▓-------
    ;; -4	----▓▓▓▓▓▓▓▓▓▓▓▓▓---------▓▓▓▓▓------
    ;; -3	---▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-------▓▓▓▓▓▓▓-----
    ;; -2	--▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-----▓▓▓▓▓▓▓▓▓----
    ;; -1	-▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-▓-▓▓▓▓▓▓▓▓▓▓▓---
    ;; 0	▓▓▓▓▓▓▓▓▓▓S▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓--
    ;; 1	-▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓S▓▓▓▓▓▓▓-
    ;; 2	--▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓S▓▓▓▓▓▓▓▓▓▓▓▓▓--
    ;; 3	---▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓SB▓▓▓▓▓▓▓▓▓▓---
    ;; 4	----▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓----
    ;; 5	-----▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-----
    ;; 6	------▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓------
    ;; 7	-------▓▓▓▓▓▓▓▓▓S▓▓▓▓▓▓▓S▓▓▓▓▓-------
    ;; 8	--------▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓------
    ;; 9	-------▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-----
    ;; 10	------▓▓▓▓B▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓----
    ;; 11	-----▓▓▓S▓▓▓▓▓▓▓▓▓▓▓▓▓-▓▓▓▓▓▓▓▓▓▓▓---
    ;; 12	------▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓--
    ;; 13	-------▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-
    ;; 14	-------▓▓▓▓▓▓▓▓▓▓▓▓▓S▓▓▓▓▓▓▓S▓▓▓▓▓▓▓▓
    ;; 15	------B▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-
    ;; 16	-----▓▓▓▓▓▓▓▓▓▓▓▓SB▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓--
    ;; 17	----▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓S▓▓▓▓▓▓▓▓▓▓B---
    ;; 18	---▓▓▓▓▓▓▓S▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓----
    ;; 19	----▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓-----
    ;; 20	-----▓▓▓▓▓▓▓▓▓▓▓▓▓S▓▓▓▓▓▓S▓▓▓▓▓▓-----
    ;; 21	------▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓------
    ;; 22	-------▓▓▓▓▓▓▓--▓▓▓▓▓▓▓▓▓▓▓▓▓B-------
    ;; 23	--------▓▓▓▓▓----▓▓▓--▓▓▓▓▓▓▓--------
    ;; 24	---------▓▓▓------▓----▓▓▓▓▓---------
    ;; 25	----------▓-------------▓▓▓----------
    ;; 26	-------------------------▓-----------
    

    In retrospect it wasn't at all funny that there was a gap in the middle of the sensors, although I chuckled a little at the time

    3 votes
  2. tomf
    Link
    No clue for part two.. but here's a really ugly part one that has been modified so you can run it without any helpers Part 1 =ARRAYFORMULA( ABS( MIN((--REGEXEXTRACT(A2:A33,"x=(-?\d+)"))-...

    No clue for part two.. but here's a really ugly part one that has been modified so you can run it without any helpers

    Part 1
    =ARRAYFORMULA(
       ABS(
        MIN((--REGEXEXTRACT(A2:A33,"x=(-?\d+)"))-
       (ABS((--REGEXEXTRACT(A2:A33,"x=(-?\d+)"))-
       (--REGEXEXTRACT(A2:A33,":.*x=(-?\d+)")))+
       ABS((--REGEXEXTRACT(A2:A33,"y=(-?\d+)"))-
       (--REGEXEXTRACT(A2:A33,":.*y=(-?\d+)")))-
       ABS((--REGEXEXTRACT(A2:A33,"y=(-?\d+)"))-2000000))))+
       MAX((--REGEXEXTRACT(A2:A33,"x=(-?\d+)"))+
       (ABS((--REGEXEXTRACT(A2:A33,"x=(-?\d+)"))-
       (--REGEXEXTRACT(A2:A33,":.*x=(-?\d+)")))+
       ABS((--REGEXEXTRACT(A2:A33,"y=(-?\d+)"))-
       (--REGEXEXTRACT(A2:A33,":.*y=(-?\d+)")))-
       ABS((--REGEXEXTRACT(A2:A33,"y=(-?\d+)"))-2000000))))
    
    1 vote