Skip to content

Conversation

@giljihun
Copy link
Member

@giljihun giljihun commented May 11, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ‚น

image

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๊ตฌํ˜„ + ์‹œ๋ฎฌ ๋ฌธ์ œ๋Š” ์•„์ง ํด๋”๊ฐ€ ์—†๊ธธ๋ž˜ ์ •์„์ ์ธ ๋ฌธ์ œ ์ฐพ์•„์„œ ํ•˜๋‚˜ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค!

image

image

์ ‘๊ทผ

  1. 8x8 ์ฒด์ŠคํŒ ๋งŒ๋“ค๊ธฐ -> ์‹ค์ œ ๊ตฌํ˜„์ค‘์— ๋ฒ„๋ ค์กŒ์”๋‹ˆ๋‹ค.
  2. ์ด๋™ ์ปจํŠธ๋กค๋Ÿฌ ์ •์˜ (dx, dy)
  3. ์ด๋™ ๋กœ์ง ์ •์˜
  4. ๋Œ์„ ๋ฐ€์น˜๋Š” ๋กœ์ง ์ •์˜

ํ•ต์‹ฌ ๊ตฌํ˜„ ๋Œ€์ƒ

๋ณธ ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๊ตฌํ˜„์ค‘์—์„œ๋„
"์•ŒํŒŒ๋ฒณ -> ์ขŒํ‘œ" ๋ณ€ํ™˜ ์„ ์ž˜ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ธ๊ฑฐ๊ฐ™์Šต๋‹ˆ๋‹ค.

์™œ๋ƒ๋ฉด,

  1. ์‹ค์ œ ์ฝ”ํ…Œ์—์„œ ์•„์Šคํ‚ค/์Šค์นผ๋ผ ๋ณ€ํ™˜ ์ด ์ž˜ ๊ธฐ์–ต์ด ์•ˆ๋‚ ๊ฑฐ๊ฐ™์•„์„œ ๊ทธ๋ ‡๊ฒŒ ๋А๊ผˆ๊ณ .
  2. ์ƒ๊ฐ๋ณด๋‹ค ๋ณด๋“œ ๊ฒŒ์ž„/์•„์Šคํ‚ค๋ณ€ํ™˜ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ์ž์ฃผ ๋ณด์ด๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์‹ค์ œ ํ•จ์ˆ˜๋ถ„๋ฆฌ๋„ ์•„๋ž˜์ฒ˜๋Ÿผ ์ปจ๋ฒ„ํ„ฐ ๊ตฌํ˜„๋งŒ ํ–ˆ๋Š”๋ฐ์š”!

// MARK: - Functions
// asciiValue: ๋ฌธ์ž โ†’ ์ˆซ์ž (ex. โ€˜Aโ€™ โ†’ 65)
// UnicodeScalar: ์ˆซ์ž โ†’ ๋ฌธ์ž (ex. 65 โ†’ โ€˜Aโ€™)

func toRealCoordinate(_ pos: String) -> (Int, Int) {
    let x = Int(pos.first!.asciiValue! - 65)
    let y = 8 - Int(String(pos.last!))!
    return (x, y)
}

func toChessCoordinate(from position: (Int, Int)) -> String {
    let x = String(UnicodeScalar(position.0 + 65)!)
    let y = String(8 - position.1)
    return x + y
}

asciiValue ๋Š” ๋ฌธ์ž โ†’ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ: 'A'.asciiValue! == 65 โ†’ 'A' โ†’ 0์œผ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด - 65

UnicodeScalar ๋Š” ์ˆซ์ž โ†’ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ: UnicodeScalar(65)! == "A" โ†’ 0 โ†’ 'A'๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด + 65

์ด ๋‘๊ฐœ๋Š” ์ตํ˜€๋‘๋ฉด ๋ถ„๋ช… ํšจ์šฉ์ด ์žˆ์–ด๋ณด์ž…๋‹ˆ๋‹ค!

Int() ์ดˆ๊ธฐํ™”๋Š” ํ•ญ์ƒ ์˜ต์…”๋„(Int?)์„ ๋ฑ‰๋Š”๋‹ค.

// let movingCnt = Int(N.components(separatedBy: " ")[2])
guard let movingCnt = Int(N.components(separatedBy: " ")[2]) else { exit(0) }

๋ณธ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๊ธฐ์กด์— ์œ„์ฒ˜๋Ÿผ ์ž‘์„ฑํ–ˆ๋‹ค๊ฐ€ guard let์œผ๋กœ ๊ฐ์‹ผ ๋ถ€๋ถ„์ด ์žˆ์–ด์š”.
์ด์œ ๊ฐ€ ์™ค๊นŒ์š”?

// ์ด๋™ ๋กœ์ง
for _ in 0..<movingCnt {

์ด์œ ๋Š” movingCnt๋ฅผ ํ™œ์šฉํ•˜๋Š” for๋ฌธ์— ์žˆ์Šต๋‹ˆ๋‹ค.
guard let์œผ๋กœ ์˜ต์…”๋„์„ ์–ธ๋ž˜ํ•‘ ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด, movingCnt์—๋Š” ์˜ต์…”๋„ Int๊ฐ€ ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์˜ ์กฐ๊ฑด์œผ๋กœ ๋„ฃ์„ ์ˆ˜๊ฐ€ ์—†์ง€์š”.

Int()๋Š” ๋ณ€ํ™˜ ์‹คํŒจ ๊ฐ€๋Šฅ์„ฑ ๋•Œ๋ฌธ์— ํ•ญ์ƒ Int? ํƒ€์ž…์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ›„๊ธฐ

๋‹ค๋ฅธ ๊ตฌํ˜„์€ ์ผ๋ฐ˜์ ์ธ ๊ตฌํ˜„์ด๋ผ ๋”ฐ๋กœ ์ ์ง„ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.
์ฝ”๋“œ๋ณด๊ณ  ๊ถ๊ธˆํ•œ๊ฑฐ ์—ฌ์ญค๋ด์ฃผ์„ธ์š” !! ~

@sep037
Copy link
Collaborator

sep037 commented May 15, 2025

๋ฌด๊ฐ€ ์‰ฝ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋‚œ ์–ด๋ ค์› ๋‹ค
๋ฌธ์ž์—ด ์ชผ๊ฐœ๊ณ  ๋‚˜๋ˆ ์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„ ๊ตฌ๊ธ€๋ง ๋งŽ์ด ํ–ˆ์–ด์š” = ์ด๋Ÿฐ ๊ฑฐ ์•ˆ ์จ๋ณธ ๊ฒƒ ๊ฐ™์Œ = ๋‚˜์˜ ๊ฒฐํ•จ
๊ทธ๋ž˜๋„ ํ•™์Šต์ด ์ž‡์—‡๋”ฐ. ๋ณด๋žŒ์ฐจ๋‹ค.

์‹ฌ์ง€์–ด ๋ฌธ์ œ ์ž˜๋ชป ์ฝ๊ณ  ์ขŒํ‘œ๋กœ ์ƒ๊ฐํ•ด์„œ ์ € directionMap ๋ฐ˜๋Œ€๋กœ ๋„ฃ๊ณ  ์•ˆ๋ผ์„œ ์ ˆ๋งํ•จ

๊ทธ์น˜๋งŒ ํ•ด๋ƒ„
1์‹œ๊ฐ„ ๊ฑธ๋ฆฐ ๊ธธ ๋Œ€๋‹จํ•˜๋‹ค

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-15 19 41 31
//
//  main.swift
//  1063
//
//  Created by Seungeun Park on 5/15/25.
//

import Foundation

// ์œ„์น˜ ๋ฌธ์ž์—ด์„ ์ˆซ์ž ์ขŒํ‘œ๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜
func chessToTuple(pos: String) -> (Int, Int) {
    let col = Int(pos.first!.asciiValue! - Character("A").asciiValue!) // ์•ŒํŒŒ๋ฒณ ๋Œ€์‹  ์ˆซ์ž๋กœ ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์—ˆ์Œ
    let row = Int(String(pos.last!))! - 1                              // 1 ~ 8์„ 0 ~ 7๋กœ ๋ฐ”๊ฟˆ
    return (row, col) // ์™œ๋ƒ๋ฉด ์ด์ฐจ์› ๋ฐฐ์—ด ์“ธ ๊ฑฐ๋‹ˆ๊นŒ
}

// ์ˆซ์ž ์ขŒํ‘œ๋ฅผ ์ฒด์Šค ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜ -> ์ด๊ฑด ์ •๋‹ต ์ถœ๋ ฅ์„ ์œ„ํ•จ
func tupleToChess(_ row: Int, _ col: Int) -> String {
    let colChar = String(UnicodeScalar(col + Int(Character("A").asciiValue!))!)
    let rowStr = String(row + 1)
    return colChar + rowStr
}

let location = readLine()!.split(separator: " ")
let kingLocation = String(location[0])
let stoneLocation = String(location[1])
let N = Int(location[2])!

// ์œ„์น˜๋ฅผ ์ˆซ์ž ํŠœํ”Œ๋กœ ๋ณ€ํ™˜
var king = chessToTuple(pos: kingLocation)     // (row, col)
var stone = chessToTuple(pos: stoneLocation)   // (row, col)

let directionMap: [String: (Int, Int)] = [ // ๋”•์…”๋„ˆ๋ฆฌ๋ฐ–์— ์ƒ๊ฐ์ด ์•ˆ ๋‚ฌ์–ด์š”. ํ˜น์‹œ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์•ˆ ํ‘ผ ์‚ฌ๋žŒ ? (์•„์ง ๊ธธ ์ฝ”๋“œ ์•ˆ ๋ด„)
    "R": (0, 1),
    "L": (0, -1),
    "B": (-1, 0),
    "T": (1, 0),
    "RT": (1, 1),
    "LT": (1, -1),
    "RB": (-1, 1),
    "LB": (-1, -1)
]

for _ in 0..<N {
    let command = readLine()!
    let (dy, dx) = directionMap[command]! 

    let newKing = (king.0 + dy, king.1 + dx)

    if (0..<8).contains(newKing.0) && (0..<8).contains(newKing.1) { // ์ด ๋ฌธ๋ฒ•์€ ๊ตฌ๊ธ€๋งํ•จ ! -> ์ด๊ฒƒ๋„ ํ”ผ๋“œ๋ฐฑ ๋ฐ›๊ณ  ์‹ถ์–ด์š” ..
        if newKing == stone { // ์™•์ด๋ž‘ ๋Œ์ด๋ž‘ ์œ„์น˜๊ฐ€ ๊ฐ™์œผ๋ฉด
            // ๋Œ๋„ ๊ฐ™์€ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™
            let newStone = (stone.0 + dy, stone.1 + dx)
            // ๋Œ๋„ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ
            if (0..<8).contains(newStone.0) && (0..<8).contains(newStone.1) {
                king = newKing
                stone = newStone
            }
            // ๋Œ์ด ๋ฒ—์–ด๋‚˜๋ฉด ํ‚น๋„ ๋ชป ๊ฐ
        } else {
            // ๋Œ์ด ์—†์œผ๋‹ˆ ํ‚น๋งŒ ์ด๋™ -> ํ‚น์ด๋ผ๊ณ  ํ•˜๋‹ˆ๊นŒ ํ‚น๋ฐ›๋Š”๋ฐ ํ‚น ๋งž๊ธด ํ•จ
            king = newKing
        }
    }
}

print(tupleToChess(king.0, king.1))
print(tupleToChess(stone.0, stone.1))

Copy link
Collaborator

Choose a reason for hiding this comment

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

์˜ค ์ด๊ฑด ๋‚˜๋ž‘ ๊ฐ™๊ตฐ

Copy link
Collaborator

Choose a reason for hiding this comment

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

์˜ค ์ด๊ฒƒ๋„ ๋‚˜๋ž‘ ๊ฐ™๋‹ค

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ €๋Š” enum์œผ๋กœ ํ•ด์คฌ์–ด์š” !

enum Command: String {
    case R, L, B, T, LB, RB, LT, RT

    func getDirection() -> (row: Int, col: Int) {
        switch self {
        case .R: return (0, 1)
        case .L: return (0, -1)
        case .B: return (-1, 0)
        case .T: return (1, 0)
        case .LB: return (-1, -1)
        case .RB: return (-1, 1)
        case .LT: return (1, -1)
        case .RT: return (1, 1)
        }
    }
}

์˜๋„์ ์œผ๋กœ ์ผ€์ด์Šค๊ฐ€ ๋‚˜๋‰˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋Š” enum์„ ์ข€ ์จ๋ดค์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ• ๋•Œ๋Š” command.getDirection()ํ•˜๋ฉด์„œ ํ•ด๋‹น ์ปค๋งจ๋“œ์— ๋Œ€ํ•œ ์ด๋™ํ•  ์ขŒํ‘œ๊ฐ’์„ ๊ฐ€์ ธ์™”์–ด์š” !
๊ธธ์„ ์ƒ๋‹˜์ฝ”๋“œ๋ณด๋‹ˆ๊นŒ dictionary๋กœ ํ•˜๋ฉด ์ฐธ์กฐ ๋‘๋ฒˆ ์•ˆ๊ฑฐ์ณ๋„ ๋˜๊ณ  ํ›จ์”ฌ ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”!
์•„๋งˆ ์—ฌ๊ธฐ์„œ enum๋ฐฉ์‹์ด ๋น›์„ ๋ณด๋ ค๋ฉด ์—ฌ๊ธฐ(enum)์•ˆ์—์„œ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค๋˜์ง€.. ์ข€ ๋” ๋งŽ์€ ๊ฒƒ์„ ํ–ˆ์–ด์•ผ ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ท

Copy link
Member

Choose a reason for hiding this comment

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

์˜คํ˜ธ ์ €๋„ ๋”•์…”๋„ˆ๋ฆฌ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ enum์œผ๋กœ ํ•˜๋ฉด ํœด๋จผ ์—๋Ÿฌ ๋ฐฉ์ง€ ๋“ฑ์— ์ข‹๊ฒ ๋„ค์š”!

Copy link
Collaborator

Choose a reason for hiding this comment

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

ํ—‰ ์ด๊ฒƒ๋„ ๋‚˜๋ž‘ ๋˜‘๊ฐ™๋‹ค ใ…‹ใ…‹

Copy link
Collaborator

@MuchanKim MuchanKim May 17, 2025

Choose a reason for hiding this comment

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

๊ธ€๋กœ๋‹ˆ ์ฝ”๋“œ๋ž‘ ๊ฑฐ์˜ ๋˜‘๊ฐ™๋„ค์š”! 0..<8 ๋ฒ”์œ„๊ฐ€ ์ž์ฃผ ๋‚˜์˜ค๋Š”๋ฐ ์ด๋Ÿฐ ๊ฐ’์€ ์ƒ์ˆ˜๋กœ ๋นผ์ค˜๋„ ์ข‹์„ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค!

let chessMapRange: Range<Int> = 0..<8

Copy link
Member Author

Choose a reason for hiding this comment

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

๋งค์ง๋„˜๋ฒ„ ์กฐ์‹ฌํ•ด์•ผ๊ฒ ์–ด์š”!

@MuchanKim
Copy link
Collaborator

MuchanKim commented May 16, 2025

@sep037 ์ œ๊ฐ€ ์‰ฝ๋‹ค๊ณ  ํ•œ ์ด์œ ๋Š” ์ด๋ฏธ ํ’€์–ด๋ดค๋˜ ๋ฌธ์ œ์˜€๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ๊ฑฐ์˜€์–ด์š”.............. ^^ ๊ทธ๋ฆฌ๊ณ  ๊ธธ ๋ฌธ์ œ์น˜๊ณค ์‰ฌ์› ๋‹ค?

Copy link
Collaborator

@MuchanKim MuchanKim left a comment

Choose a reason for hiding this comment

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

์ฟ ์ฟ  ํ’€์–ด๋ดค๋˜ ๋ฌธ์ œ๋“ค์ด ๋“ฑ์žฅํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๊ตฐ์š”.
์ „์— ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์— ์ ์‘ํ•ด๋ณด์ž๊ณ  ์ด ๋ฌธ์ œ๋ฅผ ํ•™์Šตํ•˜๋ฉด์„œ ํ’€์—ˆ์—ˆ๋˜ ๊ธฐ์–ต์ด๋‚ฉ๋‹ˆ๋‹ค..
๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ’€์ง„ ์•Š๊ณ  ์˜ˆ์ „ ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋‹ค๋“ฌ๋Š” ์ •๋„๋กœ๋งŒ ํ–ˆ์Šต๋‹ˆ๋‹ค..!

๋‹ค๋“ค ์ขŒํ‘œ๋ฅผ ์•„์Šคํ‚ค๋กœ ๋ณ€ํ™˜ํ•˜์…จ๊ตฐ์š”? ์ˆ˜ํ•™์  ์ ‘๊ทผ.. ์ „ ์•„์ง ์–ด๋ ค์›Œ์š” ์ง๊ด€์ ์œผ๋กœ ํ’€๋ž˜์š”. ์ง๊ด€์ ์ธ๊ฒŒ ์ข‹์€๊ฑฐ๊ฐ€ํƒ€์š”~~(์ž”์†Œ๋ฆฌ ๊ฐ€๋Šฅ)

import Foundation

let moveFunctions: [String: (Int) -> Int] = [
    "R": { $0 + 1 },    
    "L": { $0 - 1 },    
    "T": { $0 + 8 },    
    "B": { $0 - 8 },    
    "RT": { $0 + 9 },   
    "LT": { $0 + 7 },   
    "RB": { $0 - 7 },   
    "LB": { $0 - 9 }    
]

// ํ‚น๊ณผ ๋Œ์ด ๊ฐ™์€ ์œ„์น˜์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
func checkSamePosition(king: Int, stone: Int) -> Bool {
    return king == stone
}

// ์ฃผ์–ด์ง„ ์œ„์น˜์—์„œ ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
func moveableCheck(position: Int, command: String) -> Bool {
    switch command {
    case "R": return (position + 1) % 8 == 0 ? false : true 
    case "L": return (position == 0 || position % 8 == 0) ? false : true  
    case "T": return 56...63 ~= position ? false : true  
    case "B": return 0...7 ~= position ? false : true  
    case "RT": return (position + 1) % 8 == 0 || 56...62 ~= position ? false : true  
    case "LT": return (position == 0 || position % 8 == 0) || 56...62 ~= position ? false : true  
    case "RB": return 0...7 ~= position || (position + 1) % 8 == 0 ? false : true  
    case "LB": return 0...7 ~= position || (position == 0 || position % 8 == 0) ? false : true  
    default: return false
    }
}

// ํ‚น๊ณผ ๋Œ์˜ ์ด๋™์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜
// ์ด๋™์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ nil์„ ๋ฐ˜ํ™˜
func processMove(king: Int, stone: Int, command: String) -> (king: Int, stone: Int)? {
    // ์ด๋™ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜๊ณ , ํ‚น์ด ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ
    guard let moveFunction = moveFunctions[command],
          moveableCheck(position: king, command: command) else {
        return nil
    }
    
    // ํ‚น์˜ ์ƒˆ๋กœ์šด ์œ„์น˜ ๊ณ„์‚ฐ
    let newKing = moveFunction(king)
    
    // ํ‚น์ด ๋Œ๊ณผ ๊ฐ™์€ ์œ„์น˜๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ
    if checkSamePosition(king: newKing, stone: stone) {
        // ๋Œ๋„ ๊ฐ™์€ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ
        guard moveableCheck(position: stone, command: command) else {
            return nil
        }
        // ํ‚น๊ณผ ๋Œ ๋ชจ๋‘ ์ด๋™
        return (newKing, moveFunction(stone))
    }
    
    // ํ‚น๋งŒ ์ด๋™
    return (newKing, stone)
}

// ์ฒด์ŠคํŒ์˜ ๋ชจ๋“  ์œ„์น˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•œ ๋ฐฐ์—ด
let chessMap: [String] = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1",
                          "A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2",
                          "A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3",
                          "A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4",
                          "A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5",
                          "A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6",
                          "A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7",
                          "A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8"]


// Input
let input: [String] = readLine()!.components(separatedBy: " ").map { $0 }

var kingPosition: Int = chessMap.firstIndex(of: input[0])!
var stonePosition: Int = chessMap.firstIndex(of: input[1])!
let cycle: Int = Int(input[2])!

for _ in 0..<cycle {
    let command: String = readLine()!
    
    // ์ด๋™์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์—๋งŒ ์œ„์น˜ ์—…๋ฐ์ดํŠธ
    if let newPositions = processMove(king: kingPosition, stone: stonePosition, command: command) {
        kingPosition = newPositions.king
        stonePosition = newPositions.stone
    }
}

// ์ตœ์ข… ์œ„์น˜ ์ถœ๋ ฅ
print(chessMap[kingPosition])
print(chessMap[stonePosition])

Comment on lines +17 to +20
Copy link
Collaborator

Choose a reason for hiding this comment

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

๋ฌธ์ž์—ด ์ชผ๊ฐค ๋•Œ split๋Œ€์‹  components๋ฅผ ์“ฐ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? split์ด substring์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— String ์”Œ์šฐ๋Š” ๊ฒƒ ๋ณด๋‹ค components๊ฐ€ ๋‚ซ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์…จ๋‚˜์š”?? ์ด๊ฒŒ ํšจ์œจ์ด ์ข‹๋‚˜์š”?? ํ˜น์€ ๊ฐ€๋…์„ฑ?? (๋ฌผ์Œํ‘œ์‚ด์ธ๋งˆ)

Copy link
Member Author

@giljihun giljihun May 18, 2025

Choose a reason for hiding this comment

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

๋ถˆํ•„์š”ํ•œ String ๋ณ€ํ™˜์ด ๋งŽ๋‹ค๋ฉด, split์ด ๋‚˜์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ
components๊ฐ€ ๋ณต์‚ฌ ๋น„์šฉ์ด ์žˆ๊ธฐ์— ์ผ๋ถ€ ์ฐจ์ด๊ฐ€ ์žˆ์ง€๋งŒ,
์ œ๊ฐ€ ์•Œ๊ธฐ๋กœ ์„ฑ๋Šฅ ์ฐจ์ด๊ฐ€ ๊ฑฐ์˜ ์—†๊ณ  ๋ณธ ๋ฌธ์ œ์—์„  ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ components๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ข‹๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@MuchanKim MuchanKim May 17, 2025

Choose a reason for hiding this comment

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

๊ธ€๋กœ๋‹ˆ ์ฝ”๋“œ๋ž‘ ๊ฑฐ์˜ ๋˜‘๊ฐ™๋„ค์š”! 0..<8 ๋ฒ”์œ„๊ฐ€ ์ž์ฃผ ๋‚˜์˜ค๋Š”๋ฐ ์ด๋Ÿฐ ๊ฐ’์€ ์ƒ์ˆ˜๋กœ ๋นผ์ค˜๋„ ์ข‹์„ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค!

let chessMapRange: Range<Int> = 0..<8

Comment on lines +68 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ขŒํ‘œ ๋ณ€ํ™˜์„ ์•„์Šคํ‚ค๋กœ ํ•˜์…จ๊ตฐ์š”.. ๋‹ค๋ฅธ ํ’€์ด๋‚˜ ๋ธ”๋กœ๊ทธ ๊ธ€๋„ ์ข€ ๋ดค๋Š”๋ฐ ๋Œ€๋ถ€๋ถ„์ด ์ด๋ ‡๊ฒŒ ํ’€์ดํ•˜์‹  ๋“ฏ! ์ข‹์€ ์ ‘๊ทผ์ž…๋‹ˆ๋‹ค~!

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด๋Ÿฐ๊ฒฝ์šฐ์—๋Š” ํ•ญ์ƒ

0 < nextPosOfKing.0 < 8 

์ด๋Ÿฐ์‹์œผ๋กœ ํ•ด์ค„ ์ƒ๊ฐ๋งŒํ–ˆ๋Š”๋ฐ, containsํ•˜๋‹ˆ๊นŒ ํ›จ์”ฌ ๊น”๋”ํ•œ๊ฒƒ๊ฐ™์•„์š”. ์ฐธ swift์Šค๋Ÿฌ์šด๊ฑธ ์ž˜ ์“ฐ์‹œ๋Š”๊ตฐ์š” ๋‹ค์Œ์— ์ž˜ ์จ๋จน๊ฒ ์Šต๋‹ˆ๋‹ท

Copy link
Member

Choose a reason for hiding this comment

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

์˜คํ˜ธ ๋งž๋„ค์š” contains๋ฅผ ์ž˜ ์‚ฌ์šฉํ•ด์„œ ๋ณด๊ธฐ ์ข‹๋„ค์š”!

Copy link
Member Author

Choose a reason for hiding this comment

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

์ €๋„ ์ฒ˜์Œ์จ๋ดค๋Š”๋ฐ. ์ œ ๊ธฐ์–ต์œผ๋กœ (0..<cnt).forEach ์ด๋Ÿฐ ๊ตฌ๋ฌธ์„ ์“ฐ๋ ค๊ณ ํ•˜๋ฉด์„œ
๋น„์Šทํ•œ ๊ฐ„๊ฒฐ์„ฑ์„ ๊ฐ€์ง„ .contains๋ฅผ ์ฐจ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค! ใ…‹ใ…‹

Copy link
Collaborator

@bishoe01 bishoe01 left a comment

Choose a reason for hiding this comment

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

แ„แ…ตแ†ผ-แ„ƒแ…ฉแ†ฏ แ„†แ…ฎแ†ซแ„Œแ…ฆ

import Foundation

typealias Position = (row: Int, col: Int)

enum Command: String {
    case R, L, B, T, LB, RB, LT, RT

    func getDirection() -> (row: Int, col: Int) {
        switch self {
        case .R: return (0, 1)
        case .L: return (0, -1)
        case .B: return (-1, 0)
        case .T: return (1, 0)
        case .LB: return (-1, -1)
        case .RB: return (-1, 1)
        case .LT: return (1, -1)
        case .RT: return (1, 1)
        }
    }
}


// var Board = Array(repeating: Array(repeating: 0, count: 8), count: 8)

func isRange(_ pos: Position) -> Bool {
    return pos.row >= 0 && pos.row <= 7 && pos.col >= 0 && pos.col <= 7
}

// ์ด๋ฏธ ๋ณ€ํ™˜ํ•œ ์นœ๊ตฌ๋ฅผ ๋‹ค์‹œ ์•„์Šคํ‚ค๋กœ ๋Œ๋ ค์˜ค๋Š” ์ž‘์—…
func translate(_ pos: Position) -> String {
    return String(UnicodeScalar(pos.col + 65)!) + String(pos.row + 1)
}

func solution(king locationKing: String, stone locationStone: String, _ n: Int, _ commands: [String]) {
    let king = Array(locationKing)
    let stone = Array(locationStone)
    var kingPos: Position = (row: Int(String(king[1]))! - 1, col: Int(king[0].asciiValue! - 65))
    var stonePos: Position = (row: Int(String(stone[1]))! - 1, col: Int(stone[0].asciiValue! - 65))

    // ์‹œ์ž‘์— ์ฒจ๋ถ€ํ•œ ์‚ฌ์ง„ ๋กœ์ง ๊ทธ๋Œ€๋กœ ํ๋ฆ„ ๊ฐ€์ ธ๊ฐ”์–ด์š” ! 
    // ๋กœ์ง ์‹œ์ž‘
    for command in commands {
        let command = Command(rawValue: command)!.getDirection()
        let newKingPos: Position = (row: kingPos.row + command.row, col: kingPos.col + command.col)
        // ๊ฐˆ ์ˆ˜ ์žˆ๋‚˜ ์—†๋‚˜ ?
        if !isRange(newKingPos) {
//            print("range")
            continue
        }
        else {
            // ๊ณต๋ฐฑ์— ํ‚น ์˜ฎ๊ธฐ๊ธฐ
            if newKingPos != stonePos {
                kingPos = newKingPos
            }
            else {
                let newStonePos = (row: stonePos.row + command.row, col: stonePos.col + command.col)
                // ๋Œ์ด ๋ชป๊ฐ„๋Œ€ !
                if !isRange(newStonePos) {
                    continue
                }
                // ๋Œ๋„ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋‘˜๋‹ค ์˜ฎ๊ธฐ๊ธฐ
                else {
                    kingPos = newKingPos
                    stonePos = newStonePos
                }
            }
        }
    }
    print(translate(kingPos))
    print(translate(stonePos))
}

์ €๋Š” ์ฒ˜์Œ์— ํ–‰(A ~ H) ์—ด(1 ~ 8) ์ด๋‹ˆ๊นŒ ์ด๊ฑธ ์›๋ž˜ ๋ณด๋“œ(2์ฐจ์›๋ฐฐ์—ด)์—์„œ ๊ฑด๋“œ๋ ค์ฃผ๋ ค๊ณ 
ํ–‰์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜์‹œ์ผœ์„œ 2์ฐจ์›๋ฐฐ์—ด์—์„œ ์šฉ์ดํ•˜๊ฒŒ ์ด๋™ํ•ด์ฃผ๋ ค๊ณ  ์‹œ์ž‘์„ ํ–ˆ๋Š”๋ฐ, ๋ง‰์ƒ ์™„์„ฑํ•˜๊ณ ๋ณด๋‹ˆ ๊ตณ์ด 2์ฐจ์›๋ฐฐ์—ด์ด ํ•„์š”์—†๊ฒ ๋‹ค ์‹ถ์–ด์„œ ๋นผ์คฌ์Šต๋‹ˆ๋‹ค.
๊ณ ๋ คํ• ๊ฑฐ๋Š”

  1. 0~7์—์„œ ๋ฒ—์–ด๋‚˜๋ƒ?
  2. ์ด๋™ํ•  ๊ณต๊ฐ„์— ๋Œ์ด ์žˆ๋ƒ?
  3. ์ด๋™ํ•  ๊ณต๊ฐ„์— ๋Œ์ด ์žˆ์œผ๋ฉด ๋Œ๋„ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋ƒ?
    ์ด๊ฑฐ 3๊ฐœ๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ๋‚˜์˜จ ์‚ฌ์ง„์˜ ํ๋ฆ„๋Œ€๋กœ ์กฐ๊ฑด๋ฌธ์˜ ํ๋ฆ„์„ ๋”ฐ๋ผ๊ฐ€์‹ ๋‹ค๋ฉด ์ดํ•ด๊ฐ€ ์‰ฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
    ๊ทธ๋ ‡๊ฒŒํ•˜๊ณ  ๋งˆ์ง€๋ง‰์— ์ด์ œ ๋‹ค์‹œ ๊ฒฐ๊ณผ๊ฐ’์ธ kingPos์™€ stonePos๋ฅผ translateํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์•„์Šคํ‚ค๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ถœ๋ ฅํ•ด์คฌ์–ด์š”.

// ์•„๋ž˜๋Š” ์›๋ž˜ ์‹œ๋„ํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋˜ chessPosition ๊ตฌ์กฐ์ฒด ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.
// ์—ฌ๊ธฐ์•ˆ์—์„œ isRange ํ•จ์ˆ˜๋„ ์“ฐ๊ณ , translate ํ•จ์ˆ˜๋„ ์“ฐ๊ณ  ํ•˜๋ คํ–ˆ๋Š”๋ฐ, ๋„ˆ๋ฌด ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๋ฐฉํ–ฅ์ด๋ผ์„œ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ• ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋”๋ผ๊ตฌ์š”..! ๊ทธ๋ž˜์„œ ์ค‘๋„ํ•˜์ฐจํ•˜์…จ์Šต๋‹ˆ๋‹ค.

struct ChessPosition {
    var row: Int
    var col: Int

    init(_ position: String) {
        let chars = Array(position)
        self.row = Int(String(chars[1]))! - 1
        self.col = Int(chars[0].asciiValue! - Character("A").asciiValue!)
    }
}

Copy link
Member

@alstjr7437 alstjr7437 left a comment

Choose a reason for hiding this comment

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

๋‹ค๋ฅธ ๋ถ„๋“ค ๋ฆฌ๋ทฐ๋ฅผ ๋‹ค ์ž˜๋‹ฌ์•„์ฃผ์…”์„œ ์ข‹๋„ค์š”!!

์ฒ˜์Œ์— ํ—‰ ์ฒด์Šค๊ฒŒ์ž„ ๊ตฌํ˜„์ธ๊ฐ€ ํ•˜๊ณ  ๊ฒ๋จน์—ˆ๋‹ค๊ฐ€ ๋‹คํ–‰์ด ์ด๋™์ด๋ผ ํŽธํ•˜๊ฒŒ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!
์ฝ”๋“œ๋Š” ๋‹ค๋ฅธ๋ถ„๋“ค์ด๋ž‘ ๊ฑฐ์˜ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™์•„์š”!

Copy link
Member

Choose a reason for hiding this comment

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

์˜คํ˜ธ ์ €๋„ ๋”•์…”๋„ˆ๋ฆฌ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ enum์œผ๋กœ ํ•˜๋ฉด ํœด๋จผ ์—๋Ÿฌ ๋ฐฉ์ง€ ๋“ฑ์— ์ข‹๊ฒ ๋„ค์š”!

Copy link
Member

Choose a reason for hiding this comment

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

์˜คํ˜ธ ๋งž๋„ค์š” contains๋ฅผ ์ž˜ ์‚ฌ์šฉํ•ด์„œ ๋ณด๊ธฐ ์ข‹๋„ค์š”!

Copy link
Collaborator

@YooGyeongMo YooGyeongMo left a comment

Choose a reason for hiding this comment

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

์ €๋Š” 0 Based indexing์œผ๋กœ ํ’€์–ด์„œ -1, +1 ์„ ํ•ด์ฃผ์–ด์•ผํ–ˆ๊ณ 

์ฒด์Šค๋ณด๋“œ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์ž์ฒด์ ์œผ๋กœ ์ฒดํฌํ•ด์ฃผ๋ฉด๋œ๋‹ค ์ƒ๊ฐํ•ด์„œ ๋”ฐ๋กœ ์ฒด์ŠคํŒ์— ์ถ”์ ํ•˜์ง€์•Š์•˜์Šต๋‹ˆ๋‹ท !

Comment on lines +17 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ž…๋ ฅ ๋””ํ…Œ์ผ ... He knows the way !!! ์บฌ..

Copy link
Collaborator

Choose a reason for hiding this comment

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

์˜ค 8*8 ํ–‰๋ ฌ์„ ๋งŒ๋“œ์…จ๊ตฐ์š” ! ์ด๋ฒˆ์— ์ €๋Š” ์•ˆ ๋งŒ๋“ค๊ณ  ์ž์ฒด์ ์œผ๋กœ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ์•ˆ ๋ฒ—์–ด๋‚˜๋Š”์ง€๋งŒ ํ™•์ธํ–ˆ์—ˆ์–ด์š” !

Comment on lines +39 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด ๋ถ€๋ถ„์€ ์ €์™€ ๋„ˆ๋ฌด ๋˜‘๊ฐ™์ด ํ’€์—ˆ๋Š”๋ฐ์š” ?

import Foundation

// ๊ฐ€์žฅ ์•„๋ž˜๊ฐ€ 1 ๊ฐ€์žฅ ์œ„๊ฐ€ 8 ํ–‰์€ -๊ฐ€ ์œ„๋กœ๊ฐ€๋Š”๊ฒƒ.
let direction: [String: (Int, Int)] = [
    "R": (1,0),
    "L": (-1,0),
    "B": (0,-1),
    "T": (0,1),
    "RT": (1,1),
    "LT": (-1,1),
    "RB": (1,-1),
    "LB": (-1,-1)
]

// .first/.last๋Š” ํ•ญ์ƒ Optional(Element?) ํƒ€์ž… , .asciiValue, unicodeScalar๋Š” ๊ฐ’์ด ์—†์„์ˆ˜๋„ ์žˆ์–ด์„œ Optional๋กœ ๋ฐ˜ํ™˜
func transformToNum(_ chess: String) -> (Int, Int) {
    let colValue = Int(chess.first!.asciiValue! - Character("A").asciiValue!)
    let rowValue = Int(String(chess.last!))! - 1// Int()๋Š” String, Double, Float, NSNumber๋“ฑ์€ ๋จ Char๋Š” X
    return (colValue, rowValue)
}

func transformToChess(_ target: (Int,Int)) -> String {
    let colChar = String(UnicodeScalar(target.0 + Int(Character("A").asciiValue!))!)
    let rowChar = String(target.1 + 1)
    
    return colChar + rowChar
}

let targetInfo = readLine()!.split(separator: " ").map{ String($0) }
var kingInfo =  transformToNum(targetInfo[0])
var stoneInfo = transformToNum(targetInfo[1])
let testCase = Int(targetInfo[2])!

func solution() {
    for _ in 0..<testCase {
        let moveInfo = readLine()!
        guard let move = direction[moveInfo] else  { continue }
        
        let nKing = (kingInfo.0 + move.0 , kingInfo.1 + move.1)
        
        // ์ฒด์ŠคํŒ ๋ฒ—์–ด๋‚˜๋ฉด ๋ฌด์‹œ 0 Based Indexing
        if nKing.0 < 0 || nKing.0 >= 8 || nKing.1 < 0 || nKing.1 >= 8 {
            continue
        }
        
        //ํ‚น์ด ์ด๋™ํ•  ์ž๋ฆฌ์— ๋Œ์ด ์žˆ์œผ๋ฉดใ… 
        if nKing == stoneInfo {
            let nStone = (stoneInfo.0 + move.0, stoneInfo.1 + move.1)
            
            if nStone.0 < 0 || nStone.0 >= 8 || nStone.1 < 0 || nStone.1 >= 8 {
                continue
            }
            
            //๋Œ ์ด๋™ ๊ฐ€๋Šฅํ•˜๋ฉด ใ„ฑใ„ฑ
            kingInfo = nKing
            stoneInfo = nStone
        } else {
            //๊ทธ๋ƒฅ ํ‚น๋งŒ
            kingInfo = nKing
        }
    }
    
    print(transformToChess(kingInfo))
    print(transformToChess(stoneInfo))
}

contains์™€ if or๋ฌธ and๋ฌธ ๋น„๊ต๊ฐ€ ๋ญ๊ฐ€ ๋” ๋น ๋ฅธ์ง€ ๊ถ๊ธˆํ•˜๋„ค์š” !

Copy link
Member Author

Choose a reason for hiding this comment

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

์ฐพ์•„๋ณด๋‹ˆ.. contains์‚ฌ์šฉ์œผ๋กœ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ๋„ ์žˆ๊ณ ํ•ด์„œ ์•ฝ๊ฐ„ ๋А๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ ๋ฏธ๋น„ํ•ด๋ณด์ž…๋‹ˆ๋‹ค.
๋ฐ๋ฏธ๊ฐ“์ฒ˜๋Ÿผ ์ง์ ‘ ์กฐ๊ฑด์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์€ ์—ฐ์‚ฐ์œผ๋กœ๋งŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‹ˆ ๋” ๋น ๋ฅผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

์ถ”๊ฐ€์ ์œผ๋กœ contains ์‚ฌ์šฉ์€ ๋ณด๋‹ค. ๋ณธ ๋ฌธ์ œ์—์„  ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๊ฒฐํ•œ ์ธก๋ฉด์—์„œ ์ข‹์•„๋ณด์ด๋„ค์š”!
๋ฌผ๋ก  ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋‚˜์—ดํ•˜๋ฉด~ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋‚˜ ์ด๋Ÿฐ ๋ถ€๋ถ„์—์„œ ๋”์šฑ ๊ฐ•์ ์ด ์žˆ์–ด๋ณด์—ฌ์š”!

@giljihun giljihun merged commit ed2bbc8 into main May 19, 2025
9 checks passed
@alstjr7437 alstjr7437 deleted the 4-giljihun branch May 23, 2025 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants