Skip to content

Commit

Permalink
Merge pull request #25 from AlgorithmWithMe/heejik/5week
Browse files Browse the repository at this point in the history
[장희직] 킹, 바닥 장식, 빙고, 랜선 자르기, 가로수
  • Loading branch information
jhg3410 authored Oct 16, 2022
2 parents 67fa0d8 + 386cd21 commit 2e3b86e
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/kotlin/heejik/5week/가로수.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package heejik.`5week`

fun main() {

val n = readln().toInt()
val tree = mutableListOf<Int>()
val diff = mutableListOf<Int>()
var gcd = 1

repeat(n) {
val location = readln().toInt()
if (tree.isNotEmpty()) {
diff.add(location - tree.last())
}
tree.add(location)
}

gcd = getGcd(diff[0], diff[1])
for (i in 2 until diff.size) {
gcd = getGcd(gcd,diff[i])
}

println(((tree.last() - tree.first()) / gcd) + 1 - tree.size)

}

fun getGcd(_a: Int, _b: Int): Int {
var a = _a
var b = _b
while (b > 0) {
val tmp = a
a = b
b = tmp % b
}
return a
}
46 changes: 46 additions & 0 deletions src/main/kotlin/heejik/5week/랜선 자르기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package heejik.`5week`

import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.system.exitProcess

val lines = mutableListOf<Long>()

fun main(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
var answer = 0L
val (k, n) = readLine().split(' ').map { it.toInt() }
var s = 0L

repeat(k) {
val line = readLine().toLong()
lines.add(line)
s += line
}

var start = 0L
var end = s / n
if (end == 1L) {
println(1)
exitProcess(0)
}
while (start <= end) {
val mid = (end + start) / 2
val cnt = isCorrect(mid)
if (cnt >= n) {
answer = mid
start = mid + 1
} else {
end = mid - 1
}
}

println(answer)
}

fun isCorrect(length: Long): Long {
var cnt = 0L
lines.forEach {
cnt += it / length
}
return cnt
}
26 changes: 26 additions & 0 deletions src/main/kotlin/heejik/5week/바닥 장식.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package heejik.`5week`

fun main() {
var answer = 0

val (n, m) = readln().split(' ').map { it.toInt() }

val floor = arrayListOf<String>().apply {
repeat(n) {
add(readln())
}
}

repeat(n) { i ->
answer += floor[i].split('|').filter { it.isNotEmpty() }.size
}
repeat(m) { j ->
var tmp = ""
repeat(n) { i ->
tmp += floor[i][j]
}
answer += tmp.split('-').filter { it.isNotEmpty() }.size
}

println(answer)
}
67 changes: 67 additions & 0 deletions src/main/kotlin/heejik/5week/빙고.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package heejik.`5week`

import kotlin.system.exitProcess

val bingo = arrayListOf<MutableList<Int>>()

fun main() {
var cnt = 0
var bingoCnt = 0
val chairMan = arrayListOf<MutableList<Int>>()

repeat(5) {
bingo.add(readln().split(' ').map { it.toInt() }.toMutableList())
}
repeat(5) {
chairMan.add(readln().split(' ').map { it.toInt() }.toMutableList())
}

chairMan.forEachIndexed { _, ints ->
ints.forEachIndexed { _, num ->
bingo.forEachIndexed { _, row ->
if (num in row) {
cnt += 1
row[row.indexOf(num)] = -1
if (isBingo() >= 3) {
println(cnt)
exitProcess(0)
}
bingoCnt = 0
}
}
}
}
}

fun isBingo(): Int {
var cnt = 0
var tmp = 0
repeat(5) { i ->
if (bingo[i].filter { it == -1 }.size == 5) cnt += 1
}

repeat(5) { j ->
tmp = 0
repeat(5) { i ->
if (bingo[i][j] == -1) {
tmp += 1
}
if (tmp == 5) cnt += 1
}
}

tmp = 0
repeat(5) { i ->
if (bingo[i][i] == -1) tmp += 1
}
if (tmp == 5) cnt += 1

tmp = 0
repeat(5) { i ->
if (bingo[i][4 - i] == -1) tmp += 1
}
if (tmp == 5) cnt += 1

return cnt
}

44 changes: 44 additions & 0 deletions src/main/kotlin/heejik/5week/킹.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package heejik.`5week`


val y = listOf('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
val move = listOf("R", "L", "B", "T", "RT", "LT", "RB", "LB")
val dx = listOf(0, 0, -1, 1, 1, 1, -1, -1)
val dy = listOf(1, -1, 0, 0, 1, -1, 1, -1)

fun main() {

var (king, rock, n) = readln().split(' ')

repeat(n.toInt()) {
val location = move.indexOf(readln())

var kingX = (king[1].digitToInt()) - 1
var kingY = y.indexOf(king[0])

var rockX = (rock[1].digitToInt()) - 1
var rockY = y.indexOf(rock[0])

val kingNx = kingX + dx[location]
val kingNy = kingY + dy[location]


if ((kingNx in 0..7 && kingNy in 0..7).not()) return@repeat

if (kingNx == rockX && kingNy == rockY) {
val rockNx = rockX + dx[location]
val rockNy = rockY + dy[location]
if ((rockNx in 0..7 && rockNy in 0..7).not()) return@repeat
rockX = rockNx
rockY = rockNy
}
kingX = kingNx
kingY = kingNy

king = (y[kingY].toString()) + (kingX + 1).toString()
rock = (y[rockY].toString()) + (rockX + 1).toString()

}
println(king)
println(rock)
}

0 comments on commit 2e3b86e

Please sign in to comment.