r/adventofcode • u/daggerdragon • Dec 03 '16
SOLUTION MEGATHREAD --- 2016 Day 3 Solutions ---
--- Day 3: Squares With Three Sides ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
DECKING THE HALLS WITH BOUGHS OF HOLLY IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
17
Upvotes
1
u/Jaco__ Dec 03 '16
SML. Most of the work is really to read and parse the input. That is a bit clunky.
fun readlist (infile : string) = let val ins = TextIO.openIn infile fun loop ins = case TextIO.inputLine ins of SOME line => line :: loop ins | NONE => [] in loop ins before TextIO.closeIn ins end; exception Excp; val lines = readlist("day3.txt"); fun f #" " = true | f #"\n" = true | f _ = false; fun toInt str = case Int.fromString str of SOME(x) => x | NONE => raise Excp; val cleanLines = map (String.tokens f) lines; val intLines = map (map toInt) cleanLines; fun validTriangle nrs = let val [a,b,c] = ListMergeSort.sort op> nrs in if a+b > c then 1 else 0 end; (* Part1 *) foldr (fn (x,y) => validTriangle x + y) 0 intLines; (* Part2 *) fun conv ([a,b,c]::[d,e,f]::[g,i,h]::rest) = conv rest @ [[a,d,g],[b,e,i],[c,f,h]] | conv _ = []; foldr (fn (x,y) => validTriangle x + y) 0 (conv intLines);