Skip to content

Latest commit

 

History

History
226 lines (216 loc) · 7.14 KB

2022.08.org

File metadata and controls

226 lines (216 loc) · 7.14 KB

Day 08

Executing this code

If you have a lisp installation, emacs, org-mode, and org-babel support for lisp installed you can run this by:

  1. Starting slime (M-x slime)
  2. Typing C-c C-c in the block initialize.
  3. In the repl type (in-package :aoc-2022-08)
  4. Typing C-c C-c in the block answers

Initial stuffs

Packages to load

(unless (find-package :cl-ppcre)
  (ql:quickload "cl-ppcre"))
(unless (find-package :parseq)
  (ql:quickload "parseq"))
(unless (find-package :lparallel)
  (ql:quickload "lparallel"))
(unless (find-package :fiveam)
  (ql:quickload "fiveam"))
(unless (find-package :series)
  (ql:quickload "series"))
(unless (find-package :cl-permutation)
  (ql:quickload "cl-permutation"))
(unless (find-package :bordeaux-threads)
  (ql:quickload "bordeaux-threads"))

Create package for this day

<<packages>>
(defpackage :aoc-2022-08
  (:use :common-lisp
        :parseq
        :fiveam)
  (:export :problem-a
           :problem-b))
(in-package :aoc-2022-08)

Input

(defun tree-map (tree-lines)
  (loop with trees = (make-hash-table)
        for line in tree-lines
        with columns = (length (first tree-lines))
        with rows = (length tree-lines)
        for i from 0
        do (loop for c across line
                 for j from 0
                 for coord = (complex i j)
                 do (setf (gethash coord trees) (- (char-code c) (char-code #\0))))
        finally (return (list rows columns trees))))

(defun process-stream (in)
  (loop for line = (read-line in nil)
        while line
        collect line))
(defun read-input (file)
  (with-open-file (in file)
    (tree-map (process-stream in))))
(defparameter *input*
  (read-input "input/08.txt"))

Part 1

(defun visible-trees (tree-map)
  (let ((visible (make-hash-table)))
    (destructuring-bind
        (rows columns trees) tree-map
      (loop for i from 0 below rows
            for limit = -1
            do (loop for j from 0 below columns
                     for coord = (complex i j)
                     for height = (gethash coord trees)
                     if (< limit height)
                       do (setf (gethash coord visible) height)
                          (setf limit height)))
      (loop for i from 0 below rows
            for limit = -1
            do (loop for j from (1- columns) downto 0
                     for coord = (complex i j)
                     for height = (gethash coord trees)
                     if (< limit height)
                       do (setf (gethash coord visible) height)
                          (setf limit height)))
      (loop for j from 0 below columns
            for limit = -1
            do (loop for i from 0 below rows
                     for coord = (complex i j)
                     for height = (gethash coord trees)
                     if (< limit height)
                       do (setf (gethash coord visible) height)
                          (setf limit height)))
      (loop for j from 0 below columns
            for limit = -1
            do (loop for i from (1- rows) downto 0
                     for coord = (complex i j)
                     for height = (gethash coord trees)
                     if (< limit height)
                       do (setf (gethash coord visible) height)
                          (setf limit height)))
    (values (hash-table-count visible) visible))))
(defun print-trees (tree-map)
  (destructuring-bind
      (rows columns trees) tree-map
    (loop for i from 0 below rows
          do (loop for j from 0 below columns
                   for coord = (complex i j)
                   do (format t "~A" (gethash coord trees #\Space)))
             (format t "~%"))))
(defun problem-a () (format t "Problem 08 A: ~a~%" (visible-trees *input*)))

Part 2

(defun scenic-score (tree-map tree)
  (destructuring-bind
      (rows columns trees) tree-map
    (let ((height (gethash tree trees)))
      (*
       (loop with i = (realpart tree)
             for j from (1+ (imagpart tree)) below columns
             for coord = (complex i j)
             for h = (gethash coord trees)
             count 1
             while (< h height))
       (loop with i = (realpart tree)
             for j from (1- (imagpart tree)) downto 0
             for coord = (complex i j)
             for h = (gethash coord trees)
             count 1
             while (< h height))
       (loop with j = (imagpart tree)
             for i from (1+ (realpart tree)) below rows
             for coord = (complex i j)
             for h = (gethash coord trees)
             count 1
             while (< h height))
       (loop with j = (imagpart tree)
             for i from (1- (realpart tree)) downto 0
             for coord = (complex i j)
             for h = (gethash coord trees)
             count 1
             while (< h height))))))

(defun max-scenic-score (tree-map)
  (destructuring-bind
      (rows columns trees) tree-map
    (declare (ignore trees))
    (loop for i from 0 below rows
          maximizing (loop for j from 0 below columns
                           for scenic = (scenic-score tree-map (complex i j))
                           maximizing (scenic-score tree-map (complex i j))))))



(defun problem-b () (format t "Problem 08 B: ~a~%" (max-scenic-score *input*)))

Putting it all together

<<read-input>>
<<input>>
<<initialize>>
<<structs>>
<<functions>>
<<input>>
<<problem-a>>
<<problem-b>>
(problem-a)
(problem-b)

Answer

Problem 08 A: 1823
Problem 08 B: 211680

Test Cases

(def-suite aoc.2022.08)
(in-suite aoc.2022.08)
(defparameter *sample-input*
  "30373
25512
65332
33549
35390")

(defparameter *sample*
  (with-input-from-string (in *sample-input*)
    (tree-map (process-stream in))))

(test part-1-sample
  (is (= 21 (visible-trees *sample*))))

(test scenic-score
  (is (= (scenic-score *sample* (complex 3 2)) 8))
  (is (= (scenic-score *sample* (complex 1 2)) 4)))

(run! 'aoc.2022.08)

Test Results

Running test suite AOC.2022.08
 Running test PART-1-SAMPLE .
 Running test SCENIC-SCORE ..
 Did 3 checks.
    Pass: 3 (100%)
    Skip: 0 ( 0%)
    Fail: 0 ( 0%)

Thoughts