The puzzle is a 4x4 grid of numbers between 1 and 5. 4-connected tiles of same number form a group. For example, the following grid has five groups:
1 2 2 1
1 3 3 1
1 3 3 2
2 2 2 2
// This is an experiment to combine hashing with a floating point address space. | |
// | |
// # The Problem: | |
// | |
// In a binary search tree, we wish to assign each node a unique address such | |
// that nodes which sort to the right have higher addresses than nodes that sort | |
// to the left. We want to generate the address when the node is inserted, and | |
// we want addresses to be stable, meaning that new insertions will not effect | |
// existing addresses. We also want finite bounds on the address space. | |
// |
$ ncdump -h NAM-NMM/nam.20161111/nam.t00z.awphys.tm00.nc | |
netcdf nam.t00z.awphys.tm00 { | |
dimensions: | |
reftime = 1 ; | |
z_HTGL3 = 2 ; | |
z_ISBL3 = 5 ; | |
z_ISBL2 = 42 ; | |
z_HTGL2 = 2 ; | |
z_HTGL1 = 2 ; | |
z_ISBL1 = 39 ; |
import re | |
import sys | |
import numpy as np | |
def read(stream=sys.stdin): | |
'''Read some input to get a matrix of mines. | |
Each row of the mine matrix is a different mine. The first column is the | |
current gold value. The second column is the decay rate per travel step. |
#!/usr/bin/env swipl | |
:- initialization(main, main). | |
:- use_module(library(clpfd)). | |
:- use_module(library(dcg/basics)). | |
%% tour(Shape, Tour) | |
% Tour is a tour of a chess board of the given Shape. |
Consider the problem of matrix multiplication. A matrix A
of shape (n, m)
may be multiplied by a matrix B
of shape (m, p)
to produce a new matrix A * B
of shape (n, p)
. The number of scalar multiplications required to perform this operation is n * m * p
.
Now consider the problem of multiplying three or more matrices together. It turns out that matrix multiplication is associative, but the number of scalar multiplications required to perform such operation depends on the association.
For example, consider three matrices of the following shapes:
A: (3, 5)
B: (5, 7)
#!/usr/bin/env python3 | |
class Position: | |
def __init__(self, file, offset): | |
'''A position has both a file and offset''' | |
self.file = file | |
self.offset = offset | |
def __lt__(self, other): | |
'''Positions are ordered first by file then by offset.''' |
Test cases for the blog post [Encoding tic-tac-toe in 15 bits][post].
Run like this:
# Base4 test
$ cc base4.c && ./a.out
# Base3 test
$ cc base3.c && ./a.out