challenge LL Golf Hole 3 - 13日の金曜日を数え上げる

今日から2013年12月31日までの、13日の金曜日とその総数を表示してください。

余力のあるものはこのプログラムを短くしてみたり、短くしてみたり、短くしてください。

※LL Future実行委員の高野光弘です。この出題は LL Future公式の出題であり、優れたものについてはLL Golfのセッションでご紹介させていただくかもしれません。ご理解の上、ご投稿ください。また、LL Futureのチケットは現在も発売中です。よろしければ、メインイベントの方にもぜひご参加ください。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env ruby
require 'date'

from = DateTime.now
to = DateTime.parse("2013-12-31")

friday = (from..to).inject(0) do |friday, date|
    if date.mday == 13 and date.wday == 5 then
        puts date.strftime('%Y-%m-%d')
        friday + 1
    else
        friday
    end
end

puts friday

Posted feedbacks - Scala

 素直に書いてみましたが,長いですね。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import    java.util.Calendar
import    java.text.SimpleDateFormat

class Friday13(from:Calendar) {
    
    var    f13:List[Calendar] = null
    val    to:Calendar = Calendar.getInstance
    to.setTime((new SimpleDateFormat("yyyy/MM/dd")).parse("2014/01/01"))
    
    def this() = this(Calendar.getInstance)
    
    def search:List[Calendar] = {
        val    d:Calendar = from.clone.asInstanceOf[Calendar]
        if (d.get(Calendar.DAY_OF_MONTH) > 13) {
            d.set(Calendar.DAY_OF_MONTH,1)
            d.add(Calendar.MONTH,1)
        }
        def search(l:List[Calendar],d:Calendar,to:Calendar):List[Calendar] = {
            d.before(to) match {
                case false => l
                case _ => {
                    val    n = d.clone.asInstanceOf[Calendar]
                    n.add(Calendar.MONTH,1)
                    (d.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) match {
                        case true => search(l+d,n,to)
                        case _ => search(l,n,to)
                    }
                }
            }
        }
        d.set(Calendar.DAY_OF_MONTH,13)
        f13 = search(List(),d,to)
        f13
    }
    
    def print:Unit = {
        def print(l:List[Calendar]):Unit = l match {
            case List() => ()
            case d::rest => {
                Console.printf("%04d/%02d/%02d\n",d.get(Calendar.YEAR),d.get(Calendar.MONTH) + 1,d.get(Calendar.DAY_OF_MONTH))
                print(rest)
            }
        }
        print(f13)
        Console.printf("%dday matched\n",f13.size)
    }
}

object Main extends Application {
    try {
        val    f:Friday13 = new Friday13(Calendar.getInstance)
        f.search
        f.print
    } catch {
        case e:Exception => e.printStackTrace
    }
}

 少し短くしてみました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import    java.util.Calendar
import    java.text.SimpleDateFormat

class Friday13 {
    
    val    to:Calendar = Calendar.getInstance
    to.setTime((new SimpleDateFormat("yyyy/MM/dd")).parse("2013/12/13"))
    
    def print:Unit = {
        def search(l:List[Calendar],d:Calendar):List[Calendar] = Calendar.getInstance().after(d) match {
            case true => l
            case _ => {
                val    n:Calendar = d.clone.asInstanceOf[Calendar]
                n.add(Calendar.MONTH,-1)
                (d.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) match {
                    case true => search(d::l,n)
                    case _ => search(l,n)
                }
            }
        }
        Console.printf("%dday matched\n",(search(List(),to).foldLeft(0) { (c,d) => Console.printf("%04d/%02d/%02d\n",d.get(Calendar.YEAR),d.get(Calendar.MONTH)+1,d.get(Calendar.DAY_OF_MONTH)); c + 1 }))
    }
}

object Main extends Application {
    try {
        (new Friday13).print
    } catch {
        case e:Exception => e.printStackTrace
    }
}

今回は結構まじめに実装してみました。
副作用のある操作は結構あるような気はしますが(Calendar周り)、なるべく副作用をさけ、かつ、再起呼び出しでカウントしてみました。
1
2
3
4
5
6
7
8
9
import java.util._
import java.util.Calendar._
def ci=Calendar.getInstance
val f=new java.text.SimpleDateFormat("yyyy-MM-dd")
val c=ci
val e={val v=ci;v.setTime(f.parse("2013-12-31"));v}
def fri={c.set(DAY_OF_MONTH, 13);c.get(DAY_OF_WEEK)==FRIDAY}
def fs(s:Int):Int=if(c.after(e)) s else{c.add(MONTH, 1);if(fri){println(f.format(c.getTime));fs(s+1)}else fs(s)}
println(fs(if(c.get(DAY_OF_MONTH)>=13&&fri) 1 else 0))

Index

Feed

Other

Link

Pathtraq

loading...