Skip to content

Instantly share code, notes, and snippets.

@schmalz
schmalz / day-3.lisp
Created September 14, 2023 16:54
Advent of Code 2015; Day 3.
(defun next-house (house i)
(destructuring-bind (x y) house
(case i
(#\> (list (+ x 1) y))
(#\< (list (- x 1) y))
(#\^ (list x (+ y 1)))
(t (list x (- y 1))))))
(defun count-unique-deliveries-by-santa (file)
(with-open-file (stream file)
@schmalz
schmalz / day-2.lisp
Last active September 19, 2023 15:15
Advent of Code 2015; Day 2.
(defun box-dimensions (box-string)
"Given the dimensions of a box as a string in the form 'XxYxZ', return a
sequence containing the dimensions as integers X, Y and Z, sorted in
ascending order."
(sort (mapcar #'parse-integer
(str:split "x" box-string))
#'<))
(defun paper-for-box (box)
"Given the dimensions of BOX as integers sorted in ascending order, return
@schmalz
schmalz / day-1.lisp
Last active September 19, 2023 15:13
Advent of Code 2015; Day 1.
(defparameter *instruction->move*
'((#\( . 1+)
(#\) . 1-))
"Map instructions to floor movements.")
(defun all-floors (instructions)
"Return a sequence of all floors passed through when starting at floor 0 and
following the list of INSTRUCTIONS."
(reverse (reduce #'(lambda (acc i)
(cons (funcall (cdr (assoc i *instruction->move*))
@schmalz
schmalz / core.clj
Created February 3, 2023 19:44
Clojure implementation of the Blocks World Keyboard Exercise from Common Lisp: A Gentle Introduction to Symbolic Computing
(ns blocks-world.core)
(def ^:private database
"The blocks database."
[[:b1 :shape :brick]
[:b1 :color :green]
[:b1 :size :small]
[:b1 :supported-by :b2]
[:b1 :supported-by :b3]
[:b2 :shape :brick]
@schmalz
schmalz / core.clj
Created February 3, 2023 10:52
Clojure implementation of the Robbie Robot Keyboard Exercise from Common Lisp: A Gentle Introduction to Symbolic Computing
(ns robbie.core)
(def rooms
"Robbie's world."
{:living-room {:north :front-stairs
:south :dining-room
:east :kitchen}
:upstairs-bedroom {:west :library
:south :front-stairs}
:dining-room {:north :living-room
@schmalz
schmalz / core.clj
Created February 2, 2023 11:41
Clojure implementation of the Feature Comparison Mini-Exercise from Common Lisp: A Gentle Introduction to Symbolic Computing
(ns compare-descriptions.core
(:require [clojure.set :only intersection]))
(defn- right-side
"Given a list containing the descriptions of two objects separated by the
keyword :-vs-, return the description of the object to the right of the
separator."
[descriptions]
(rest (drop-while #(not= % :-vs-)
descriptions)))
@schmalz
schmalz / core.clj
Created December 16, 2022 19:54
Advent of Code 2015 - day 1.
(ns day-1.core)
(def input (slurp "input.txt"))
(def up \()
(defn where-is-santa
"What floor will Santa finish on after following INSTRUCTIONS?"
[instructions]
(reduce #(+ %1
@schmalz
schmalz / core.clj
Last active December 16, 2022 19:46
Advent of Code 2015 - day 2.
(ns day-2.core)
(defn box-dimensions
"Given the dimensions of a box in the form \"XxYxZ\", return a sorted
sequence of X, Y and Z."
[box]
(sort (map parse-long
(re-seq #"[0-9]+" box))))
(def boxes
@schmalz
schmalz / core.clj
Last active December 9, 2022 11:47
Advent of Code 2022, day 2
(ns day-2.core
(:require [clojure.string :as str]))
(def rounds
"Return a sequence of all the rounds of the game."
(str/split (slurp "input.txt")
#"\n"))
(defn plan-a
[round]
@schmalz
schmalz / core.clj
Last active August 4, 2023 16:01
Advent of Code 2022, day 1
(ns day-1.core
(:require [clojure.string :as string]))
(defn- calories-per-elf
"The amount of calories each elf is carrying, greatest value first."
[input-file-name]
(sort >
(map #(reduce +
(map parse-long
(re-seq #"[0-9]+" %)))