Skip to content

Instantly share code, notes, and snippets.

View tatut's full-sized avatar

Tatu Tarvainen tatut

View GitHub Profile
@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) ].
% clj -Sdeps "{:deps {juxt/crux-core {:mvn/version \"21.06-1.17.1-beta\"}}}"
Clojure 1.10.1
;; Require API and create noe
user=> (require '[crux.api :as crux])
nil
user=> (def crux (crux/start-node {})) ; start in-memory node
#'user/crux
;; Insert and query some data
@tatut
tatut / day2.sql
Last active December 2, 2020 14:20
adventofcode 2020, day 2 part 1
create table day2 (policy text, password text);
insert into day2 (policy,password)
values ('1-3 a','abcde'),
('1-3 b','cdefg'),
('2-9 c','ccccccccc');
CREATE OR REPLACE FUNCTION count_substring_matches(string TEXT, substr TEXT) RETURNS INTEGER AS $$
DECLARE
pos INTEGER;
c INTEGER;