Skip to content

Instantly share code, notes, and snippets.

@kaharlichenko
Last active August 29, 2015 14:24
Show Gist options
  • Save kaharlichenko/3f27895e8c46461f14f3 to your computer and use it in GitHub Desktop.
Save kaharlichenko/3f27895e8c46461f14f3 to your computer and use it in GitHub Desktop.
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