Skip to content

Instantly share code, notes, and snippets.

@brase
Last active December 2, 2017 15:09
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 brase/03519341d7d24cf1ccd71bcbdda1d3a6 to your computer and use it in GitHub Desktop.
Save brase/03519341d7d24cf1ccd71bcbdda1d3a6 to your computer and use it in GitHub Desktop.
Advent of Code 2017
let rec resultRec ints last sum =
match ints with
| i :: rest ->
//printfn "Ints: %A ; last: %i, sum: %i" ints last sum
if (i = last) then resultRec rest i (sum + i) else resultRec rest i sum
| [] -> sum
let result (s:string) =
let ints = [for c in s -> (int c) - 48]
let last = List.last ints
//printfn "ints %A, last: %i" ints last
resultRec ints last 0
let input = "5228833336355848549915459366737982598312959583817455621545976784792489468198365998232722734876612332352376192813552949814275947575774339529811976644361517795586998319242241614813622734255797569571577699238592667287428166398221572885869416419682687759743978434571821267146514338394624525648338739929479912368172669885577319718389278168766844487948761697438722556857882433224393723131298876252626643517236883999115665656935521675772866516185899317132494716723615493476397115627687887665194781746377341468995954554518252916859227397693885254329628812355612487594445522395853551734567498838382248616137969637971369615443599973588326388792893969924855316437952313492551671545714262784738343517166544197194547173515155927244175447296474282154114951181648317875827525814453758846194548872789943372281952995222779173812444186491115426476188672253249744478946863317915136832199132868917891243591195719354721129116229164688256853628339233919671468781913167415624214152793864585332944468428849171876873433621524242289488135675313544498245498637424139153782925723745249728743885493877792648576673196889949568317234125863369187953788611841388353999875519172896329524346527265231767868839696693328273381772726782949166112932954356923757485139367298699922984925977724972944277991686823219295939734313874834861796179591659174726432357533113896212781566659154939419866797488347448551719481632572231632463575591599696388223344219228325134233238538854289437756331848887242423387542214691157226725179683638967415678697625138177633444765126223885478348951332634398291612134852858683942466178329922655822225426534359191696177633167962839847985826676955417426617126288255366123169174674348417932158291334646767637764323226842771523598562429399935789788215958367362467652444854123951972118358417629679454978687337137675495295768451719631999398617828287671937584998697959425845883145736323818225129311845997214987663433375689621746665629187252511643969315283316269222835744532431378945137649959158495714472963839397214332815241141327714672141875129895"
printfn "%i" (result input)
open System
open System.Collections.Generic
let input = "409 194 207 470 178 454 235 333 511 103 474 293 525 372 408 428\n" +
"4321 2786 6683 3921 265 262 6206 2207 5712 214 6750 2742 777 5297 3764 167\n" +
"3536 2675 1298 1069 175 145 706 2614 4067 4377 146 134 1930 3850 213 4151\n" +
"2169 1050 3705 2424 614 3253 222 3287 3340 2637 61 216 2894 247 3905 214\n" +
"99 797 80 683 789 92 736 318 103 153 749 631 626 367 110 805\n" +
"2922 1764 178 3420 3246 3456 73 2668 3518 1524 273 2237 228 1826 182 2312\n" +
"2304 2058 286 2258 1607 2492 2479 164 171 663 62 144 1195 116 2172 1839\n" +
"114 170 82 50 158 111 165 164 106 70 178 87 182 101 86 168\n" +
"121 110 51 122 92 146 13 53 34 112 44 160 56 93 82 98\n" +
"4682 642 397 5208 136 4766 180 1673 1263 4757 4680 141 4430 1098 188 1451\n" +
"158 712 1382 170 550 913 191 163 459 1197 1488 1337 900 1182 1018 337\n" +
"4232 236 3835 3847 3881 4180 4204 4030 220 1268 251 4739 246 3798 1885 3244\n" +
"169 1928 3305 167 194 3080 2164 192 3073 1848 426 2270 3572 3456 217 3269\n" +
"140 1005 2063 3048 3742 3361 117 93 2695 1529 120 3480 3061 150 3383 190\n" +
"489 732 57 75 61 797 266 593 324 475 733 737 113 68 267 141\n" +
"3858 202 1141 3458 2507 239 199 4400 3713 3980 4170 227 3968 1688 4352 4168"
let rec processRow row high low =
match row with
| x :: rest ->
let newHigh = max x high
let newLow = min x low
processRow rest newHigh newLow
| [] -> high - low
let rec processRows rows checksum =
match rows with
| x :: rest ->
let rowDiff = processRow x System.Int32.MinValue System.Int32.MaxValue
processRows rest (rowDiff + checksum)
| [] -> checksum
let processInput (input:string) =
let rows = input.Split([|'\n'|], StringSplitOptions.RemoveEmptyEntries)
|> List.ofArray
|> List.map (fun x ->
x.Split([|' '|])
|> List.ofArray
|> List.map (fun x -> int x))
processRows rows 0
printfn "%i" (processInput input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment