Skip to content

Instantly share code, notes, and snippets.

(ns cljproxy
(:require [org.httpkit.client :as client]
[org.httpkit.sni-client]
[org.httpkit.server :as server]))
;; minimalist local proxy for development
;; Usage: TARGET=https://erdos.dev PORT=8081 bb core.clj
(def target-root (doto (System/getenv "TARGET") assert))
(def port (doto (System/getenv "PORT") assert))
@erdos
erdos / aoc21_day8_2.clj
Created December 8, 2021 13:53
advent of code 2021 day 8 second part with clojure core logic
(ns aoc21-day8-2
(:require [clojure.core.logic :as logic]))
(def lines
(for [line (clojure.string/split-lines (slurp (clojure.java.io/resource "input.txt")))
:let [[data test] (clojure.string/split line #" \| ")]]
[(clojure.string/split data #" ") (clojure.string/split test #" ")]))
(defn- segments [xs digit & segments]
(logic/all (logic/member1o digit xs)
@erdos
erdos / aoc21_day18.clj
Created December 18, 2021 06:33
Advent Of Code 2021 Day 18 in Clojure with zippers
(require '[clojure.zip :as zip])
(def input (slurp "day18.txt"))
(def lines (vec (s/split-lines input)))
(def vecs (map read-string lines))
(defn magnitude [x]
(if (number? x)
x
(+ (* 3 (magnitude (first x))) (* 2 (magnitude (second x))))))
@erdos
erdos / SuffixArray.java
Created February 2, 2020 21:39
O(nlogn) suffix array algo
package dev.erdos.algo;
import java.util.Arrays;
public class SuffixArray {
// O(n*logn) algorithm for constructing a suffix array.
// Original source: https://cp-algorithms.com/string/suffix-array.html#toc-tgt-3
public static int[] calculateSuffixArray(char[] s) {
@erdos
erdos / zippers.clj
Created June 26, 2019 18:40
Budapest Clojure User Group 2019 June
; root
; / \ \
; 1 [2 3] [4 5]
; /\ /\
; 2 3 4 5
(require '[clojure.zip :as zip])
@erdos
erdos / hungarian.clj
Last active April 24, 2019 08:10
egesz szamok magyar nyelvu szoveges kiirasa
(ns hungarian
(:require [clojure.string]))
(defn- fmt [num]
(cond
(>= num 1000_000_000)
(let [szorzo (quot num 1000_000_000)
maradek (rem num 1000_000_000)]
(str (fmt szorzo) "milliárd" (when (pos? maradek) (str "-" (fmt maradek)))))
@erdos
erdos / tester.clj
Created September 28, 2018 13:55
make all private vars visible for unit testing
(let [target (the-ns 'testable-namespace)]
(doseq [[k v] (ns-map target)
:when (and (var? v) (= target (.ns v)))]
(eval `(defn ~(symbol (str "-" k)) [~'& args#] (apply (deref ~v) args#)))))
;; Now if testable-namespace has a function valled fun1
;; then i am able call -fun1 from the current namespace.
@erdos
erdos / document.xml
Created May 1, 2018 16:06
ooxml gridspan example
<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal" />
<w:rPr />
</w:pPr>
<w:r>
<w:rPr />
@erdos
erdos / rpn.clj
Created June 7, 2017 08:35
Simple Reverse Polish Notation Calculator in Clojure
(defn rpn [s]
(first (reduce (fn [[s1 s2 & ss :as stack] op]
(case op
+ (cons (+ s1 s2) ss)
- (cons (- s1 s2) ss)
* (cons (* s1 s2) ss)
/ (cons (/ s1 s2) ss)
, (cons op stack)))
() (read-string (str \( s \))))))
@erdos
erdos / lis.clj
Created June 6, 2017 17:01
Longest Increasing Subsequence in Clojure
(defn filt [sequences]
(mapv (partial apply min-key peek) (vals (group-by count sequences))))
(defn rfn [seqs x]
(filt (concat [[x]] seqs (for [cs seqs :when (> x (peek cs))] (conj cs x)))))
(defn longest-subseq [xs]
(apply max-key count [] (reduce rfn nil xs)))