Skip to content

Instantly share code, notes, and snippets.

View daiksy's full-sized avatar

KASUYA, Daisuke daiksy

View GitHub Profile
@daiksy
daiksy / DateUtil.scala
Last active December 15, 2015 23:09
ScalaでC#っぽい Date ⇔ StringとかDate同士の大小比較とかするためのtrait
package util
import java.util.Date
import java.text.SimpleDateFormat
object DateUtil {
implicit class StringToDate(self: String) {
/**
* フォーマット指定された形式でDate型に変換する
*
@daiksy
daiksy / dq7Baroque.scala
Last active December 14, 2015 04:49
ドラクエ7のバロックタワーのパズルを解くためのコード。
// コードを読みやすいように名前をつけとこう.
val Up = 0
val Right = 1
val Down = 2
val Left = 3
type Direction = Int
/*
* 石像をあらわすcase class. 像の向きを状態として持つ.
* また、一回の動作で向きが90度回転するという振る舞いも定義.
// http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2025
// http://projecteuler.net/problem=25
val fib:Stream[BigInt] = BigInt(1) #:: fib.scanLeft(BigInt(1)){(a, b) => a + b }
def getDigits = (n: BigInt) => n.toString.size
fib.takeWhile(getDigits(_) <= 1000).zipWithIndex.find(t => getDigits(t._1) == 1000).get._2 + 1
@daiksy
daiksy / maxPrime.scala
Created December 19, 2012 00:38
ご祝儀用に予算内のMax素数を算出するコード
def maxPrime(n: Int) = {
def isPrime(n: Int) = Iterator.from(2).takeWhile(p => p * p <= n).forall(p => n % p != 0)
(1 to n).filter(isPrime).max
}
@daiksy
daiksy / problem20.scala
Created December 8, 2012 11:05
Project Euler Problem 20
(BigInt(100) to BigInt(1) by -1).reduce((x, y) => x * y).toString.toList.map(_.getNumericValue).sum
@daiksy
daiksy / irof.scala
Created December 3, 2012 12:46
irof_Advent
case class irof (formula: Boolean)(irofBlock: => Unit) {
formula match {
case true => irofBlock
case _ =>
}
def elof(elofBlock: => Unit): Unit = {
formula match {
case false => elofBlock
@daiksy
daiksy / gist:4180777
Created December 1, 2012 06:11 — forked from irof/Hoge.java
コレクションをぶんまわしてみる
List list = [
[color:"blue", weight:10],
[color:"red", weight:30],
[color:"blue", weight:50],
]
// Javaで
int weight1 = 0;
for (def e : list) {
if ("blue".equals(e.color))
@daiksy
daiksy / hentaiAdventCode_2.scala
Created November 30, 2012 14:42
hentaiAdventCode_2
trait hentaiConverter {
implicit def stringToHentaiChecker(s: String) = HentaiChecker(s)
case class HentaiChecker(s: String) {
def hentaiPower: Int = {
import scala.io.Source
val contents = Source.fromURL("http://www.google.co.jp/search?q=変態+%s".format(s), "utf8").getLines.mkString
val searchCountPattern = "約? ?([0-9,]+) ?件".r
@daiksy
daiksy / hentaiAdventCode_1.scala
Created November 30, 2012 14:37
hentaiAdventCode_1
trait hentaiConverter {
implicit def stringToHentaiChecker(s: String) = HentaiChecker(s)
case class HentaiChecker(s: String) {
def isHentai: Boolean = s == "変態"
}
}
@daiksy
daiksy / problem16.scala
Created November 25, 2012 15:17
Project Euler Problem 16
/**
* http://projecteuler.net/problem=16
* http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2016
*/
val pow = (base: Int, n: Int) => {
def calc(base: Int, n: Int, summary: BigInt): BigInt = {
n match {
case 1 => summary * base
case _ => calc(base, n - 1, summary * base)