Skip to content

Instantly share code, notes, and snippets.

View code-shoily's full-sized avatar

Code Shoily code-shoily

  • Toronto, ON
  • 20:38 (UTC -12:00)
View GitHub Profile
@code-shoily
code-shoily / geo.py
Created August 11, 2024 05:09
Create a random spatial point within specific distance (in meter) of a given point.
from random import random
from math import cos, pi, radians, degrees, sin, sqrt
METER_PER_DEGREE = 111_139
def generate_location_near(*, lat: float, lng: float, distance: float) -> tuple[float, float]:
lat_radian, lng_radian = radians(lat), radians(lng)
w, t = (distance / METER_PER_DEGREE) * sqrt(random()), 2 * pi * random()
@code-shoily
code-shoily / day_23_12.py
Created December 13, 2023 05:43
Advent of Code 2023 - Day 12
from functools import lru_cache
type Group = tuple[int, ...]
type Result = tuple[int, int]
type SpringConfiguration = tuple[str, Group]
OPERATIONAL = "."
DAMAGED = "#"
@code-shoily
code-shoily / aoc_day_12.livemd
Last active December 13, 2022 04:27
Advent of Code 2022/15 LiveBook attempt 1

Day 12

Mix.install([{:vega_lite, "~> 0.1.6"}, {:kino_vega_lite, "~> 0.1.7"}])
alias VegaLite, as: Vl

Part 1

@code-shoily
code-shoily / aoc_20_5_alt.ex
Created December 5, 2020 19:10
Advent of Code 2020, Day 5
def run_1_bin, do: input!() |> String.split("\n") |> Enum.map(fn d -> d |> String.replace("F", "0") |> String.replace("B", "1") |> String.replace("R", "1") |> String.replace("L", "0") |> String.to_integer(2) end) |> Enum.max()
def run_2_bin, do: input!() |> String.split("\n") |> Enum.map(fn d -> d |> String.replace("F", "0") |> String.replace("B", "1") |> String.replace("R", "1") |> String.replace("L", "0") |> String.to_integer(2) end) |> Enum.sort() |> (fn [_ | tail] = total -> total |> Enum.zip(tail) |> Enum.filter(fn {a, b} -> b - a != 1 end) |> (fn [{_, b}] -> b - 1 end).() end).() end
@code-shoily
code-shoily / logic_puzzle.exs
Last active April 12, 2020 14:37
Solve a logic puzzle with Elixir
defmodule LogicChallenge do
@type combination :: String.t()
@spec run :: combination | :error
def run() do
1..999
|> Enum.map(&to_string/1)
|> Enum.map(&String.pad_leading(&1, 3, "0"))
|> Enum.map(&String.graphemes/1)
|> Enum.filter(&check/1)
@code-shoily
code-shoily / find_referred_tables.sql
Created October 28, 2019 14:30
Find all tables that has 'auth_user' as Foreign Key
select
(select
cls.relname
from
pg_class cls
where
cls.oid = cnstr.conrelid) as tbl,
(select
array_agg(attname)
from
import datetime
from typing import Dict, Any
from hypothesis import strategies
from django.db import models
from django.db.models import F, When, Case
class MessageManager(models.Manager):
def latest_distinct(self):
Duration toDuration(String duration) {
var tokens = duration.split(":").map(int.parse).toList();
if (tokens.length == 3)
return Duration(hours: tokens[0], minutes: tokens[1], seconds: tokens[2]);
else
return Duration(hours: tokens[0], minutes: tokens[1]);
}
Duration timediff(String startTime, String endTime) {
@code-shoily
code-shoily / main.dart
Created December 23, 2018 01:56
Restart Aqueduct devserver upon file change [ Attemp #1 ]. Replace `bin/main.dart` with this.
/// Tried to autoreloading aqueduct on code change. First attempt off my head.
/// Dependency: `watcher: ^0.9.7+10`, maybe this whole thing is a bad idea?
///
/// TODO:
/// 1. Refactor so that it has minimal code change from the generated `bin/main.dart`
/// 2. Reload only once if lots of files change (i.e. scaffolding while devserver was running)
/// 3. Take hints from configuration file
/// 4. What to do if error occurs?
import "package:watcher/watcher.dart";
@code-shoily
code-shoily / admin.py
Last active June 9, 2018 02:36
Answer to Python Bangladesh question regarding adding button in admin.
"""
This is from an actual snippet of mine, trimmed down, in order to answer a question asked in Python Bangladesh Group on
adding extra buttons in Admin list.
"""
from django.contrib import admin
from django.urls import reverse
from django.utils.html import mark_safe
from .models import *