Created
January 5, 2013 04:50
-
-
Save tnoda/4459823 to your computer and use it in GitHub Desktop.
#mitori_clj Project Euler Problem 19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns tnoda.projecteuler.problem-19 | |
(:import (org.joda.time DateTime) ; [joda-time/joda-time "2.1"] | |
(org.joda.time.chrono GregorianChronology))) | |
(defn- a-month-later | |
[^DateTime dt] | |
(.plusMonths dt 1)) | |
(defn- sunday? | |
[^DateTime dt] | |
(= 7 (.getDayOfWeek dt))) | |
(defn- twentieth-century? | |
[^DateTime dt] | |
(= 20 (.getCenturyOfEra dt))) | |
(defn solver | |
[] | |
(->> (DateTime. 1901 1 1 0 0 (GregorianChronology/getInstance)) | |
(iterate a-month-later) | |
(filter sunday?) | |
(take-while twentieth-century?) | |
count)) |
基本的に問題文にある情報だけ使って地道に計算した例です。
何の工夫も無いんで、ちょっと恥かしいです。 gistに貼るまでも無いかと思ったので、ブログのままです。
(このころはまだgistをembedできることを知らなかったもので)
http://ypsilonbox.blogspot.jp/2011/05/euler-problem-19.html
そういえば、文中が1900年の1月1日なのに、質問が1901年からになっているのに気づかなくて、答えが合わなくてかなり悩んだのを思い出しました。計算のときに、1900から1900までを引いているのはそれです。
こんなやりかたなのに、かなり速いですね。
Joda Time、いいですね。そういえば最近日付関係の演算って業務でやってないな...。
これくらいシンプルだとあえて Clojure の wrapper を作るまでもないのでしょうが、自分は filter
とか take-while
とかの述語にカッコをなるべく付けないのが好きなので、tnoda さんのように sunday?
とか twentieth-century?
は多分自分も同じように作ってしまうのでしょうね。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Joda Time 覚えておきます.
Java ライブラリ, タイプヒントの扱い, いつも参考になります.
いろいろハードコードでやっつけですが, ライブラリを使わず, 与えられた情報から手動で計算したので, 貼っておきます.
https://gist.github.com/4472323