Skip to content

Instantly share code, notes, and snippets.

View christophejunke's full-sized avatar

chris christophejunke

  • France
  • 09:48 (UTC +02:00)
View GitHub Profile
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
/* PARAMETERS */
/* Upper bound of loop (inclusive) */
const unsigned int upper_bound = 12;
@christophejunke
christophejunke / fizzbuzz.pl
Last active December 31, 2018 21:32
FizzBuzz alternative
% tested with http://eclipseclp.org/
divisors_term(_, [3,5],"FizzBuzz").
divisors_term(_, [3], "Fizz").
divisors_term(_, [5], "Buzz").
divisors_term(N, [], N).
% first clause, always fails, but with side-effects
fizz_buzz :-
% backtrackably iterates N from 1 to 100 by 1
(defpackage :ev (:use :cl :optima :alexandria))
(in-package :ev)
(defstruct (meta (:constructor meta (form))) form)
(defun alist/augment (env namespace key value)
(acons namespace (acons key value (cdr (assoc namespace env))) env))
(defun alist/augment* (env namespace fresh-alist)
(acons namespace (nconc fresh-alist (cdr (assoc namespace env))) env))
(defpackage :o (:use :cl :bricabrac.sdl2.event-loop :sdl2))
(in-package :o)
(defgeneric handle-event (state event type)
(:method (state event type))
(:method (state event (type (eql :quit)))
(throw :quit :quit)))
(defun clock-ms ()
(round (* (osicat:get-monotonic-time) 1000)))
(in-package :advent.2019)
(defstruct (point (:conc-name) (:constructor point (x y))) x y)
(defstruct (segment (:conc-name)) from to)
(defstruct (hsegment (:constructor hsegment (from to)) (:include segment)))
(defstruct (vsegment (:constructor vsegment (from to)) (:include segment)))
(defun manhattan (p1 p2)
(+ (abs (- (x p2) (x p1)))
(abs (- (y p2) (y p1)))))
@christophejunke
christophejunke / day-03-alt.lisp
Last active December 4, 2019 17:44
day-03-alt.lisp
(defpackage :advent.2019.03-alt (:use :cl :alexandria :cl-ppcre))
(in-package :advent.2019.03-alt)
(defstruct seg lo hi o d z)
(defstruct (hseg (:constructor hseg (lo hi o d z)) (:include seg)))
(defstruct (vseg (:constructor vseg (lo hi o d z)) (:include seg)))
(defun seg (type from to z)
(funcall type
(min from to)
@christophejunke
christophejunke / day-10.lisp
Last active December 10, 2019 11:42
AoC 2019 Day 10
(defpackage :advent.2019.10 (:use :cl :alexandria))
(in-package :advent.2019.10)
;; (setf *default-pathname-defaults* #P"~/data/advent/2019/")
;; day 10 - part 1
(defun map-map (fn stream &aux (y 0) (x 0))
(flet ((callback () (funcall fn x y)))
(loop
(in-package :advent.2019.intcode)
;; utils
(defun make-command-queue (size)
(assert (plusp size))
(let* ((commands (make-list size))
(head commands))
(lambda (value callback)
(setf (car head) value)
(defpackage :advent.2019.14
(:use :cl :alexandria :cl-ppcre)
(:import-from :series #:collect #:map-fn #:scan-stream))
(in-package :advent.2019.14)
(defun parse-reactions (stream)
(labels ((reaction (line)
(destructuring-bind (in out) (split " => " line)
(mapcar #'compound (list* out (split ", " in)))))
@christophejunke
christophejunke / map-lines.lisp
Created May 16, 2020 13:46
Map lines over an internal buffer - the callback must copy the string if needed, otherwise the underlying data changes.
(defpackage :map-lines (:use :cl))
(in-package :map-lines)
(defparameter *max-line-size* 65536)
(defparameter *default-buffer-size* 16384)
(declaim (inline map-lines-on-shared-buffer))
(define-condition internal-buffer-overflow (error)
((buffer :reader internal-buffer-overflow/buffer :initarg :buffer)