Skip to content

Instantly share code, notes, and snippets.

@kaharlichenko kaharlichenko/easy217.fs
Last active Aug 29, 2015

Embed
What would you like to do?
Solution for /r/dailyprogrammer 217 easy challange without input parsing
// See https://www.reddit.com/r/dailyprogrammer/comments/3840rp/20150601_challenge_217_easy_lumberjack_pile/
let pileUpCollectionOnce distributeLogs collection logs =
let rec pileUpCollectionOnceRec acc element logs =
match acc, element, logs with
| acc, [], _ -> (List.rev acc, logs)
| acc, _, 0 -> (List.append (List.rev acc) element, logs)
| acc, head :: tail, logs ->
let newElement, remainingLogs = distributeLogs head logs
pileUpCollectionOnceRec (newElement :: acc) tail remainingLogs
pileUpCollectionOnceRec [] collection logs
let pileUpPileOnce targetPile pile logs =
if pile = targetPile
then pile + 1, logs - 1
else pile, logs
let pileUpRowOnce = pileUpPileOnce >> pileUpCollectionOnce
let pileUpGridOnce = pileUpRowOnce >> pileUpCollectionOnce
let pileUpGrid grid logs =
let minPile = grid |> Seq.collect id |> Seq.min
let rec pileUpGridRec grid logs targetPile =
if logs = 0 then grid
else
let newGrid, remainingLogs = pileUpGridOnce targetPile grid logs
pileUpGridRec newGrid remainingLogs (targetPile + 1)
pileUpGridRec grid logs minPile
let printRow (row, logsRemaining) =
printfn "row: %A, logs remaining: %d" row logsRemaining
let printGrid (grid, logsRemaining) =
printfn "grid: %A, logs remaining: %d" grid logsRemaining
pileUpRowOnce 1 [1; 2; 3; 1] 10 |> printRow
pileUpRowOnce 1 [1; 2; 3; 1] 2 |> printRow
pileUpRowOnce 1 [1; 2; 3; 1] 1 |> printRow
printfn ""
pileUpGridOnce 1 [[1; 1; 1]; [2; 1; 3]; [1; 4; 1]] 2 |> printGrid
pileUpGridOnce 1 [[1; 1; 1]; [2; 1; 3]; [1; 4; 1]] 10 |> printGrid
pileUpGridOnce 2 [[2; 2; 2]; [2; 2; 3]; [2; 4; 2]] 4 |> printGrid
printfn ""
pileUpGrid [[1; 1; 1]; [2; 1; 3]; [1; 4; 1]] 7 |> printfn "grid: %A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.