Skip to content

Instantly share code, notes, and snippets.

function global:prompt
{
$global:GitPromptSettings.DefaultPromptSuffix = ""
Update-ZLocation $pwd
"╭─ $(ZLocationOrigPrompt) $('+' * ($nestedPromptLevel))`n╰─$ ";
#"╭─ PS $($executionContext.SessionState.Path.CurrentLocation) $('+' * ($nestedPromptLevel))`n╰─$ ";
}
@codecontemplator
codecontemplator / gist:7620852
Created November 23, 2013 22:31
Hopfield network implementation
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
type Network = { mutable s : array<int>; w : double[,]; n : int }
with
static member create (n : int) (patterns : seq<array<int>>) =
{
n = n
s = Array.create n 0
w = Array2D.init n n (fun i j ->
@codecontemplator
codecontemplator / gausselimination.js
Created May 30, 2015 14:13
Example implementation of gauss elimination in javascript
function print(M, msg) {
console.log("======" + msg + "=========")
for(var k=0; k<M.length; ++k) {
console.log(M[k]);
}
console.log("==========================")
}
function diagonalize(M) {
var m = M.length;
from ortools.sat.python import cp_model
from typing import NamedTuple
class Box(NamedTuple):
weight: int
class Truck(NamedTuple):
capacity: int
trucks = [Truck(3000), Truck(5000), Truck(8000)]
-- ref: https://www.youtube.com/watch?v=k-QwBL9Dia0&ab_channel=SkillsMatter%28formerlyYOW%21Conferences%29
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
import Data.Functor.Identity
import Data.Functor.Const
type Lens s t a b = forall f . Functor f => (a -> f b) -> (s -> f t)
type Lens' s a = Lens s s a a
import Data.List
import Data.Map (Map)
import qualified Data.Map as Map
frequencyOfElt xs = [ (c, length $ filter (== c) xs) | c <- nub xs]
type Rule = (String,Char)
parseFile :: [String] -> (String, [Rule])
parseFile (r1:_:rs) =
# v <- c(3,4,3,1,2)
v <- c(5,3,2,2,1,1,4,1,5,5,1,3,1,5,1,2,1,4,1,2,1,2,1,4,2,4,1,5,1,3,5,4,3,3,1,4,1,3,4,4,1,5,4,3,3,2,5,1,1,3,1,4,3,2,2,3,1,3,1,3,1,5,3,5,1,3,1,4,2,1,4,1,5,5,5,2,4,2,1,4,1,3,5,5,1,4,1,1,4,2,2,1,3,1,1,1,1,3,4,1,4,1,1,1,4,4,4,1,3,1,3,4,1,4,1,2,2,2,5,4,1,3,1,2,1,4,1,4,5,2,4,5,4,1,2,1,4,2,2,2,1,3,5,2,5,1,1,4,5,4,3,2,4,1,5,2,2,5,1,4,1,5,1,3,5,1,2,1,1,1,5,4,4,5,1,1,1,4,1,3,3,5,5,1,5,2,1,1,3,1,1,3,2,3,4,4,1,5,5,3,2,1,1,1,4,3,1,3,3,1,1,2,2,1,2,2,2,1,1,5,1,2,2,5,2,4,1,1,2,4,1,2,3,4,1,2,1,2,4,2,1,1,5,3,1,4,4,4,1,5,2,3,4,4,1,5,1,2,2,4,1,1,2,1,1,1,1,5,1,3,3,1,1,1,1,4,1,2,2,5,1,2,1,3,4,1,3,4,3,3,1,1,5,5,5,2,4,3,1,4)
n <- 80
for(i in 1:n) {
v1 <- v[v == 0] + 8
v2 <- v[v == 0] + 6
v3 <- v[v > 0] - 1
v <- c(v1, v2, v3)
#print(length(v))
input <- read.table("input.txt", header = FALSE) # the input was sligtly adjusted to be accepted by read.table
n <- dim(input)[1]
xs <- input[c(1, 3)]
xs <- xs - min(xs) + 1
ys <- input[c(2, 4)]
ys <- ys - min(ys) + 1
m <- matrix(0, max(ys), max(xs))
-- https://bartoszmilewski.com/2017/01/02/comonads/
-- https://hackage.haskell.org/package/comonad-5.0.8/docs/Control-Comonad.html
import Control.Comonad
import Debug.Trace (trace)
data Stream a = Cons a (Stream a)
instance Functor Stream where
fmap f (Cons a as) = Cons (f a) (fmap f as)
-- https://adventofcode.com/2020/day/20
{-# LANGUAGE DeriveFunctor #-}
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (Maybe)
import qualified Data.Maybe as Maybe
import Data.Set (Set)
import qualified Data.Set as Set