aoc2021-day03a Common Lisp/SBCL program
source code
;; SPDX-License-Identifier: LGPL-3.0-or-later
;; Copyright (C) 2021 Massimo Zaniboni <mzan@dokmelody.org>
;; WARNING: I'm learning CL
(ql:quickload :trivia) ;; common macro and functions and optimal pattern matching
(ql:quickload :alexandria) ;; common CL extensions
(ql:quickload :trivial-types) ;; common types
(ql:quickload :defstar) ;; add type annotations
(ql:quickload :str) ;; Common string manipulation functions
(ql:quickload :parse-float)
(ql:quickload :iterate)
(ql:quickload :let-plus) ;; extend "let"
(ql:quickload :array-operations) ;; rich management of arrays
(defpackage :main
(:import-from :alexandria)
(:import-from :trivial-types :proper-list :tuple)
(:use :cl :defstar :trivia :parse-float :iterate :let-plus)
(:export main))
(in-package :main)
(declaim (optimize (speed 3) (debug 0) (safety 0)))
(defun grow-vector-upto (vect new-size value)
(iter (for i from (fill-pointer vect) below new-size)
(vector-push-extend value vect)))
; # Day 3a
(defun count-bits->gamma-epsilon (max-count count-bits)
(iter (for cb in-vector count-bits downto 0)
(for e first 1 then (* e 2))
(with mc = (/ max-count 2))
(with gamma = 0)
(with epsilon = 0)
(after-each
(cond
((> cb mc) (setf gamma (+ gamma e)))
(t (setf epsilon (+ epsilon e)))))
(finally (return (values (* gamma epsilon) gamma epsilon)))))
(defun day3a ()
(iter (for line in-stream *standard-input* using #'read-line)
(count line into count-lines)
(with count-bits = (make-array 15 :fill-pointer 0 :adjustable t :element-type 'fixnum))
(if-first-time (grow-vector-upto count-bits (length line) 0))
(after-each
(iter (for c in-string line)
(for i from 0 )
(when (char= c #\1) (incf (elt count-bits i)))))
(finally
(return (count-bits->gamma-epsilon count-lines count-bits)))))
(defun main () (format t "~a~%" (day3a)))
notes, command-line, and program output
NOTES:
Linux
Sun, 23 Jan 2022 12:52:04 GMT
MAKE:
sbcl --disable-debugger --load "aoc2021_day03a.lisp-1.lisp" --eval "(sb-ext:save-lisp-and-die \"app_lisp\" :executable t :toplevel #'main:main)"
This is SBCL 2.1.11, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
To load "trivia":
Load 1 ASDF system:
trivia
; Loading "trivia"
To load "alexandria":
Load 1 ASDF system:
alexandria
; Loading "alexandria"
To load "trivial-types":
Load 1 ASDF system:
trivial-types
; Loading "trivial-types"
To load "defstar":
Load 1 ASDF system:
defstar
; Loading "defstar"
To load "str":
Load 1 ASDF system:
str
; Loading "str"
...
To load "parse-float":
Load 1 ASDF system:
parse-float
; Loading "parse-float"
To load "iterate":
Load 1 ASDF system:
iterate
; Loading "iterate"
To load "let-plus":
Load 1 ASDF system:
let-plus
; Loading "let-plus"
To load "array-operations":
Load 1 ASDF system:
array-operations
; Loading "array-operations"
[undoing binding stack and other enclosing state... done]
[performing final GC... done]
[defragmenting immobile space... (fin,inst,fdefn,code,sym)=1172+996+19861+20291+25972... done]
[saving current Lisp image into app_lisp:
writing 0 bytes from the read-only space at 0x50000000
writing 736 bytes from the static space at 0x50100000
writing 48136192 bytes from the dynamic space at 0x1000000000
writing 2080768 bytes from the immobile space at 0x50200000
writing 13066240 bytes from the immobile space at 0x52a00000
done]
2.55s to complete and log all make actions
COMMAND LINE:
./app_lisp 0 < aoc2021_day03a-input100000.txt
PROGRAM OUTPUT:
386463119445733053722557199393548794069517420395751036911156