Skip to content

Instantly share code, notes, and snippets.

연습문제 4.1

(a)

12월 31일 말한 사람 승

12월 1일 말한 사람 패

11월 30일 말한 사람 승

연습문제 3.15

최소 시간

소요시간
4 4 4 1 1 1
4 4 4 1 1 1
1 4 4 4 1 4
1 1 4 4 4 1
  • 복귀자 1명일때 시간: (t.2 + t.1) + (t.3 + t.1) + t.4 = 2t.1 + t.2 + t.3 + t.4
  • 복귀자 2명일때 시간: (t.2 + t.1) + (t.4 + t.2) + t.2 = t.1 + 3t.2 + t.4

(최소 시간) = (2t.1 + t.2 + t.3 + t.4) $\downarrow$ (t.1 + 3t.2 + t.4) = (t.1 + t.2 + t.4) + { (t.1 + t.3) $\downarrow$ 2t.2 }

t는 각각 1, 2, 5, 10 이므로 대입하면 17분이다.

1번 사람이 더 빠른 경우는 1번 사람 3번 사람의 건너는 시간의 합이 2번 사람 건너는 시간의 두 배보다 작은 경우이므로

연습문제 3.6

세 칸 옮기기

-1 0 1 2 3 4 5
1
1 2 1
1 3 2 1
1 3 3 2 1

연습문제 3.5

정방향

-1 0 1 2 3 4 5
1
1 1 1
1 2 1 1
1 2 2 1 1
@sungkmi
sungkmi / APS3-4.md
Created April 12, 2024 11:43
AlgorithmicProblemSolving

연습문제 3.4

정방향

-1 0 1 2 3 4 5 6 7
1
1 1
1 1 1
1 1 1 1
package lascala.aoc2021.day1_10.day9
case class Point(row: Int, column: Int)
case class HeightMap(
map: Map[Point, Int],
numberOfRows: Int,
numberOfColumns: Int,
)
package lascala.aoc2021.day1_10.day8
opaque type Digit = Set[Char]
opaque type SignalPattern = Set[Digit]
def parse(s: String): (SignalPattern, Seq[Digit]) =
val Array(pattern, digits) = s.split(" \\| ")
def parsePart(s: String): Seq[Digit] =
s.split(" ").map(_.toSet).toSeq
(parsePart(pattern).toSet, parsePart(digits))
package lascala.aoc2021.day1_10.day7
def findMinCost(from: Int, to: Int, cost: Int => Int): Int =
val mid = (from + to) / 2
val fromCost = cost(from)
val toCost = cost(to)
if (from + 1) >= to then (fromCost min toCost)
else if fromCost >= toCost then findMinCost(mid, to, cost)
else findMinCost(from, mid, cost)
package lascala.aoc2021.day1_10.day6
opaque type School = Map[Int, BigInt]
extension (s: School)
def next: School =
val current: School = for (remainingDates, count) <- s if remainingDates > 0
yield (remainingDates - 1, count)
s.get(0).fold(current) { zeroesCount =>