Skip to content

Instantly share code, notes, and snippets.

View tatut's full-sized avatar

Tatu Tarvainen tatut

View GitHub Profile
@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
@tatut
tatut / sort-natural.clj
Created January 27, 2022 06:11
A naive natural string sort
(defn sort-natural [names]
(sort-by
(fn [name]
(mapv (fn [s]
(if (every? #(Character/isDigit %) s)
(Long/parseLong s)
s))
(map second (re-seq #"(\d+|[^\d]+)" name))))
names))
;; (sort-natural ["mun dokkari 13.doc" "mun dokkari 2.doc"])
(ns day20
(:require [clojure.java.io :as io]))
(def input (-> "day20.txt" io/reader line-seq))
(def enhance-alg (vec (first input)))
(def initial-img
(into #{}
(mapcat identity)
(map-indexed
@tatut
tatut / aoc2021_day1.smalltalk
Created December 1, 2021 15:27
AoC2021, day1 using SmallTalk (using blocks, instead of OO)
testArray := #(199 200 208 210 200 207 240 269 260 263).
"day1 part 1"
part1 := [ :input |
| len prev incr |
incr := 0.
prev := input first.
len := input size.
2 to: len do: [ :i |
(prev < (input at: i)) ifTrue: [ incr := (incr + 1) ].