Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env bash
echo "Install all AppStore Apps manuallyfirst!"
read -p "Press any key to continue... " -n1 -s
echo '\n'
echo "Install xcode-select --install if not done yet!"
read -p "Press any key to continue... " -n1 -s
echo '\n'
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class Anagram {
private static final long [] FIRST_26_PRIMES = new long [] {
2,
3,
5,
7,
11,
13,
17,
;; x-1 y-1 | x y-1 | x+1 y-1
;; x-1 y | x y | x+1 y
;; x-1 y+1 | x y+1 | x+1 y+1
(-> (empty-board 9 9 10)
(init [1 1])
(flag [0 1])
(explore [0 2])
(display))
(defn init
[board start-idx]
(if-not (started? board)
(init-game board start-idx)
board))
(defn explore
[board idx]
(if-not (or (pos? (nth (:flags board) idx))
(pos? (nth (:explored board) idx))
(defn- init-game
[board start-idx]
(explore-cell
(-> (place-mines board start-idx)
(place-warnings))
start-idx))
(defn- explore-cell
([board idx]
(explore-cell board #{} #{idx}))
([board explored remaining]
(if (empty? remaining)
board
(let [curr (first remaining)
explored (conj explored curr)
remaining (disj remaining curr)
board (set-explored board curr)
(defn- nice-neighbours
[board idx]
(let [mines (:mines board)
explored (:explored board)
neighbours (neighbours board idx)
is-set? (fn [[k v]] (pos? v))]
(when
(not-any? is-set? (select-keys mines neighbours))
(->> (select-keys explored neighbours)
(remove is-set?)
(defn- place-warnings
[board]
(->> (keep-indexed #(if (pos? %2) %1) (:mines board))
(mapcat (partial neighbours board))
(frequencies)
(reduce-kv set-warning board)))