LoginSignup
0
0

More than 5 years have passed since last update.

1日1個 @nabetani さんの作った問題を解く、どう書くAdventCalendarの23日目です。

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

module Doukaku.LAndLs (solve) where
import Data.Char (digitToInt)

type L = (Pt, Pt, Pt)
type Pt = (Int, Int)

parse :: String -> (L, L)
parse (x1:y1:'-':x2:y2:'-':x3:y3:',':x1':y1':'-':x2':y2':'-':x3':y3':_) =
  (((digitToInt x1, digitToInt y1), (digitToInt x2, digitToInt y2),
    (digitToInt x3, digitToInt y3)),
   ((digitToInt x1', digitToInt y1'), (digitToInt x2', digitToInt y2'),
    (digitToInt x3', digitToInt y3')))

solve :: String -> String
solve input = show . length $ [(x, y) | x <- [0 .. 9], y <- [0 .. 9]
                                       , contains l1 (x, y) && contains l2 (x, y)]
  where
    (l1, l2) = parse input

contains :: L -> Pt -> Bool
contains (p1, p2, p3) p = squareContains (p1, p2) p || squareContains (p1, p3) p

squareContains :: (Pt, Pt) -> Pt -> Bool
squareContains ((x1, y1), (x2, y2))  (x, y) =
  (x1 - x) * (x2 - x) <= 0 && (y1 - y) * (y2 - y) <= 0

L字に含まれるかを判定するcontainsを実装し、100マスのうち双方のL字に含まれるマスの数を数えました。L字は長方形2つで構成されるため、長方形に含まれるかを判定するsquareContainsを実装して、この関数の||として表現しました。

http://qiita.com/Nabetani/items/4c60f10b73812e86441c に他の方の回答もありますので、見ると参考になるでしょう。

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