Skip to content

Instantly share code, notes, and snippets.

use std::collections::{HashMap, HashSet};
const M: u64 = 1_000_000_007;
fn main() {
const N: usize = 10;
let array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
let mut dp: [[HashSet<u64>; N + 1]; N + 1] = Default::default();
let mut path = HashMap::new();
for l in 1..=N {
@hkurokawa
hkurokawa / ReptileSolver.java
Created April 27, 2022 15:35
Rep-tile problem solver
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public class ReptileSolver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
@hkurokawa
hkurokawa / main.py
Last active January 13, 2022 13:13
Wordle solver
import collections
import random
def is_alphabet(s):
return all('a' <= ch <= 'z' for ch in s.strip().lower())
class Problem:
def __init__(self, answer=None):
@hkurokawa
hkurokawa / my_solver.py
Created June 6, 2021 03:16
Traveling Salesman Problem Solver
#!/usr/bin/env python3
import math
import random
import sys
import time
from common import print_tour, read_input
TIME_LIMIT_TWO_OPT = 10 * 60 # 10 min.
TIME_LIMIT_THREE_OPT = 10 * 60 # 10 min.
@hkurokawa
hkurokawa / MiracleSudokuSolver.kt
Created May 23, 2020 09:11
Miracle Sudoku Solver/Generator
import kotlin.math.abs
import kotlin.system.measureTimeMillis
// This code solves a Sudoku puzzle with some additional constraints by Mitchell Lee
// https://www.theverge.com/tldr/2020/5/18/21262771/sudoku-puzzle-cracking-the-cryptic-watch-this-video-simon-anthony
//
// The constraints are as below:
// 1. Any two cells separated by a knight's move or a king's move (in chess) cannot contain the same digit.
// 2. Any two orthogonally adjacent cells cannot contain consecutive digits
// 3. Normal Sudoku rules apply
require 'priority_queue'
def solve(n, max_discs, l, t, m)
dp = Array.new(n + 1) { a = Array.new(n + 1, Float::INFINITY); a[0] = 0; a }
cut = Array.new(n + 1) { Array.new(n + 1) }
cumulate_ts = t.inject([0]) { |result, tvalue| result << result.last + tvalue }
(1..max_discs).each do |i|
k = 1
pq = PriorityQueue.new
(1..n).each do |j|
@hkurokawa
hkurokawa / wrong_round_down.rb
Last active September 13, 2019 11:44
A function to round down to 2 decimal places which has a bug
# This function rounds down the given value to 2 decimal places
# **Note!** This implementation looks correct but actually is wrong
def floor_2(value)
return (value.to_f * 100).floor.to_f / 100
end
p floor_2(3.1415) # => 3.14
p floor_2(2.71828) # => 2.71
p floor_2(33.62) # => 33.61 !?
class MyQueue
def initialize(size)
@size = 0
@min_index = 0
@queues = Array.new(size) { Array.new }
end
def push(cost, index)
if cost < 0 || cost >= @queues.size
raise "invalid cost: cost=#{cost}, size=#{@queues.size}"
require 'benchmark'
require 'priority_queue'
#require 'profile'
M = 64
N = 153600
pq = PriorityQueue.new
list = Array.new(N) { |i| i }.shuffle
time = Benchmark.realtime do
(1..M).each {
@hkurokawa
hkurokawa / DetectLoopLinkedList.kt
Last active February 20, 2019 14:48
Detect loop of the given LinkedList
class LinkedListNode(var next: LinkedListNode?, val value: String)
fun detectLoop(list: LinkedListNode): String {
var node1 = list
var node2 = list
var n = 0
do {
node1 = node1.next!!
node2 = node2.next!!.next!!
n++