Skip to content

Instantly share code, notes, and snippets.

View cashlo's full-sized avatar

Cash TingHin Lo cashlo

  • Indeed.com
  • Tokyo
View GitHub Profile
@cashlo
cashlo / gist:da518b185f577641b969af075ae7a21c
Last active July 31, 2022 17:24
Floyd-Steinberg Dithering for Kotlin Android
fun dithering(bitmap: Bitmap) {
val height: Int = bitmap.getHeight()
val width: Int = bitmap.getWidth()
val colors = 3
val threshold = 255/colors
for (x in 0 until width) {
for (y in 0 until height) {
val pixel = bitmap.getPixel(x, y)
var alpha = Color.alpha(pixel)
@cashlo
cashlo / balance_bug.ino
Created October 5, 2020 20:56
M5Stack BugC self-balance bot
#include <M5StickC.h>
#include "bugC.h"
#define sampleTime 0.005
float pitch = 0.0F;
float roll = 0.0F;
float yaw = 0.0F;
float roll_offset = 0.0F;
@cashlo
cashlo / dwarf_fortress_height_map.scad
Created June 21, 2020 20:52
Generate model from Dwarf Fortress elevation map
difference(){
scale([1,1,0.3]){
surface("YOUR FILE NAME GOES HERE", center=true);
}
cube([200,200,14], center=true);
}
@cashlo
cashlo / gomoku_board.py
Created June 3, 2020 16:45
Gomoku Board
class GomokuBoard:
def __init__(self, size=9):
self.size = size
self.board = [0]*(size*size)
self.last_move = None
self.total_moves = 0
def clone_board(self):
new_board = GomokuBoard(self.size)
@cashlo
cashlo / gomoku_mcst.py
Created June 3, 2020 16:30
Gomoku MCST implementation
class GomokuSearchTree(Node):
def __init__(self, parent, board, from_move, next_player, simulation_limit=1, exploration_constant=1):
Node.__init__(self, parent=parent, simulation_limit=simulation_limit, exploration_constant=exploration_constant)
self.board = board
self.from_move = from_move
self.next_player = next_player
def create_from_move(self, move):
new_board = self.board.clone_board()
new_board.place_move(move, self.next_player)
def check_board(self, test=False):
if self.last_move is None:
return Gomoku.IN_PROGRESS
x = self.last_move%self.size
y = self.last_move//self.size
cell = self.board[self.last_move]
min_x = max(0, x-4)
min_y = max(0, y-4)
@cashlo
cashlo / print_gomoku_board.py
Last active June 3, 2020 15:32
Printing a gomoku board
def print(self):
header = ' '
for x in range(self.size):
if self.last_move is not None and x == self.last_move%self.size:
header += '▼ '
else:
header += f'{x+1} '
print(header)
for y in range(self.size):
@cashlo
cashlo / monte_carlo_tree_search.py
Created June 2, 2020 13:06
Monte Carlo Tree Search
import math
import random
class Node:
def __init__(self, parent=None, simulation_limit=1, exploration_constant=1):
self.parent=parent
self.reward = 0
self.visit_count = 0
self.possible_move_list = None
self.expanded_children = {}
from collections import defaultdict
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums_count_map = defaultdict(int)
result_set = set()
for n in nums:
nums_count_map[n] += 1
for n in nums_count_map:
for m in nums_count_map:
from scipy.io import loadmat
import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt
def sigmoid(z):
return 1/(1+np.exp(-z))
def costFunctionReg(theta, X, y, lmbda):
m = len(y)