Skip to content

Instantly share code, notes, and snippets.

View tatut's full-sized avatar

Tatu Tarvainen tatut

View GitHub Profile
@tatut
tatut / day4.c
Created December 4, 2025 14:31
AoC 2025, day 4
#include "aoc.h"
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
typedef struct Grid {
int w, h;
char *data;
} Grid;
char grid_at(Grid *g, int x, int y) {
@tatut
tatut / day3.c
Created December 3, 2025 16:22
AoC 2025, day 3
#include "aoc.h"
int joltage(char *in, char *end) {
int sum = 0;
while(in < end) {
int a = -1, b = -1;
// naively go through from here to end of line
char *end = strchr(in, '\n');
int max = end - in;
@tatut
tatut / day2.c
Created December 2, 2025 14:09
AoC 2025, day2
#include "aoc.h" // common libc includes and reading input file to memory
bool is_invalid_p1(long num) {
char buf[16];
size_t len = snprintf(buf, 16, "%ld", num);
if(len % 2) return false; // not even length
int hl = len/2;
for(int i=0;i<hl;i++) {
if(buf[i] != buf[i+hl]) return false;
}
@tatut
tatut / day1.c
Created December 1, 2025 15:19
AoC 2025, day1
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
int count(char *in, void (*rotate)(int*, int*, int)) {
int count = 0;
int current = 50;
while(*in == 'R' || *in == 'L') {
int dx = *in == 'R' ? 1 : -1;
@tatut
tatut / trie.c
Created August 15, 2025 10:46
Trie in C
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <ctype.h>
@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),