Skip to content

Instantly share code, notes, and snippets.

@matthid
Last active September 21, 2019 15:16
Show Gist options
  • Save matthid/8b67f36b8175521f02afa0946986cf0b to your computer and use it in GitHub Desktop.
Save matthid/8b67f36b8175521f02afa0946986cf0b to your computer and use it in GitHub Desktop.
// Runs in https://fable.io/repl/#
open System
type Intermediate = { Digit : int; Flow: int; LastWasZero : bool }
let t = [ 1 .. 9 ] |> List.map (fun i -> { Digit = i; Flow = 0; LastWasZero = false })
let nextRound (intermediate) =
let sum = intermediate.Digit * 2 + intermediate.Flow
let digit = sum % 10
let nextFlow = sum / 10
{ Digit = digit; Flow = nextFlow; LastWasZero = intermediate.Digit = 0 }
let checkFoundSingle startNum intermediate =
let ok = startNum = intermediate.Digit && intermediate.Flow = 0 && not intermediate.LastWasZero
if ok then Console.WriteLine(sprintf "Found OK: %A" intermediate)
ok
let checkFound list =
list
|> List.mapi (fun idx intermediate -> (idx, intermediate))
|> List.tryFind (fun (idx, intermediate) ->
checkFoundSingle (idx + 1) intermediate)
|> Option.map (fun (idx, _) -> idx + 1)
let y_0 = t |> List.map nextRound
//Fable.Core.JS.console.log(y_0)
//Console.WriteLine(y_0)
let mutable next = y_0
let mutable found = None
let mutable i = 0
while found.IsNone do
i <- i + 1
next <- next |> List.map nextRound
match checkFound next with
| Some num ->
Console.WriteLine(sprintf "%d worked in round %d" num i)
found <- Some num
| _ -> ()
Console.WriteLine(sprintf "Testing round %d" i)
match found with
| Some num ->
let start = { Digit = num; Flow = 0; LastWasZero = false }
let mutable start = [ nextRound start; start ]
while not (checkFoundSingle num start.Head) do
start <- nextRound start.Head :: start
let sol = start.Tail |> List.map (fun i -> i.Digit)
Console.WriteLine (sprintf "Solution %A" sol)
let num =
sol
|> List.rev
|> List.mapi (fun idx num -> bigint num * (System.Numerics.BigInteger.Pow(10I, idx)))
|> List.sum
Console.WriteLine (sprintf "Solution %A" num)
Console.WriteLine (sprintf "Times 2: %A" (num * 2I))
| _ -> ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment