Skip to content

Instantly share code, notes, and snippets.

@lvijay
lvijay / SelfModify.jsh
Created October 24, 2023 16:31
Self Modifying Java jshell script
// -*- mode: java; -*-
// Source code for the YouTube video: https://youtu.be/c4umDcNHBVs
// Start jshell in a GNU/Linux OS as follows (same instructions as
// the video)
/*
* jshell --startup SelfModify.jsh
*/
// After starting jshell,
@lvijay
lvijay / is_it_interesting.py
Created August 18, 2023 12:09
script to find a specific commit id in git magic
# Made for the YouTube video https://youtu.be/TFIxU_vOCR8
from itertools import count
import hashlib
import zlib
PREFIX = b'commit %d\x00'
COMMIT_TEMPLATE = b'tree 62ee7f49375b274e064e4277b10f044fca62f144\nparent e4fe1d2e9d8e123586da04c86cf74983ae399385\nauthor Vijay Lakshminarayanan <laksvij@hawk.iit.edu> 1597940579 +0530\ncommitter Vijay Lakshminarayanan <laksvij@hawk.iit.edu> 1597940579 +0530\n\ncommit #%d contains c0ffee\n'
@lvijay
lvijay / csvtosqlite.sh
Created June 9, 2023 09:34
save csv to sqlite db
function csv2sqlitedb() {
_FILENAME=$1
_TABLENAME=$2
echo "
.mode csv
.import \"${_FILENAME}\" ${_TABLENAME}
.save ${_TABLENAME}.db" | sqlite3
}
## Sample usage
@lvijay
lvijay / ffmpeg-recipes.sh
Last active August 18, 2023 11:40
Video generation for the Automated Solver Introduction Raw
## Generate a video from a sequence of images
ffmpeg -framerate 2 -pattern_type glob -i '*.PNG' -c:v libx264 -pix_fmt yuv420p out.mp4
# framerate -- each frame will show on screen for 1/framerate seconds (in this case 1/2 = 0.5s)
guilty :- true.
innocent :- false.
solve_inspector(Chase, Decker, Ellis, Heath, Mullaney):-
% each individual is either innocent or guilty
member(Chase, [guilty, innocent]),
member(Decker, [guilty, innocent]),
member(Ellis, [guilty, innocent]),
member(Heath, [guilty, innocent]),
member(Mullaney, [guilty, innocent]),
@lvijay
lvijay / bzp-trick-or-treat.pro
Last active May 1, 2022 16:59
Solve a Logic Grid Puzzle using Prolog
% The below program solves a Logic Grid Puzzle using Prolog.
% The puzzle is available at the following url:
% https://www.brainzilla.com/logic/logic-grid/trick-or-treat/
% License: Public Domain
/*
* Tested with SWI-Prolog.
* To run:
*
@lvijay
lvijay / Dispatch.java
Created May 13, 2021 04:57
Some Java puzzlers
public class Dispatch {
static class Fruit { public String taste = "---"; }
static class Melon extends Fruit { public String taste = "squishy"; }
static class Watermelon extends Melon { public String taste = "sweet"; }
public void describe(Object o) { print("object", "inedible."); }
public void describe(Fruit f) { print("fruit", f.taste); }
public void describe(Melon m) { print("melon", m.taste); }
public void describe(Watermelon w) { print("watermelon", w.taste); }
public static void main(String[] args) {
@lvijay
lvijay / 2021-05-07.py
Created May 7, 2021 10:58
New Scientist Puzzle date May 7, 2021, #112 A muse in barrels
## documentation pending
## tldr, there are 3 barrels on the floor, touching one another
## there's a board on the barrels that's at 60° from the horizontal
## the board touches all the barrels
## the first barrel has diameter 10
## what's the diameter of the 3rd barrel?
## this program draws the problem and solves it
## to run:
## python3 2021-05-07.py
def din(n):
if n:
rest()
else:
less()
## reset
def rest():
print("tridnt")
@lvijay
lvijay / SelfModify.java
Last active October 20, 2023 18:05
Program which modifies a String's content in RAM
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.file.Files.newBufferedReader;
import static java.nio.file.Files.newByteChannel;
import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;