Skip to content

Instantly share code, notes, and snippets.

(defn parse-int [s]
(Integer/parseInt s))
(defn parse [inst]
(let [[_ cmd sx sy ex ey] (re-find #"(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)" inst)
sx (parse-int sx)
sy (parse-int sy)
ex (parse-int ex)
ey (parse-int ey)]
[cmd [sx sy] [ex ey]]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(defn parse-int [s]
(Integer/parseInt s))
(defn get-coords [^long n [^long sx ^long sy] [^long ex ^long ey]]
(for [x (range sx (inc ex))
y (range sy (inc ey))]
(+ (long x) (* (long n) (long y)))))
(ns advent.a7
(:require [clojure.string :as str]
[instaparse.core :as insta]
[clojure.core.match :refer [match]]))
(def MAX (int (dec (Math/pow 2 16))))
(defn unsign [i]
(cond (< i 0) (+ MAX (inc i))
(> i MAX) (- (dec i) MAX)
(ns advent.a8
(:require [clojure.string :as str]
[instaparse.core :as insta]))
(def parser (insta/parser (slurp "a8.bnf")))
(def lines (str/split-lines (slurp "a8.txt")))
(defn nlits [l] (count (re-seq #"[^\s]" l)))
(ns a6
(:require [clojure.string :as str]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(defn parse-int [s]
(Integer/parseInt s))
(defn get-coords [^long n [^long sx ^long sy] [^long ex ^long ey]]
(ns advent.a9
(:require [clojure.string :as str]
[clojure.math.combinatorics :refer [permutations]]))
(def text "AlphaCentauri to Snowdin = 66
AlphaCentauri to Tambi = 28
AlphaCentauri to Faerun = 60
AlphaCentauri to Norrath = 34
AlphaCentauri to Straylight = 34
AlphaCentauri to Tristram = 3
(ns advent.a10)
(defn look-and-say [ls]
(flatten (for [l (partition-by identity ls)]
[(count l) (first l)])))
(defn str->ints [s]
(map #(Integer/parseInt (str %)) s))
(time
(ns advent.a13
(:require [clojure.string :as str]
[clojure.math.combinatorics :refer [permutations]]))
(defn parse [l]
(let [[_ subject sign value object]
(re-find #"(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)." l)]
[subject
object
(Integer/parseInt
(ns advent.a14
(:require [clojure.string :as str]))
(def rules "Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds.
Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds.
Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds.
Donner can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
Dasher can fly 11 km/s for 12 seconds, but then must rest for 125 seconds.
Comet can fly 21 km/s for 6 seconds, but then must rest for 121 seconds.
Blitzen can fly 18 km/s for 3 seconds, but then must rest for 50 seconds.
(ns advent.a15
(:require
[clojure.string :as str]
[clojure.core.logic :as l :refer [run run* fresh]]
[clojure.core.logic.fd :as fd]))
(def text "Sugar: capacity 3, durability 0, flavor 0, texture -3, calories 2
Sprinkles: capacity -3, durability 3, flavOr 0, texture 0, calories 9
Candy: capacity -1, durability 0, flavor 4, texture 0, calories 1
Chocolate: capacity 0, durability 0, flavor -2, texture 2, calories 8")