-
-
Save borkdude/3d080f43c2097ddf26b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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