Browse Source

solve day 13 part 2

Mistivia 1 year ago
parent
commit
2b3aa9edd9
3 changed files with 73 additions and 6 deletions
  1. 2 3
      08/1.rkt
  2. 2 3
      08/2.rkt
  3. 69 0
      13/2.rkt

+ 2 - 3
08/1.rkt

@@ -4,14 +4,13 @@
 
 (define lines
   (call-with-input-file "input"
-    (lambda (fp)
-      (get-lines fp))))
+    (λ (fp) (get-lines fp))))
 
 (define instructions (string->list (car lines)))
 
 (define (make-instruction-generator)
   (define cur instructions)
-  (lambda ()
+  (λ ()
     (when (null? cur)
       (set! cur instructions))
     (define ret (car cur))

+ 2 - 3
08/2.rkt

@@ -4,14 +4,13 @@
 
 (define lines
   (call-with-input-file "input"
-    (lambda (fp)
-      (get-lines fp))))
+    (λ (fp) (get-lines fp))))
 
 (define instructions (string->list (car lines)))
 
 (define (make-instruction-generator)
   (define cur instructions)
-  (lambda ()
+  (λ ()
     (when (null? cur)
       (set! cur instructions))
     (define ret (car cur))

+ 69 - 0
13/2.rkt

@@ -0,0 +1,69 @@
+#lang racket
+
+(require "../lib/utils.rkt")
+
+(define lines
+  (call-with-input-file "input"
+    (λ (fp) (get-lines fp))))
+
+(define patterns (split-list-by "" lines))
+
+(define (fix-smudge pattern x y)
+  (define new-pattern (map string-copy pattern))
+  (define pattern-vec (list->vector new-pattern))
+  (define c (string-ref (vector-ref pattern-vec y) x))
+  (case c
+    ((#\#) (string-set! (vector-ref pattern-vec y) x #\.))
+    ((#\.) (string-set! (vector-ref pattern-vec y) x #\#)))
+  (vector->list pattern-vec))
+
+(define (find-reflection lines)
+  (define lines-vec (list->vector lines))
+  (define len (vector-length lines-vec))
+  (define (find-first)
+    (let loop ((i 0) (ret '()))
+      (if (>= i (- len 1))
+          (reverse ret)
+          (if (string=? (vector-ref lines-vec i)
+                        (vector-ref lines-vec (+ i 1)))
+              (loop (+ 1 i) (cons i ret))
+              (loop (+ 1 i) ret)))))
+  (define (count start)
+    (let loop ((cnt 1) (i start))
+      (if (or (< i 0)
+              (>= (+ start cnt) len))
+          (+ start 1)
+          (if (string=? (vector-ref lines-vec i)
+                        (vector-ref lines-vec (+ start cnt)))
+              (loop (+ 1 cnt) (- i 1))
+              0))))
+  (define start (find-first))
+  (if (null? start)
+      '()
+      (filter (λ (x) (not (= 0 x)))
+              (map count start))))
+
+(define (score pattern)
+  (define h-score (map (λ (x) (* 100 x))(find-reflection pattern)))
+  (define w-score (find-reflection (transpose-list pattern)))
+  (filter (λ (x) (not (= 0 x)))
+          (append h-score w-score)))
+
+(define (fixed-score pattern)
+  (define original-score (car (score pattern)))
+  (define w (string-length (car pattern)))
+  (define h (length pattern))
+  (let loop ((x 0) (y 0))
+    (if (>= y h)
+        0
+        (if (>= x w)
+            (loop 0 (+ 1 y))
+            (let ()
+              (define s (score (fix-smudge pattern x y)))
+              (define result (filter (λ (x) (not (= original-score x)))
+                                     s))
+              (if (null? result)
+                  (loop (+ 1 x) y)
+                  (car result)))))))
+
+(apply + (map fixed-score patterns))