-
-
Save justinabrahms/d9d110c55ad5f53288a56d0cc882aa1e 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
(require "fiveam") | |
(require "log4cl") | |
(use-package :iterate) | |
(declaim (optimize (debug 3))) | |
(defpackage :aoc/three | |
(:use #:cl) | |
(:import-from #:fiveam | |
#:def-suite | |
#:def-suite* | |
#:test-case | |
#:is | |
#:run! | |
#:test | |
#:test-result) | |
(:import-from #:log4cl | |
#:log-info)) | |
(in-package :aoc/three) | |
(defun string-to-int-list (s) | |
(iter (for i from 0 to (- (length s) 1)) | |
(collect (parse-integer (string (char s i)))))) | |
;; Okay, so (apply 'mapcar #'list sequence) is what I want, I just don't | |
;; understand *why* it works. | |
(defun detector (comparator) | |
(lambda (x) (if (funcall comparator (count 1 x) (count 0 x)) 1 0))) | |
(defun gamma (x) (funcall (detector '>) x)) | |
(defun epsilon (x) (funcall (detector '<) x)) | |
(defun num-list-to-binary (l) | |
(parse-integer (format nil "~{~a~}" l) :radix 2)) | |
(defun power-consumption (numbers) | |
(let* ( | |
;; convert a list of abc def to three lists of ad, be, cf | |
(groups-by-column (apply 'mapcar #'list (mapcar 'string-to-int-list numbers))) | |
(gamma-list (mapcar 'gamma groups-by-column)) | |
(gamma-num (num-list-to-binary gamma-list)) | |
(epsilon-list (mapcar 'epsilon groups-by-column)) | |
(epsilon-num (num-list-to-binary epsilon-list)) | |
) | |
(progn | |
(log-info "group: " groups-by-column) | |
(log-info "gamma: " gamma-num) | |
(log-info "epsilon: " epsilon-num)) | |
(* gamma-num epsilon-num) | |
) | |
) | |
(def-suite* :aoc/three) | |
(test example | |
(let ((diagnostics '( | |
"00100" | |
"11110" | |
"10110" | |
"10111" | |
"10101" | |
"01111" | |
"00111" | |
"11100" | |
"10000" | |
"11001" | |
"00010" | |
"01010" | |
))) | |
(is (equal 198 (power-consumption diagnostics))))) | |
(run! :aoc/three) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment