Skip to content

Instantly share code, notes, and snippets.

@liammclennan
Created September 22, 2016 02:11
Show Gist options
  • Save liammclennan/675c84ccaf9803bd2e7c2d64f172964c to your computer and use it in GitHub Desktop.
Save liammclennan/675c84ccaf9803bd2e7c2d64f172964c to your computer and use it in GitHub Desktop.
// https://code.google.com/codejam/contest/351101/dashboard#s=p0
open System.Linq
type Case = { c: int; i: int; ps: int[]; index: int[] }
let readFile filename =
System.IO.File.ReadLines(filename)
let transpose arr =
let t = [|for a in [0..Array.max arr] -> -1|]
for v in [0..arr.Length-1] do
let m = arr.[v]
t.[m] <- v
t
let fileToCases (data:list<string>) =
let createCase sequence num =
let three = List.skip ((num-1) * 3) sequence |> List.take 3 |> Array.ofList
let i = three.[1]|>int
let bits = (three.[2] |> string).Split [|' '|] |> Array.map int
{c=three.[0]|>int; i=i; ps=bits; index=transpose bits}
let caseCount = List.head data |> int
let caseData = List.tail data
List.map (createCase caseData) [1..caseCount]
let solve (case:Case) =
seq {
for i in [0..Array.length case.ps-1] do
let value = case.ps.[i]
if value < case.c then
let diff = case.c - value
if diff < Array.length case.index && case.index.[diff] >= 0 then
yield (i,case.index.[diff])
} |> Seq.head
[<EntryPoint>]
let main argv =
let filename = argv.[0]
let sw = System.Diagnostics.Stopwatch.StartNew()
let data = readFile filename |> List.ofSeq
sw.Stop()
printfn "io %f" sw.Elapsed.TotalMilliseconds
let sw2 = System.Diagnostics.Stopwatch.StartNew()
let cases = fileToCases data
sw2.Stop()
printfn "parse %f" sw2.Elapsed.TotalMilliseconds
let sw3 = System.Diagnostics.Stopwatch.StartNew()
let solutions = List.map solve cases
sw3.Stop()
printfn "solve %f" sw3.Elapsed.TotalMilliseconds
printfn "%A" (solutions)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment