View Advent9.groovy
// see challenge here: http://adventofcode.com/2017/day/9
String input = /your input here/
String collapseEscape = input.replaceAll(/!./,'')
String collapseGarbage = collapseEscape.replaceAll(/<.*?>/,'')
String listString = collapseGarbage.tr('{}','[]')
String normCode = listString.replaceAll(/\[,/,'[')
View Advent8.groovy
// http://adventofcode.com/2017/day/8
def input = '''b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10'''
regs =[:].withDefault{0}
inc = { String key, Integer x -> regs[key] += x }
dec = { String key, Integer x -> inc(key, -x) }
View Advent6.fr
module Advent6 where
{-
http://adventofcode.com/2017/day/6
-}
import Data.List
mapAtIndex :: (a->a) -> Int -> [a] -> [a]
mapAtIndex f n xs = take n xs ++ rest (drop n xs) where
View Advent3.fr
module Advent3 where
{-
http://adventofcode.com/2017/day/3
-}
import Data.List
data Direction = Right | Up | Down | Left
derive Show Direction
View Advent2.fr
module Advent2 where
{-
http://adventofcode.com/2017/day/2
-}
-- the core of the checksum logic
checkSum xss = sum (map lineCS xss) where
lineCS xs = maximum xs - minimum xs
View Advent1.groovy
// http://adventofcode.com/2017/day/1
def captcha(list) {
def last = list[-1]
def clean = list.findAll { e ->
if (e == last) {
last = e; true
} else {
last = e ; false
}
View Leak.fr
-- Leak example from http://homepages.inf.ed.ac.uk/wadler/papers/leak/leak.ps
-- convenience
parseStr :: String -> (String, String)
parseStr = packedPair . parse . unpacked where
packedPair (x,y) = (packed x, packed y)
parse :: [Char] -> ([Char], [Char])
parse [] = ([], [])
parse (' ': cs) = ([], cs)
View Balanced.purs
module Balanced where
import Prelude (Unit, discard, ($), (+), (-), (>=), (<=), (&&), (==), negate)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Data.Foldable (traverse_, foldMap)
import Control.Monad.State (State, execState)
import Control.Monad.State.Class (modify)
import Data.String (toCharArray)
View FizzBuzz.idr
module FizzBuzz
import Data.Vect
Rule : Type
Rule = (n: Int) -> (old: List String) -> (List String)
everyNthRule : (every: Int) -> (replacement: String) -> Rule
everyNthRule every replacement n old =
if (mod n every == 0) then old ++ [replacement] else old
View Either.groovy
// Either type in Groovy as a special implementation of the generic sum type
// see: Phil Wadler at LambdaWorld 2016: youtu.be/V10hzjgoklA
import java.util.function.*
import groovy.transform.*
interface Either<A,B> {
public <C> C match (Function <A,C> leftCase, Function <B,C> rightCase)
}