Skip to content

Instantly share code, notes, and snippets.

@borkdude
Created December 20, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/3d080f43c2097ddf26b8 to your computer and use it in GitHub Desktop.
Save borkdude/3d080f43c2097ddf26b8 to your computer and use it in GitHub Desktop.
Takes ages:
object Day10 extends App {
def input = "3113322113"
def charGroups(s: String): List[(Char, Int)] = s.foldLeft(List[(Char,Int)]())((acc, c) => acc match {
case Nil => List((c,1))
case v @ ((char, counted)) :: rest =>
if (char == c) (char, counted + 1) :: rest
else (c, 1) :: v
}).reverse
def groupToString(groups: List[(Char,Int)]): String = {
groups.foldLeft("")((acc,g) => g match {
case (char,count) => acc + count + char
})
}
def lookAndSay = charGroups _ andThen groupToString
val part1 = Function.chain(List.fill(40)(lookAndSay))(input)
println(part1.length)
val part2 = Function.chain(List.fill(10)(lookAndSay))(part1)
println(part2.length)
}
Terminates quickly:
(ns advent.day10)
(defn count-encode [x]
(apply str
(mapcat
(juxt count first)
(partition-by identity x))))
;; part 1 and part 2
#_(count (nth (iterate count-encode "1321131112") #_40 50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment