Skip to content

Instantly share code, notes, and snippets.

View akkartik's full-sized avatar

Kartik Agaram akkartik

View GitHub Profile
@akkartik
akkartik / code.st
Last active September 24, 2021 20:14
Conway's Game of Life in Smalltalk (Glamorous Toolkit)
"Hopefully I copied it correctly from my image and CodeChanges."
Object subclass: #GameOfLife
instanceVariableNames: 'grid grid1 grid2 side'
classVariableNames: ''
package: 'KartikAgaram'.
GameOfLife >> initialize
side := 10.
grid1 := (Array2D rows: side columns: side element: 0).
@akkartik
akkartik / x0.c
Created January 12, 2021 06:14
Adding a gap buffer to a text editor
#include <SDL2/SDL.h>
#include <stdio.h>
int error(char *msg, const char *err) {
printf("Error %s: %s\n", msg, err);
return 0;
}
#define HOR 32
#define VER 16
@akkartik
akkartik / 3-1.py
Last active December 17, 2019 18:39
import sys
def parse(inst):
dir, mag = inst[0], int(inst[1:])
if dir == 'U': return (0, -mag)
if dir == 'D': return (0, mag)
if dir == 'L': return (-mag, 0)
if dir == 'R': return (mag, 0)
raise 'bad inst {}'.format(inst)
@akkartik
akkartik / 3-1.arc
Last active December 14, 2019 23:02
Advent 2019, day 3, problem 1
(def parse (inst)
(with (dir inst.0
mag (int (cut inst 1)))
(case dir
#\U (list 0 (* -1 mag))
#\D (list 0 mag)
#\L (list (* -1 mag) 0)
#\R (list mag 0))))
(def add (a b)
@akkartik
akkartik / x.c
Created May 7, 2019 17:53
Stride through the heap until segfault
// Linux only.
// gcc x.c && ./a.out
//
// Example run on a 64-bit system:
// 0x56339fd78000 0x56339f74601a
// 0x56339fd78000 0x56339f74601b
// 0x56339fd78000 0x56339f74601c
// 0x56339fd78000 0x56339f74601d
// 0x56339fd78000 0x56339f74601e
// ...
(mac every (n . body)
`(thread
(while t
(sleep ,n)
,@body)))
;; try it out
; print a heartbeat every second
(every 1 (prn "."))
@akkartik
akkartik / x.s
Created August 29, 2018 06:36
attempt to read argc
; attempt to read argc
;
; Build and run on 32-bit Linux:
; $ nasm -f elf x.s
; $ gcc -Wall -s -nostdlib x.o -o x
; $ ./x; echo $?
; 77 # expected: 1
; $ ./x abc; echo $?
; 73 # expected: 2
; $ ./x abc def; echo $?
@akkartik
akkartik / session.md
Last active July 25, 2018 04:02
SubX online help, 2018-07-20

$ subx

Welcome to SubX, a better way to program in machine code.
SubX uses a subset of the x86 instruction set. SubX programs will run without modification on Linux computers.
It provides a better experience and better error messages than programming directly in machine code, but you have to stick to the instructions it supports.

== Ways to invoke subx
- Run tests:
    subx test
- See this message:
@akkartik
akkartik / commandline.sh
Last active March 7, 2017 02:45
Script to emit lines prefixed by line length
cat *.cc |line-length.pl |sort -nr |less
@akkartik
akkartik / ide
Created February 17, 2017 23:17
Structuring tmux usage
#!/bin/bash
#
# Use the regular 'tmux' command and '.tmux.conf' to run tmux for managing
# multiple sessions running long-running commands that you might need to
# detach from to go offline.
#
# Use this script to manage a special hard-coded session called 'ide' with
# editor and shell windows configured how you want it.
if tmux has-session -t ide >&/dev/null