Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
MikuroXina / poly.rs
Last active October 1, 2023 10:13
Polyomino structure defintion with Rust.
#[derive(Clone, PartialEq, Eq, Hash)]
struct Poly<const W: usize, const H: usize> {
field: [[bool; W]; H],
}
impl<const W: usize, const H: usize> std::fmt::Debug for Poly<W, H> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "\"\"\"")?;
for y in 0..H {
for x in 0..W {
@MikuroXina
MikuroXina / grid_graph.rs
Last active March 25, 2025 02:04
Quick grid graph implementation for searching square map with Rust.
/// Coordinate on 2D plane, which `+X` is right and `+Y` is down.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Coordinate {
pub x: usize,
pub y: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GridGraph<T> {
grid: Vec<Vec<T>>,
class SelfLoveRecoveryProgram:
def __init__(self):
self.self_love_level = 50
def increase_self_love(self, amount):
self.self_love_level += amount
def decrease_self_love(self, amount):
self.self_love_level -= amount
@MikuroXina
MikuroXina / co_co.rs
Created August 13, 2023 02:06
Coordinate Compression utility with Rust.
use std::collections::BTreeMap;
#[derive(Debug)]
pub struct CoCo<K: Ord> {
forward: BTreeMap<K, usize>,
backward: Vec<K>,
}
impl<K: Ord> CoCo<K> {
pub const fn new() -> Self {
import json
import itertools
from typing import Any, Dict, List, Set, Tuple
from tqdm import tqdm
from functools import reduce
import operator
import math
def product_of_positive_factorials(multi_index):
@MikuroXina
MikuroXina / btree_multi_set.rs
Created June 17, 2023 17:59
A set structure which contains multiple same items with count of them, implemented for competitive programming in Rust.
use std::{borrow::Borrow, collections::BTreeMap, num::NonZeroUsize};
#[derive(Debug, Clone)]
pub struct BTreeMultiSet<T> {
counts: BTreeMap<T, NonZeroUsize>,
len: usize,
}
impl<T> BTreeMultiSet<T> {
pub const fn new() -> Self {
@MikuroXina
MikuroXina / setup-canvas.js
Last active June 7, 2023 06:17
Make `<canvas>` prepared for more pixel density display.
/**
* Make `<canvas>` prepared for more pixel density display and returns a 2D rendering context.
*
* @param canvas {HTMLCanvasElement} - To be preapared.
* @param devicePixelRatio {number} - The pixel ratio by device.
* @returns {Canvas2DRenderingContext} - The scaled 2D rendering context.
*/
export function setupCanvas(canvas, devicePixelRatio) {
const { width, height } = canvas;
canvas.width *= devicePixelRatio;
@MikuroXina
MikuroXina / fenwick.hpp
Created May 23, 2023 04:56
The implementation of Fenwick tree.
#pragma once
#include <vector>
class Fenwick {
std::vector<size_t> data;
size_t parent(size_t i) const {
return i - (i & (-i));
}
#pragma once
template<class T>
inline void insert_sort(vector<T> &vec, size_t g) {
for (size_t i = g; i < vec.size(); ++i) {
int v = vec[i];
int j = i - g;
while (0 <= j && v < vec[j]) {
vec[j + g] = vec[j];
j -= g;
#pragma once
#include <algorithm>
#include <optional>
enum class Direction {
North,
West,
East,
South,