View Single Post
Advent of code luke 5 i Haskell
SPOILER ALERT! Vis spoiler

Ikke noe fancy, ser ut som den kanskje misser tail call optimization, men orker ikke finne ut nå

Kode

import Data.Sequence 
data CPU = CPU !Int !Int (Seq Int)

main =  do
        textBlob <- readFile "dec05.txt"
        let tape = fromList (map read (lines textBlob))
        print(process (CPU 0 0 tape))

process (CPU clock ip tape)
    | ip < 0 = clock
    | ip >= Data.Sequence.length tape = clock
    | otherwise = let jump = tape `index` ip
                      nextJmp = if jump >= 3 then jump - 1 else jump + 1
                      mutedTape = update ip nextJmp tape
        in process (CPU (clock + 1) (ip + jump) mutedTape)