LoginSignup
0
0

More than 5 years have passed since last update.

1日1個 @nabetani さんの作った問題を解くAdventCalendarの17日目です。

今日の問題は http://nabetani.sakura.ne.jp/hena/ord3ynode/ にあります。

{-# LANGUAGE TupleSections #-}
module Doukaku.YRoad (solve) where

solve :: String -> String
solve = map snd . scanl next' ('B', 'A')
  where
    next' (f, t) d = (t, next f t d)

route :: Char -> (Char, Char, Char)
route 'A' = ('D', 'C', 'B')
route 'B' = ('A', 'C', 'E')
route 'C' = ('A', 'F', 'B')
route 'D' = ('F', 'A', 'E')
route 'E' = ('B', 'F', 'D')
route 'F' = ('D', 'E', 'C')

next :: Char -> Char -> Char -> Char
next f t d = case d of
  'r' -> r
  'l' -> l
  _   -> f
  where
    (l, r) = case route t of
               (n1, n2, n3) | n1 == f -> (n2, n3)
                            | n2 == f -> (n3, n1)
                            | n3 == f -> (n1, n2)

各点についてつながっている点を時計回りに定義してあげて、それを元に左右を判定させてます。実に素直ですね。

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