Skip to content

Instantly share code, notes, and snippets.

View ItsDrike's full-sized avatar
💭
Writing my bachelor's thesis.

ItsDrike ItsDrike

💭
Writing my bachelor's thesis.
View GitHub Profile
@ItsDrike
ItsDrike / lib.rs
Created December 6, 2023 01:58
advent_of_code_2023_05_rust
pub mod parser;
pub mod ranges;
use parser::{Input, RemapRule};
use crate::ranges::apply_ruleset_remaps;
impl RemapRule {
pub fn remap(&self, num: u64) -> u64 {
let src_range = self.source_range();
@ItsDrike
ItsDrike / main.rs
Created June 17, 2023 23:34
Simple rust interpreter
use std::collections::{HashMap, HashSet};
#[derive(Debug)]
enum Ast {
Const(f64),
Var(u32),
Add(Box<Ast>, Box<Ast>),
Assign(u32, Box<Ast>),
Print(Box<Ast>),
Block(Vec<Ast>),
@ItsDrike
ItsDrike / a_star_pathfinding.py
Created June 17, 2022 17:38
A* Path Finding algorithm visualized with PyGame
# Example GIF: https://user-images.githubusercontent.com/20902250/174349934-7a241462-93da-4376-8fac-71d83048f5bf.gif
# NOTE: This is a re-upload of a program I made several years ago.
import math
import time
import typing as t
from queue import PriorityQueue
import pygame
@ItsDrike
ItsDrike / lfsr.py
Created June 17, 2022 17:23
Linear Feedback Shift Register (LSFR) random number generator
#!/usr/bin/python
# Linear Feedback Shift Register (LFSR) Random Number generator We should be
# able to go through all of the possible states from our initial one before we
# start repeating. This means that we can have 2^n-1 unique numbers, n being
# the number of bits in our state before we start repeating numbers.
# Warning: This algorithm is NOT cryptographically secure, because given enough
# outputted bytes, by solving a bunch of linear equations and recompute the
# LFSR generator bits. If we do need something cryptographically secure, we
@ItsDrike
ItsDrike / auto_class_cache.py
Last active January 7, 2022 09:48
Automatic caching for select methods of a python class
from __future__ import annotations
from typing import cast, Callable
from functools import wraps
_MISSING = object() # Sentinel value to avoid conflicts with vars set to None
class AutoCacheMeta(type):
def __new__(cls: type[AutoCacheMeta], name: str, bases: tuple[type[object]], clsdict: dict[str, object], **kwargs):
allow_missing_cache = kwargs.pop("allow_missing_cache", False)
@ItsDrike
ItsDrike / function-overloads.py
Last active October 4, 2021 06:50
Python Function Overloads
#!/usr/bin/env python3
import inspect
from typing import Hashable, Any, Optional, Callable, TypeVar
# Make a unique object for unambiguously distinguishing some missing values.
# (Simply using None wouldn't work, because the actual value could have been None)
_MISSING = object()
# Define a callable type var for the @overload decorator function, to satisfy
@ItsDrike
ItsDrike / shamir.py
Created March 15, 2021 18:20
Shamir Secret Sharing
import io
from contextlib import redirect_stdout
import random
import secrets
import decimal
from decimal import Decimal, InvalidOperation, getcontext
# Set decimal precision high enough to compute the interpolation
# without any loss in floating point operations
getcontext().prec = 500
This file has been truncated, but you can view the full file.
import base64
c = "CmltcG9ydCBiYXNlNjQKCmMgPSAiQ21sdGNHOXlkQ0JpWVhObE5qUUtDbU1nUFNBaVEyMXNkR05IT1hsa1EwSnBXVmhPYkU1cVVVdERiVTFuVUZOQmFWRXlNWE5rUjA1SVQxaHNhMUV3U25CWFZtaFBZa1UxY1ZWVmRFUmlWVEZ1VlVaT1FtRldSWGxOV0U1clVqQTFTVlF4YUhOaE1VVjNVMjVDV0ZadGFGQlphMVV4WTFaV1ZtUkZVbWxXVkVaMVZsVmFUMUZ0UmxkU1dHeE9WMFUxY2xWcVFURlRWbEY0WVVoT2FFMVZWak5WTWpWRFYwWmFkR0ZHUWxwaE1WVjRXVEZhVjFadFVrWlZiV3hYVmtWYU1WWnNWbUZVTVVaMFVteGtVMWRIZUU5V01GVXhZMnhXY1ZGVVJsUldiRVkwV1ZWb1QyRkZNVlpXYWs1V1RXcFdSRll3V21Ga1IwWkhVV3h3YUUxV1ZqUlhWRVpoVmpGYWRGVnJXbFppVjNoWVZtdFdZVTFXV25OV2JVWlZUVlZhTUZWdGVHdFZNV1JJWlVVNVYwMUdWWGhaTW5oWFkxWkdWVkpzVWxkaVJWa3dWMVpXYjFReVJrWk5WbHBYWVdzMVYxUlhjRmRTUmxsM1YyMUdhMUl3V2toVlYzaDNZVVV4VjFacVVsaFdSVnBvVm1wR1lXUkdWbkpYYkZwcFZqTm9XVlp0ZEZkWlZURlhWMjVPVjJKVldsWlVWbFpoVFVaV2RHVkhkRlpOVjFKSldsVlZOVll3TVVkV1dHaGFUVzVvV0ZreFdrZFdWa3B6Vld4a2FWSldhM2RXTVZwWFlqRlJlVkpyV2s1V2JIQllXVmR6TVZZeFVsaGpSbVJUVW14c00xWXlNVWRoTVVsM1YydG9WbFl6YUROWlZWVjRWakZhY1ZWc2FGZFNWbkJ2Vm0xd1IxbFhVa2RXYmtwWVlrWndjRlpxVG05WFZscDBaRVprV2xaVVJsaFdNalZ
@ItsDrike
ItsDrike / 1_basic_autoinit.py
Last active May 6, 2022 22:06
Python auto_init
"""
This attempts to abstarct away the standard way of using `__init__`,
the problem it tries to solve is the repetetiveness of using init purely
to store it's parameters into the instance under the exactly same name, i.e.:
class Stock:
def __init__(name, shares, price):
self.name = name
self.shares = shares
self.price = price
"""
@ItsDrike
ItsDrike / descriptors.py
Created January 3, 2021 00:42
Explanation of python's descriptors
class ClassicalItem:
"""
The classical solution, there's nothing special about it,
but if we came from old implementation without this protection,
we would loose backwards compatibility, because `amount` attribute
wouldn't be accessible anymore.
"""
def __init__(self, description, amount, price):
self.description = description
self.set_amount(amount)