Skip to content

Instantly share code, notes, and snippets.

@ypsilon-takai
Created June 25, 2015 16:13
Show Gist options
  • Save ypsilon-takai/c61d35267769e51fc794 to your computer and use it in GitHub Desktop.
Save ypsilon-takai/c61d35267769e51fc794 to your computer and use it in GitHub Desktop.
Euler: Problem 102
(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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment