Skip to content

Instantly share code, notes, and snippets.

View jreighley's full-sized avatar

Josh Reighley jreighley

  • Spokane Software
  • Spokane, WA
View GitHub Profile
@jreighley
jreighley / AOC2023d2.clj
Created December 2, 2023 07:10
Advent of Code day 2
(ns y2023.d2)
(def input (->> "input/2023/2.txt"
(slurp)))
(def cube-limits {:red 12 , :green 13, :blue 14})
(defn legal-game? [game]
(= cube-limits (merge-with max game cube-limits)))
(ns scratches.gapful)
(comment
Challenge from https://purelyfunctional.tv/issues/purelyfunctional-tv-newsletter-361-tip-trampoline-your-tail-recursion/
Some numbers are gapful, some aren't. 100 is gapful because it has at least three digits and 100 is divisible by 10 ((str 1 0), the concatenation of the first and last digit). That's the definition of gapful: it has at least three digits and is divisible by the number formed by concatenating the first and last digits.
Create a function that takes a number and finds the closest gapful number. The function should return its argument if the argument itself is gapful. And if there are two gapful numbers equidistant to the argument, return the lower {one.})
@jreighley
jreighley / fives.clj
Created February 26, 2019 08:20
Rule of five experiment
(ns fives.core
(:gen-class))
(defn make-collection [size max]
(for [n (range size)]
(rand-int max)))
(defn square [n]
(* n n))
@jreighley
jreighley / SoE.clj
Last active February 19, 2019 16:03
Sieve of Eratosthenes -- Clojure
(defn filter-factors [primes unfiltered]
(let [new-prime (first unfiltered)
new-primes (conj primes new-prime)
new-unfiltered (filter #(< 0 (mod % new-prime)) unfiltered)]
(if (zero? (count new-unfiltered))
new-primes
(recur new-primes new-unfiltered))))
(defn soe [n]
(filter-factors [] (range 2 (inc n))))
@jreighley
jreighley / gist:103de8202978c279e5e23efbcadfd6e4
Created November 10, 2018 00:10
Clojure EBCDIC to String
(defn ebcdic-to-string
"takes an EBCDIC byte array and returns the corresponding string"
[bytearray]
(String. bytearray "cp500"))
@jreighley
jreighley / gist:2acee67c179eba762844561115d865cd
Last active August 10, 2018 19:41
Clojure on the IBMi.
~~~~
(ns jtcompojure.ASconn
(:require [clojure.java.jdbc :as j])
(:gen-class)
(:import
(com.ibm.as400.access AS400ConnectionPool
CommandCall)))
;; I make a list of systems I am going to connnect to...
;; I can add keys for anything I would like that might be different amoungst system
(def syslist [{:name "SPOKANE" :iseries true :address "ipaddress" :dftlib "Libary"}]
@jreighley
jreighley / spec960.txt
Last active July 15, 2017 06:19
Clojure.spec Chess 960
(ns sssmakespec.c960
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]))
(def correct-pieces? #(= {\R 2, \K 1, \B 2, \Q 1, \N 2} (frequencies %))) ;; The right pieces and only the right peices
(def bishops-opposite? #(odd? (+ (.indexOf (vec %) \B) (.lastIndexOf (vec %) \B)))) ;; one bishop on even squares one on odd
(def K-between-R? #(< (.indexOf (vec %) \R) (.indexOf (vec %) \K) (.lastIndexOf (vec %) \R))) ;;King between the rooks
(s/def ::validc960 (s/and string?
correct-pieces?
@jreighley
jreighley / Clojurescript AWS Lambda Alexa skill starters
Last active April 7, 2017 23:52
Simple starter for Clojurescript AWS lambda app.
;;This is untested off the top of my head -- but should give you the gist..
;; use https://github.com/nervous-systems/cljs-lambda as a starter -- follow their lein build steps and then replace their core with something like this.
(ns alexalisten.core
(:require [cljs-lambda.util :as lambda]
[cljs-lambda.context :as ctx]
[cljs-lambda.macros :refer-macros [deflambda]]))
(deflambda alexa-listen [inputjson]
(let [jsonstring (js->clj inputjson)
@jreighley
jreighley / Dhqa-19.js
Last active September 26, 2016 21:04
JS c960 generator- https://repl.it/Dhqa/19
function c960seq (){
var emptySquares = new Set([1,2,3,4,5,6,7,8]);
var peiceMap = new Map()
var ob = (Math.floor(Math.random() * 4)+1)*2-1;
var eb = (Math.floor(Math.random() * 4)+1)*2;
function pickasquare(){
do{
tsq = (Math.floor(Math.random() * 8)+1);
}
while (emptySquares.has (tsq)===false);
@jreighley
jreighley / Nicer way to do c960
Created September 3, 2016 05:24
chess960 clojure revision
(ns c960)
(def pclist '(B B N N Q R K R))
(defn c960 [](let [c960seq (shuffle (range 1 9))
oddBish (first (filter odd? c960seq))
evenBish (first (filter even? c960seq))
goodseq (filterv #(not= oddBish %)
(filterv #(not= evenBish %)c960seq))]
(flatten (conj [evenBish oddBish] ;Bishop numbers go first
(take 3 goodseq) ; then Knights and Queens
(sort (take-last 3 goodseq)))))) ;then Rook King ;rook (sorted)