Last active
December 16, 2015 04:02
-
-
Save sbenhaim/8e13c63f759f383183c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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