diff --git a/golf/distinct-chars-and-distinct-sizes.rkt b/golf/distinct-chars-and-distinct-sizes.rkt new file mode 100644 index 0000000..15b1426 --- /dev/null +++ b/golf/distinct-chars-and-distinct-sizes.rkt @@ -0,0 +1,31 @@ +#lang racket +; https://codegolf.stackexchange.com/questions/277441/distinct-characters-and-distinct-sizes?answertab=modifieddesc#tab-top + +(define solve +(λ(s'set-count,list->set)[apply +',s{set->list,(map length [group-by(λ(x)x){sort s <}])}]) +) + +(define tests + (hash + "" 0 + "a" 2 + "aa" 3 + "aaa" 4 + "ab" 3 + "aab" 5 + "aaab" 6 + "abab" 4 + "abc" 4 + "abccbcb" 7 + "abaccac" 7 + "abcaaccac" 8 + "emmmmmmmm" 11 + "abcc" 6 + "aabcc" 6 + "aabbcc" 5)) + +(define ez-solve (compose solve bytes->list string->bytes/utf-8)) + +(require rackunit) +(for ([(in out) tests]) + (check-equal? (ez-solve in) out in)) diff --git a/png.lisp b/png.lisp new file mode 100644 index 0000000..eec8b31 --- /dev/null +++ b/png.lisp @@ -0,0 +1,26 @@ +(ql:quickload "flexi-streams") + +(defpackage :mehpng + (:use :common-lisp :flexi-streams)) +(in-package :mehpng) +(load "utils.lisp") + +(defmacro hexbytes (&rest bytes) + (map '(vector flex:octet) (λ (x) (reread "#x" x)) bytes)) + +(defparameter signature + (hexbytes 89 50 4E 47 0D 0A 1A 0A)) + +(defgeneric bmatch (obj stream) + (:documentation "attempt to match OBJ with the binary input stream STREAM. +returns (VALUES BOOLEAN T)")) + +(defmethod bmatch ((obj vector) stream) + (values + (block nil + (loop for exp across signature + for got = (read-byte stream nil nil) + do (unless (and got (= exp got)) + (return))) + t) + nil)) diff --git a/utils.lisp b/utils.lisp index f686448..ea8e9f9 100644 --- a/utils.lisp +++ b/utils.lisp @@ -1,5 +1,6 @@ ;;; fun (proclaim '(inline last1 single append1 conc1)) +;; TODO: packagify and such ;; has a lot of goodies ;; hey whaddayaknow, pg has a with-gensyms