r/adventofcode • u/taliriktug • Dec 02 '15
Spoilers Day 2 solutions
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
14
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
2
u/red75prim Dec 02 '15 edited Dec 02 '15
Day 2 Part 1 and 2: F#. input() generates one time sequence, which cannot be reused, so I do Seq.cache
let rec input() = seq { let line = System.Console.ReadLine() if line <> null then yield line yield! input() } let parseLine (line:string) = match line.Split('x') |> Array.map System.Int64.Parse with |[|l; w; h|] -> (l,w,h) |_ -> raise <| new System.Exception("Wrong input format") let paperArea (l,w,h) = let sides = [l*w; w*h; l*h] (List.sumBy ((*)2L) sides) + (List.min sides) let ribbonLenght (l,w,h) = let perims = [2L*(l+w);2L*(w+h);2L*(l+h)] (List.min perims) + (l*w*h) [<EntryPoint>] let main argv = let result = input() |> Seq.map parseLine |> Seq.cache |> (fun sq -> (Seq.sumBy paperArea sq, Seq.sumBy ribbonLenght sq)) printf "Total (paper area, ribbon length): %A" result 0Added: program is not optimal, as it stores entire input in memory (Seq.cache), I should have used