Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Week3] 전현수: 단풍잎 이야기, 킹, 크로스워드, 랜선 자르기 #14

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

soopeach
Copy link
Member

Issue Number

Issue #12


💎 문제 해결


추가로 푼 문제

🔥 특이사항/질문

흑.... 이번주 너무 바빠서 도피를 풀지 못했습니다ㅜㅜㅜ
죄송합니다ㅜㅡㅜ

@soopeach soopeach self-assigned this Oct 17, 2022
@soopeach soopeach linked an issue Oct 17, 2022 that may be closed by this pull request
Copy link
Member

@gongdongho12 gongdongho12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

드디어 리뷰완료!


fun solution() {

val (keyCnt, questCnt, skillPerQuest) = readln().split(" ").map { it.toInt() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skillPerQuest 변수 사용 안하고 있네요!
각각의 항목들에 어떤 용도인지 명시해주는 부분 좋은것 같아요 👍🏼

Comment on lines +38 to +41
repeat(questCnt) {
val quests = readln().split(" ").map { it.toInt() }
questDataMap.add(quests)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add를 반복적으로 하는것 보다 애초에 초기화를 동시에 적용하게 repeat 대신에 Array 사용하고 바로 questDataMap 에 반영하는 구조는 어떠신가요?

questDataMap = Array(questCnt) {
    readln().split(" ").map { it.toInt() }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러면 lateinit var questDataMap: Array<List<Int>> 로 선언하는게 효율적이겠죠?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리스트보다... Array에 입력과 동시에 초기화.. 좋은 것 같습니다!!

Comment on lines +50 to +54
questDataMap.forEach { needKeyList ->
if (needKeyList.all { it in pickedKey }) {
curCanClearQuestCnt++
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분 count 함수로 쓰면 ++ 연산 없이 될것같아요

val curCanClearQuestCnt = questDataMap.count { it.all { it in pickedKey } }

Comment on lines +37 to +41
var canGetLanCnt = 0L
// 조건
lanList.forEach { lan ->
canGetLanCnt += lan / mid
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sumOf 사용해보시는건 어떤가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오... 그러네요 고차함수를 쓸 생각을 못했네요...!!!

Comment on lines +22 to +27
val lanList = mutableListOf<Long>()

repeat(haveCnt) {
val lanInfo = readln().toLong()
lanList.add(lanInfo)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정이 필요없는 타입인것 같은데 초기화하면서 넣어주는 방식은 어떨까요?

val lanList = Array(haveCnt) {
    readln().toLong()
}

Comment on lines +94 to +98
if ((king.x + nx in 0..7).not() ||
(king.y + ny in 0..7).not()
) {
return@repeat
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분도 contain 함수로 교체가능하죠?

if (!nKing.contain(0..7, 0..7)) {
    return@repeat
}

Comment on lines +90 to +91
val nx = dir.position.x
val ny = dir.position.y
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oprator 재정의해서!

val nKing = king + dir.position

Comment on lines +100 to +117
if (king.x + nx == stone.x &&
king.y + ny == stone.y
) {
// 돌이 움직일 수 있는 위치라면
if (stone.x + nx in 0..7 &&
stone.y + ny in 0..7
) {
// 돌 이동 후 킹 이동
stone.x += nx
stone.y += ny
king.x += nx
king.y += ny
}
// 킹이 이동할 위치에 돌이 없다면 바로 이동
} else {
king.x += nx
king.y += ny
}
Copy link
Member

@gongdongho12 gongdongho12 Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분도 위랑 동일하게 공통화 할 수 있겠죠

if (nKing == stone) {
    val nStone = stone + dir.position
    // 돌이 움직일 수 있는 위치라면
    if (nStone.contain(0..7, 0..7)) {
        // 돌 이동 후 킹 이동
        stone = nStone
        king = nKing
    }
    // 킹이 이동할 위치에 돌이 없다면 바로 이동
} else {
    king = nKing
}

Comment on lines +120 to +121
println(positionToCoordinate(king))
println(positionToCoordinate(stone))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

출력은 toString에 정의했으니 그냥 출력하면됩니다

println(king)
println(stone)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) 개행은 한번에 하면 더 좋겠죠?

println("$king\n$stone")

Comment on lines +16 to +136
fun solution() {

val (kingLocation, stoneLocation, moveCnt) = readln().split(" ")

val king = GameCharacter()
coordinateToPosition(kingLocation).apply {
king.x = this.x
king.y = this.y
}

val stone = GameCharacter()
coordinateToPosition(stoneLocation).apply {
stone.x = this.x
stone.y = this.y
}

repeat(moveCnt.toInt()) {
val command = readln()
lateinit var dir: Dir
when (command) {
(Dir.R).toString() -> {
dir = Dir.R
}

(Dir.L).toString() -> {
dir = Dir.L
}

(Dir.B).toString() -> {
dir = Dir.B
}

(Dir.T).toString() -> {
dir = Dir.T
}

(Dir.RT).toString() -> {
dir = Dir.RT
}

(Dir.LT).toString() -> {
dir = Dir.LT
}

(Dir.RB).toString() -> {
dir = Dir.RB
}

(Dir.LB).toString() -> {
dir = Dir.LB
}
}

val nx = dir.position.x
val ny = dir.position.y

// 킹이 이동할 수 없는 위치면 명령 무시
if ((king.x + nx in 0..7).not() ||
(king.y + ny in 0..7).not()
) {
return@repeat
}
// 킹이 이동할 위치에 돌이 있다면
if (king.x + nx == stone.x &&
king.y + ny == stone.y
) {
// 돌이 움직일 수 있는 위치라면
if (stone.x + nx in 0..7 &&
stone.y + ny in 0..7
) {
// 돌 이동 후 킹 이동
stone.x += nx
stone.y += ny
king.x += nx
king.y += ny
}
// 킹이 이동할 위치에 돌이 없다면 바로 이동
} else {
king.x += nx
king.y += ny
}

}
println(positionToCoordinate(king))
println(positionToCoordinate(stone))
}

fun coordinateToPosition(coordinate: String): Position {
val (xPos, yPos) = coordinate.chunked(1)
return Position(
Alphabet.values().indexOf(Alphabet.valueOf(xPos)),
yPos.toInt() - 1
)
}

fun positionToCoordinate(character: GameCharacter): String {
return "${(Alphabet.values()[character.x])}${(character.y) + 1}"
}

}
Copy link
Member

@gongdongho12 gongdongho12 Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체적으로 정리하면

class `전현수_킹` {

    data class Position(val x: Int, val y: Int)

    data class GameCharacter(var x: Int = 0, var y: Int = 0) {
        operator fun plus(pos: Position) = run {
            GameCharacter(x + pos.x, y + pos.y)
        }

        fun contain(xRange: IntRange, yRange: IntRange) = x in xRange && y in yRange

        constructor(pos: Position) : this(pos.x, pos.y)

        override fun toString(): String {
            return "${Alphabet.values()[x]}${(y) + 1}"
        }
    }

    enum class Dir(val position: Position) {
        R(Position(1, 0)),
        L(Position(-1, 0)),
        B(Position(0, -1)),
        T(Position(0, 1)),
        RT(Position(1, 1)),
        LT(Position(-1, 1)),
        RB(Position(1, -1)),
        LB(Position(-1, -1))
    }

    enum class Alphabet {
        A, B, C, D, E, F, G, H
    }

    fun solution() {

        val (kingLocation, stoneLocation, moveCnt) = readln().split(" ")

        var king = GameCharacter(coordinateToPosition(kingLocation))
        var stone = GameCharacter(coordinateToPosition(stoneLocation))

        repeat(moveCnt.toInt()) {
            val command = readln()

            val dir: Dir = Dir.valueOf(command)

            val nKing = king + dir.position
            // 킹이 이동할 수 없는 위치면 명령 무시
            if (!nKing.contain(0..7, 0..7)) {
                return@repeat
            }
            // 킹이 이동할 위치에 돌이 있다면
            if (nKing == stone) {
                val nStone = stone + dir.position
                // 돌이 움직일 수 있는 위치라면
                if (nStone.contain(0..7, 0..7)) {
                    // 돌 이동 후 킹 이동
                    stone = nStone
                    king = nKing
                }
                // 킹이 이동할 위치에 돌이 없다면 바로 이동
            } else {
                king = nKing
            }
        }
        println("$king\n$stone")
    }

    fun coordinateToPosition(coordinate: String): Position {
        val (xPos, yPos) = coordinate.chunked(1)
        return Position(
            Alphabet.values().indexOf(Alphabet.valueOf(xPos)),
            yPos.toInt() - 1
        )
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

훨씬 더 객체 지향적으로... 너무 이쁘고 깔끔하네요 코드

@gongdongho12 gongdongho12 added the review complete 리뷰 완료 label Oct 22, 2022
Comment on lines +100 to +101
if (king.x + nx == stone.x &&
king.y + ny == stone.y
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같은 객체의 data class면 자동으로 항목 비교가 되게끔!

king == stone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review complete 리뷰 완료
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[3주차 문제]
2 participants