Skip to content

Instantly share code, notes, and snippets.

View emdeesee's full-sized avatar
🤔
hmm

Michael Cornelius emdeesee

🤔
hmm
View GitHub Profile
@emdeesee
emdeesee / day3.lisp
Created December 3, 2024 18:02
AoC2024 Day 3
(defvar *input*
(uiop:read-file-string "day3.txt"))
(defun eval-muls (blob)
(let ((sum 0))
(ppcre:do-register-groups ((#'parse-integer i) (#'parse-integer j))
("mul\\(([0-9]+),([0-9]+)\\)" blob sum)
(incf sum (* i j)))))
(defun eval-muls* (blob)
@emdeesee
emdeesee / day2.lisp
Last active December 3, 2024 18:05
AoC 2024 Day 2
(defun safep (levels)
(loop for (a b) on levels by #'cdr
with sign = (signum (- (cadr levels) (car levels)))
do
(if (null b)
(return t)
(let ((delta (- b a)))
(unless (and (= sign (signum delta))
(<= 1 (abs delta) 3))
(return nil))))))
@emdeesee
emdeesee / weighted-random.el
Created April 17, 2020 18:15
Weighted random selection in elisp
;; In this population, a should be selected with frequency of 10%, while
;; b and c are each chosen with frequency 45%.
(defvar weights '(10 45 45))
(defvar population '(a b c))
(defun cumulative-weights (weights)
"Return the total of `weights' and the cumulative distribution
table."
(loop for w in weights
sum w into total
@emdeesee
emdeesee / random-float.el
Created April 17, 2020 16:46
Generate a random value in [0.0, 1.0)
(defun random-0-to-1 (&optional range)
"Generate a random value in [0.0, 1.0)."
(let ((range (or range most-positive-fixnum)))
(/ (random range) (float range))))
@emdeesee
emdeesee / name-tools.lisp
Last active March 21, 2020 22:54
utilities for scraping/parsing
(ql:quickload :str)
(ql:quickload :alexandria)
(import 'alexandria:compose)
(ql:quickload :arrows)
(use-package :arrows)
(defun split-names (s)
(mapcar (compose #'str:downcase #'str:trim) (str:split #\, s)))
(let (names)
@emdeesee
emdeesee / qdrink.lisp
Created November 1, 2019 19:24
Splitting the universe to choose a drink
(ql:quickload :jsown)
(ql:quickload :dexador)
(ql:quickload :quri)
(if (evenp
(sxhash
(car (jsown:val
(jsown:parse
(dex:get
(quri:make-uri
@emdeesee
emdeesee / debounce.tcl
Last active April 12, 2019 20:52
Why is it so hard to get a debounced key sequence in TCL/Tk?
set pending_release {}
set pressed false
proc press_action {} { puts "pressed" }
proc release_action {} { puts "released" }
proc is_pressed {} {
global pressed
return $pressed
}
@emdeesee
emdeesee / forecast.lisp
Created January 3, 2019 21:51
Get weather forecast from NWS
(ql:quickload :dexador)
(ql:quickload :jsown)
(ql:quickload :arrows)
(use-package :arrows)
(defparameter url "https://api.weather.gov/gridpoints/FWD/95,141/forecast")
(defun json-val-in (o &rest keys)
(reduce (lambda (o k) (jsown:val o k)) keys :initial-value o))
@emdeesee
emdeesee / fb.lisp
Created September 25, 2018 17:47
Inside out FizzBuzz in Common Lisp
(flet ((cycle (list) (setf (cdr (last list)) list)))
(let ((fcyc (cycle (list "" "" "Fizz")))
(bcyc (cycle (list "" "" "" "" "Buzz"))))
(mapcar (lambda (f b n &aux (fb (concatenate 'string f b)))
(if (= 0 (length fb)) n fb))
fcyc bcyc (loop for n from 1 to 100 collect n))))
@emdeesee
emdeesee / chainmap.lisp
Last active January 5, 2019 00:15
Functionality like Python's ChainMap in Common Lisp
(defun make-chain-table (&rest tables)
(lambda (key)
(labels ((aux (tables)
(if (null tables)
(values nil nil)
(multiple-value-bind (value found) (gethash key (car tables))
(if found
(values value t)
(aux (cdr tables)))))))
(aux tables))))