Euler: Problem 102

Posted by YpsilonTAKAI On 2020年3月14日土曜日 1 コメント
これも5年前に解いたやつだ。 思い出しながら書く。

内部に原点を含む三角形を判定する問題です。

外積を計算して同じ向きかどうかを判定しています。

(ns euler.pe-102)
;; Prablem 102
;; "Elapsed time: 1107.975434 msecs"
(defn calc-alfa [[ax ay] [bx by] [cx cy]
[px py]]
(/ (+ (* (- by cy) (- px cx))
(* (- cx bx) (- py cy)))
(+ (* (- by cy) (- ax cx))
(* (- cx bx) (- ay cy)))))
(defn calc-beta [[ax ay] [bx by] [cx cy]
[px py]]
(/ (+ (* (- cy ay) (- px cx))
(* (- ax cx) (- py cy)))
(+ (* (- by cy) (- ax cx))
(* (- cx bx) (- ay cy)))))
(defn triangle-includes-origin? [[a b c]]
(let [p [0 0]
alfa (calc-alfa a b c p)
beta (calc-beta a b c p)]
(and (<= 0 alfa 1)
(<= 0 beta 1)
(<= 0 (+ alfa beta) 1))))
(defn line-to-points [s]
(->> (clojure.string/split s #",")
(map #(Integer/parseInt %) ,,)
(partition 2 ,,)))
(defn pe-102 [triangles-txt]
(with-open [f (clojure.java.io/reader triangles-txt)]
(->> (line-seq f)
(map line-to-points ,,)
(map triangle-includes-origin? ,,)
(filter true? ,,)
(count ,,))))
(pe-102 "https://projecteuler.net/project/resources/p102_triangles.txt")
view raw pe_102.clj hosted with ❤ by GitHub

1 コメント:

paddioborny さんのコメント...

Casinos Near Atlanta - DRMCD
We 경기도 출장마사지 are a part of the MGA 안동 출장샵 Casino Rewards Network. 부천 출장안마 The MGA offers $100 free to play, $1,000 창원 출장안마 in monthly rewards in 익산 출장안마 weekly contests, Weekly contests,

コメントを投稿