Skip to content

Instantly share code, notes, and snippets.

defmodule CLIHistogram do
defp block_index_from_fractional(blocks, fractional) when fractional >= 0 and fractional <= 1, do:
round(fractional * (tuple_size(blocks) - 1))
defp block_index_from_fractional(_blocks, fractional) when fractional < 0, do: 0
defp block_index_from_fractional(blocks, fractional) when fractional > 1, do: tuple_size(blocks) - 1
defp block_from_fractional(blocks, fractional), do: elem(blocks, block_index_from_fractional(blocks, fractional))
defp repeat_string(str, count) when count <= 0, do: str
defp repeat_string(str, count), do: String.pad_leading("", count, str)
@ddlsmurf
ddlsmurf / wordle_cheat.exs
Last active October 13, 2023 15:08
Playing around with wordle logic
defmodule WordleCheat do
defmodule Utils do
def chars_of(str) when is_binary(str), do: str |> String.downcase() |> String.graphemes()
def map_inc(map, key) when is_map(map), do: Map.update(map, key, 1, &(&1 + 1))
def map_dec(map, key) when is_map(map), do: Map.update(map, key, -1, &(&1 - 1))
def map_dec_or_del(map, key) when is_map(map) do
case Map.get(map, key) do
1 -> Map.delete(map, key)
n when n > 1 -> Map.put(map, key, n - 1)
end
@ddlsmurf
ddlsmurf / ssh_sign.exs
Last active February 6, 2022 01:36
SSH private key file and signature file reader
defmodule T do
def read_ssh_sig(filename) do
{:ok, raw_sig} = File.read(filename)
raw_sig = String.replace(raw_sig, ~r/.*-----BEGIN SSH SIGNATURE-----\n/s, "")
raw_sig = String.replace(raw_sig, ~r/\n-----END SSH SIGNATURE-----.*/s, "")
Base.decode64(raw_sig, ignore: :whitespace)
end
defp shift_string(<<len::unsigned-integer-32, str::binary-size(len), rest::binary>>), do: { str, rest }
defp parse_ssh_sig_body(sig_body) do
# ssh_sig_magic = 'SSHSIG'
@ddlsmurf
ddlsmurf / index.html
Last active December 25, 2021 22:37
Calculates more interesting dates to celebrate if your existence duration is the main variable
<html>
<head>
<title>Birthdates ! </title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone-with-data.js" integrity="sha512-oqSECbLRRAy3Sq2tJ0RmzbqXHprFS+n7WapvpI1t0V7CtV4vghscIQ8MYoQo6tp4MrJmih4SlOaYuCkPRi3j6A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!-- Select 2 -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
</head>
@ddlsmurf
ddlsmurf / philips_hue_tap_notes.md
Last active September 19, 2022 23:23
Philips Hue Tap notes

I had issues with my tap switch, it would stop working. Here are my notes from the disassembly.

Internally a PTM215Z.

Join (or re-join) procedure

This sometimes is required even if nothing changed, not sure why.

  • Set the hub to scan for sensors (POST api/${user}/sensors) - This could be optional if the channel was changed. Try skipping this first, if that doesn't work, retry without skipping this.
  • Press and hold button for your channel (see table below) for at least 7 seconds.
@ddlsmurf
ddlsmurf / index.html
Last active December 21, 2021 17:25
Watchy BLE OTA client #watchy #tool
<html>
<head>
<style>
.dragging {
background-color: rgb(236, 135, 135);
}
</style>
</head>
<body>
<h1>Watchy BLE firmware upload client</h1>
@ddlsmurf
ddlsmurf / wifi_update_client.c
Last active August 7, 2021 00:56
ESP32 https OTA #watchy #esp32
#include <wifi_update_client.h>
#include <esp_log.h>
#include <esp32-hal.h>
#include <esp_https_ota.h>
static const char *LOG_TAG = "wifi_update_client";
const char *url = "https://raw.githubusercontent.com/sqfmi/Watchy/master/examples/WatchFaces/7_SEG/7_SEG.bin";
@ddlsmurf
ddlsmurf / pretty_json.coffee
Last active May 22, 2021 11:28
This is a very hastily cobbled together JSON prettifyer that doesn't bug me about input validity as long as unambiguous. Also it warns about a few validity issues (not all). #tool
{ Transform } = require('stream')
EOL = "\n"
Utils =
spaces: (num) -> if num <= 0 then '' else (new Array(num + 1)).join(" ")
ljust: (str, len) -> str + Utils.spaces(len - str.length)
streamToString: (stream, cb) ->
len = 0
buffer = []
stream
.on('end', -> cb(null, Buffer.concat(buffer, len)))
@ddlsmurf
ddlsmurf / type.coffee
Last active May 22, 2021 11:39
More exhaustive version of typeof #snippet_js
toString = Object::toString
hasOwnProperty = Object::hasOwnProperty
propertyIsEnumerable = Object::propertyIsEnumerable
toStringTags = date: '[object Date]'
Utils =
### @return `undefined|null|boolean|string|symbol|number|NaN|array|arguments|date|object|function` ###
type: (val) ->
switch type = typeof val
when 'undefined', 'string', 'boolean', 'symbol'
return type
@ddlsmurf
ddlsmurf / this_in_javascript.md
Last active May 22, 2021 11:29
what's `this` in javascript #tutorial

this

Function declarations

There are two (mostly) equivalent ways to define a function:

function Add(a, b) { return a + b };

var Add = function(a, b) { return a + b };