R

@RealSaitama

CodeWars SAFE (TipToe)

Python
9 months ago
from math import hypot, acos, cos, sin, pi, atan2 from collections import defaultdict import heapq as hq import time EPS = 1e-7 # returns True if the point corresponding to the angle x lies between the points for angles a and b def is_between_angles(x, a, b): a %= 2 * pi b %= 2 * pi x %= 2 * pi

Quad Tree

Python
9 months ago
from math import hypot, acos, cos, sin, pi, atan2 class _Point: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return f"({self.x}, {self.y})"

Totient Sum

Python
9 months ago
from math import isqrt PHI = [0, 1, 2, 4, 6, 10, 12, 18, 22, 28, 32, 42, 46, 58, 64, 72, 80, 96, 102, 120, 128, 140, 150, 172, 180, 200, 212, 230, 242, 270, 278, 308, 324, 344, 360, 384, 396, 432, 450, 474, 490, 530, 542, 584, 604, 628, 650, 696, 712, 754, 774, 806, 830, 882, 900, 940, 964, 1000, 1028, 1086, 1102, 1162, 1192, 1228, 1260, 1308, 1328, 1394, 1426, 1470, 1494, 1564, 1588, 1660, 1696, 1736, 1772, 1832, 1856, 1934, 1966, 2020, 2060, 2142, 2166, 2230, 2272, 2328, 2368, 2456, 2480, 255

Naturals to Rationals

Python
9 months ago
from fractions import Fraction from math import isqrt def gen(): k = 1 while 1: i = 1 while i <= k: yield (i, k - i + 1)

Square Root Continued Fraction

Python
9 months ago
from math import isqrt from fractions import Fraction def generate_continued_fraction(n): a0 = isqrt(n) is_square = a0**2 == n step = 0 r, s = 0, 1 while True: if step > 0 and is_square:

Exact Cover

Python
9 months ago
from copy import deepcopy from typing import List # https://www.cs.mcgill.ca/~aassaf9/python/algorithm_x.html def solve(X, Y, solution=[]): if not X: yield list(solution) else: c = min(X, key=lambda o: len(X[o])) for r in list(X[c]): solution.append(r)

Wumpus

Python
9 months ago
tests = [ ([ [*"____"], [*"_W__"], [*"___G"], [*"P___"] ], True), ([ [*"____"],

Packing Rectangles

Python
9 months ago
import numpy as np from collections import defaultdict from typing import List, Tuple, Set, Optional class EfficientRectangleSolver: def __init__(self, board: List[str], pieces: List[List[int]]): self.board = board self.pieces = pieces self.n, self.m = len(board), len(board[0])

Rectangular Tiling with Obstacles

Python
9 months ago
import numpy as np import heapq as hq from collections import defaultdict class Rectangle: def __init__(self, h, w, x, y, n, m): self.h, self.w = h, w self.x, self.y = x, y self.n, self.m = n, m self.mask = self.bit_mask()

Closure of Set

Python
9 months ago
import heapq as hq def closure(xs, M): q = xs[:] seen = set() hq.heapify(q) for _ in range(M): if len(q) > 0: y = hq.heappop(q) if not y in seen:

Solver

Python
9 months ago
from fractions import Fraction P = [[0, 3, 3], [0, 3, 1], [1, 3, 1]] mod = 4 # 1, 1, 0, -1, -1, 0, ... def seq(n): return [1, 1, 0, -1, -1, 0][n % 6] def show(m): return "\n".join(" ".join(map(str, r)) for r in m) + "\n"

Modular Puzzle Solver

Python
9 months ago
from collections import defaultdict # 1, 1, 0, -1, -1, 0, ... def seq(n): return [1, 1, 0, -1, -1, 0][n % 6] def get_next_row(n, row, mod): if n % 3 == 2: return get_next_row(n - 1, row[:-1], mod) + [0]

Array Inverse

Python
9 months ago
from fractions import Fraction def gauss(_arr): arr = [[Fraction(x) for x in r] + [{i: Fraction(1)}] for i, r in enumerate(_arr)] n, m = len(arr), len(arr[0]) h, k = 0, 0 while h < n and k < m: idx = next((i for i in range(h, n) if arr[i][k] != 0), None) if idx is None: k += 1 else:

Arrow Puzzle

Python
9 months ago
def gauss(arr, mod): n, m = len(arr), len(arr[0]) h, k = 0, 0 while h < n and k < m: idx = next((i for i in range(h, n) if arr[i][k] != 0), None) if idx is None: k += 1 else: arr[idx], arr[h] = arr[h], arr[idx] for i in range(h + 1, n):

Marple Solver

NodeJS
9 months ago
const LETTERS = "ABCDEFGHIJKLMNOPQRST"; const ROWS = [ [...("ABCDE")], [...("FGHIJ")], [...("KLMNO")], [...("PQRST")] ]; class Marple{ constructor(clues){

Queen mandatory

C++
9 months ago
#include <algorithm> #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <ctime> using namespace std; #define clr(a) memset(a, 0, sizeof(a))

Four Pass Transport

NodeJS
9 months ago
const N = 10; class PriorityQueue { constructor() { this.heap = []; } push(item, priority) { this.heap.push({ item, priority }); this._heapifyUp(this.heap.length - 1);

Circle path

NodeJS
10 months ago
function vec_polar(r, a) { return {x: r * Math.cos(a), y: r * Math.sin(a)}; } function vec_add(p, q) { return {x: p.x + q.x, y: p.y + q.y}; } function vec_sub(p, q) { return {x: p.x - q.x, y: p.y - q.y};

Clock Rotation

Python
10 months ago
fct = lambda k:( [k,k,k,0,0,0,0,0,0], [0,0,0,0,0,0,k,k,k], [k,0,0,k,0,0,k,0,0], [0,0,k,0,0,k,0,0,k], [k,k,0,k,k,0,0,0,0], [0,k,k,0,k,k,0,0,0], [0,0,0,k,k,0,k,k,0], [0,0,0,0,k,k,0,k,k],

Marple Game

Python
10 months ago
def solve_marple_logic(clues): letters = "ABCDEFGHIJKLMNOPQRST" poss = {letter: set(range(5)) for letter in letters} rows = [ list("ABCDE"), list("FGHIJ"), list("KLMNO"), list("PQRST") ]