summaryrefslogtreecommitdiff
path: root/8.scm
blob: f4025e8bb0862efdce5ec5737a427f6f749d28f4 (plain)
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
#!/usr/bin/env -S guile -s
!#
(use-modules (ice-9 regex)
             (ice-9 textual-ports)
             (srfi srfi-1)
             (srfi srfi-69))

(define (read-lines port)
  (letrec ((loop (lambda (l ls)
                   (if (eof-object? l)
                       ls
                       (loop (get-line port) (cons l ls))))))
    (reverse (loop (get-line port) '()))))

(define (parse-line line)
  (let ((m (string-match "([A-Z]{3}) = \\(([A-Z]{3}), ([A-Z]{3})\\)" line)))
    `(,(match:substring m 1) (,(match:substring m 2) ,(match:substring m 3)))))

(define (find-node tag tree)
  (car (filter (lambda (x) (equal? tag (car x))) tree)))

(let* ((port (open-input-file "8.txt"))
       (lines (read-lines port))
       (instructions (car lines))
       (tree (map parse-line (cddr lines)))
       (current-instruction 0)
       (current-node "AAA")
       (count 0))
  (close-port port)
  (while (not (equal? current-node "ZZZ"))
    (if (equal? (string-ref instructions current-instruction) #\L)
        (set! current-node (caadr (find-node current-node tree)))
        (set! current-node (cadadr (find-node current-node tree))))
    (set! current-instruction (modulo (1+ current-instruction) (string-length instructions)))
    (set! count (1+ count)))
  (display count))