0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第10回オフラインリアルタイムどう書くの問題をF#で

Posted at

番兵 '!' を特別扱いするのは型設計がよろしくないけど、手早く書くならこれでもいいか。

honeycomb.fs
let board =
  [| "!!!!!!!!!".ToCharArray();
     "!!!!TUVW!".ToCharArray();
     "!!!kHIJX!".ToCharArray();
     "!!jSBCKY!".ToCharArray();
     "!iRGADLZ!".ToCharArray();
     "!hQFEMa!!".ToCharArray();
     "!gPONb!!!".ToCharArray();
     "!fedc!!!!".ToCharArray();
     "!!!!!!!!!".ToCharArray() |]

let directions =
  [| (0,-1); (1,-1); (1,0); (0,1); (-1,1); (-1,0) |]

let folder (x,y,_) i =
  let (dx,dy) = directions.[i]
  let (x',y') = (x+dx,y+dy)
  let c' = board.[y'].[x']
  if c' = '!' then (x,y,c') else (x',y',c')

let solve (s:string) =
  let xs =
    s.ToCharArray()
    |> Array.map (fun c -> (int c)-(int '0'))
    |> Array.scan folder (4,4,board.[4].[4])
    |> Array.map (fun (_,_,c) -> c)
  new string(xs)

(* for test *)

type TestResult = Success | Failure

let test target expected =
  let actual = solve target
  printfn "%A" (if expected = actual then Success else Failure)

[<EntryPoint>]
let main args =
  test "135004" "ACDABHS"
  test "1" "AC"
  test "33333120" "AENc!!b!M"
  test "0" "AB"
  test "2" "AD"
  test "3" "AE"
  test "4" "AF"
  test "5" "AG"
  test "4532120" "AFQPOEMD"
  test "051455" "ABSHSj!"
  test "23334551" "ADMb!cdeO"
  test "22033251" "ADLKLa!ML"
  test "50511302122" "AGSjkTHTU!VW"
  test "000051" "ABHT!!!"
  test "1310105" "ACDKJW!V"
  test "50002103140" "AGSk!HU!IVIU"
  test "3112045" "AEDKYXKC"
  test "02021245535" "ABCIJW!JIHBS"
  test "014204" "ABIBCIB"
  test "255230" "ADAGAEA"
  test "443501" "AFPefgQ"
  test "022321" "ABCKLZ!"
  test "554452" "AGRh!!Q"
  test "051024" "ABSHTUH"
  test "524002" "AGAFGSB"
  test "54002441132" "AGQRjSRhRSGA"
  test "11010554312" "ACJV!!UTkSHI"
  test "23405300554" "ADMNEFOFGRi!"
  test "555353201" "AGRih!gPQG"
  test "22424105" "ADLMabaLD"
  test "11340202125" "ACJKDCKJX!!J"
  test "4524451" "AFQFPf!P"
  test "44434234050" "AFPf!!e!!Pgh"
  test "00554040132" "ABHk!j!i!jRG"
  test "3440403" "AEOePfgf"
  test "111130" "ACJW!XW"
  test "21133343125" "ADKXYZ!a!Z!L"
  test "353511" "AEFOPFA"
  test "22204115220" "ADLZYLY!KY!X"
  test "03013541" "ABABICBGB"
  test "101344" "ACIVJCA"
  test "2432541" "ADENbNdN"
  test "45332242015" "AFQPedc!!NME"
  test "215453" "ADKCAGF"
  test "45540523454" "AFQh!i!RQg!!"
  test "42434302545" "AFEOd!!ONOef"
  0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?