Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created November 21, 2014 13:32
Show Gist options
  • Save waynejo/9524611687277abadff7 to your computer and use it in GitHub Desktop.
Save waynejo/9524611687277abadff7 to your computer and use it in GitHub Desktop.
val input = "2 20 8 2 3 5"
val normalSpeed = 0.5f
val boostSpeed = 1
val split = input.split(" ").map(_ toInt)
val numBoosters = split(0)
val constructionTime = split(1)
val numStars = split(2)
val numDistances = split(3)
val distances = split.drop(4)
val stars = (0 until numStars).map{i => distances(i % numDistances)}.toList
println(stars)
val result = (0 until numStars).map{
starToPlaceBooster =>
stars.zipWithIndex.map{
case (d, index) =>
if (index == starToPlaceBooster) (index, d, true) else (index, d, false)
}
}
result.foreach(println)
def getTime(testSet:List[(Int, Int, Boolean)]):Float = {
testSet.foldLeft(0.0f)((accum, x) => {
val distance = x._2
val boost = x._3
val result = if (constructionTime > distance * 2 + accum || !boost) {
distance / normalSpeed + accum
} else if (accum > constructionTime)
distance / boostSpeed + accum
else {
val timeWithoutBoost = constructionTime - accum
val distanceWithoutBoost = timeWithoutBoost * normalSpeed
val distanceWithBoost = distance - distanceWithoutBoost
val timeWithBoost = distanceWithBoost / boostSpeed
timeWithoutBoost + timeWithBoost + accum
}
result
})
}
@swkimme
Copy link

swkimme commented Nov 21, 2014

같이 문제풀이한 비트윈의 김상우입니다. 정말 재밌었습니다!

@swkimme
Copy link

swkimme commented Nov 21, 2014

val input = "2 20 8 2 3 5"
//val input = "1 4 2 2 10 4"
val normalSpeed = 0.5f
val boostSpeed = 1
val split = input.split(" ").map(_ toInt)
val numBoosters = split(0)
val constructionTime = split(1)
val numStars = split(2)
val numDistances = split(3)
val distances = split.drop(4)

val stars = (0 until numStars).map{i => distances(i % numDistances)}.toList

case class StarInfo(val index: Int, val distance: Int, val boost: Boolean)

val exhaustiveTestSet = (0 until numStars).combinations(numBoosters).map{
  starIndexToPlaceBooster =>

    val boosters = (0 until numStars).map(i => starIndexToPlaceBooster.contains(i))
    stars.zipWithIndex.zip(boosters).map{case ((distance, index), boost) => StarInfo(index, distance, boost)}
}

def calculateTime(testSet:List[StarInfo]):Float = {
  testSet.foldLeft(0.0f)((accumTime, starInfo) => {
    val distance = starInfo.distance
    val boost = starInfo.boost

    val noBoost = !boost || constructionTime > (distance / normalSpeed) + accumTime
    val fullBoost = boost && accumTime > constructionTime

    val travelTime = if (noBoost) {
      distance / normalSpeed
    } else if (fullBoost) {
      distance / boostSpeed
    } else {
      val timeWithoutBoost = constructionTime - accumTime
      val distanceWithoutBoost = timeWithoutBoost * normalSpeed
      val distanceWithBoost = distance - distanceWithoutBoost
      val timeWithBoost = distanceWithBoost / boostSpeed
      timeWithoutBoost + timeWithBoost
    }
    travelTime + accumTime
  })
}

val testResult = exhaustiveTestSet.map(calculateTime _)

val answer = testResult.min

@swkimme
Copy link

swkimme commented Nov 21, 2014

정답도 구해지는 답안 첨부합니다~!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment