Skip to content

Instantly share code, notes, and snippets.

@sbenhaim
Last active December 16, 2015 04:02
Show Gist options
  • Save sbenhaim/8e13c63f759f383183c3 to your computer and use it in GitHub Desktop.
Save sbenhaim/8e13c63f759f383183c3 to your computer and use it in GitHub Desktop.
(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)))
(defn nchars [l]
(->> (insta/parse parser l)
flatten
(filter #(= :char %))
count))
(apply + (for [l lines] (- (nlits l) (nchars l))))
;; This is so cheap!
(def vals {:dq 2
:hex 3
:esq 4
:ess 4
:char 0
:c 1})
(defn ncodes [l]
(->> (insta/parse parser l)
flatten
(filter keyword?)
rest
(map vals)
(reduce +)
(+ 2)))
(apply + (for [l lines] (- (ncodes l) (nlits l))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment