Skip to content

Instantly share code, notes, and snippets.

View tatut's full-sized avatar

Tatu Tarvainen tatut

View GitHub Profile
@tatut
tatut / deps.edn
Created February 28, 2025 11:53
Using C code from Clojure via Chicory (WebAssembly)
{:paths ["."]
:deps {com.dylibso.chicory/runtime {:mvn/version "1.1.0"}}}
@tatut
tatut / day1.sql
Created December 1, 2024 14:22
AoC2024, day1 PostgreSQL 17
WITH
lines AS (
SELECT regexp_split_to_array(line,' ') AS ns
FROM regexp_split_to_table(pg_read_file('/tmp/day1.txt'),'\n') line),
nums AS (
SELECT ns[1]::integer as l, ns[2]::integer as r FROM lines),
lefts AS (
SELECT l as n, row_number() over(order by l) as pos FROM nums),
rights AS (
SELECT r as n, row_number() over(order by r) as pos FROM nums)
@tatut
tatut / day1.elf
Created December 1, 2024 08:26
AoC2024 day1, elf
nums: "aoc2024/day1.txt" lines map({ [&read, " ", &read] match($) }),
left: nums map(&_0) sort,
right: nums map(&_1) sort,
dif: 0 ref,
0 to(left len dec) do({
d: (left nth($) - right nth($)) abs,
dif swap({$ + d})
}),
"Part1: %d" fmt(dif val) print,
@tatut
tatut / WebUI.st
Created January 9, 2024 19:17
WebUI FFI bindings
FFIStructure subclass: #WuEvent
instanceVariableNames: ''
classVariableNames: 'OFFSET_BIND_ID OFFSET_ELEMENT OFFSET_EVENT_NUMBER OFFSET_EVENT_TYPE OFFSET_WINDOW'
package: 'WebUI'!
!WuEvent methodsFor: 'accessing - structure variables' stamp: 'UFFIGenerator 1/4/2024 13:07'!
element: anObject
"This method was automatically generated"
handle pointerAt: OFFSET_ELEMENT put: anObject getHandle.! !
%% Instead of JSON, encode point of interest data
%% as: input([poi(N1, X1, Y1, Z1), ... poi(Nn, Xn, Yn, Zn)]) where Z is the elevation
input([poi('Metsäjärvi', 23, 56, 20),
poi('Tunturikylä', 78, 12, 120),
poi('Sinijärvi', 45, 89, 21),
poi('Kallioranta', 34, 67, 19),
poi('Karhunpää', 91, 23, 66),
poi('Kuusimetsä', 17, 43, 66),
poi('Aurinkoniemi', 62, 78, 18),
@tatut
tatut / day5.pl
Created October 17, 2023 15:45
AoC 2022, dqy 5 prolog implementation (SWI-Prolog)
:- use_module(library(dcg/basics)).
:- use_module(library(yall)).
:- set_prolog_flag(double_quotes, codes).
crates(_, []) --> [].
crates(Ind, Cs) --> " ", { Ind1 is Ind + 1 }, crates(Ind1, Cs).
crates(Ind, [Ind-C|Cs]) --> "[", [Code], "] ", { char_code(C, Code) },
{ Ind1 is Ind + 1},
crates(Ind1, Cs).
@tatut
tatut / asemat.pl
Created October 12, 2023 11:52
contiguous check
contiguous([_]).
contiguous([[_,E1],[E1,E2]|Segments]) :-
contiguous([[E1,E2]|Segments]).
% contiguous([[1,3],[3,10],[10,123]]). succeeds
% contiguous([[1,3],[420,666]]). fails
@tatut
tatut / index.html
Created April 27, 2023 15:08
Definite Clause Grammars, toy Logo interpreter
<!DOCTYPE>
<html>
<head>
<script src="swipl-bundle.js"></script>
<script type="text/prolog">
:- use_module(library(dcg/basics)).
:- set_prolog_flag(double_quotes, chars).
turtle([]) --> []. % the empty program
turtle([Cmd|Cmds]) --> blanks, turtle_command(Cmd), blanks, turtle(Cmds).
(ns app.mappy
#?(:cljs (:require-macros app.mappy))
(:require #?(:clj [datascript.core :as d]) ; database on server
#?(:clj [clojure.data.csv :as csv])
[hyperfiddle.electric :as e]
[hyperfiddle.electric-dom2 :as dom]
[hyperfiddle.electric-ui4 :as ui]
#?(:cljs ["@openlayers-elements/core/ol-map" :as ol-map])
#?(:cljs ["@openlayers-elements/maps/ol-layer-openstreetmap" :as ol-layer-openstreetmap])
#?(:cljs ["@openlayers-elements/core/ol-layer-vector" :as ol-layer-vector])
@tatut
tatut / inspect.clj
Created January 18, 2023 17:14
hack cider orchard.inspect/render-indexed-values to render a table
(defn render-indexed-values
([inspector obj] (render-indexed-values inspector obj 0))
([inspector obj idx-starts-from]
(if (every? map? obj)
;; Print table instead
(let [[_ header1 header2 & rows] (clojure.string/split-lines
(with-out-str
(clojure.pprint/print-table obj)))]
(loop [{counter :counter :as ins} (render-ln inspector header1 header2)
obj obj