learnings/PS
[๊ตฌ๋ฆLevel] ๊ท์น ์ซ์ ์ผ๊ตฌ Swift ํ์ด
Une.
2024. 6. 15. 13:17
๋ฌธ์ ์ ์๊ตฌ์ฌํญ์ ์ดํดํ๊ธฐ ํ๋ค์๋,,,
2๋จ๊ณ์์ fail์ผ ๋, ์ซ์ ๋ณํ ํ 2๋จ๊ณ๊ฐ ๋ชจ๋ ๋๋ ๊ฐ์ ๋ค์ ํ๊ฐํ์ฌ 3๋จ๊ณ๋ฅผ ์งํํ๋ ์ค ์ดํดํ๋ค.
1๋จ๊ณ์์๋ง ํ์ธํ์๋ strike๊ฐ์ ๋ํด์๋ง 3๋จ๊ณ๋ฅผ ์งํํ๋๋ก ์ ์ํ์.
์์ค์ฝ๋
๋๋ณด๊ธฐ
let answer = readLine()!.map{ Int(String($0))! }
var expectation = readLine()!.map{ Int(String($0))! }
func isCorrect()->Bool{
for i in 0..<4{
if answer[i] != expectation[i]{ return false}
}
return true
}
if isCorrect() { print(1) }
else{
var count = 1
step2: while !isCorrect(){
//2๋จ๊ณ ์ค ball์ ๋ฐ์์ ๋ฌด
var strikeIdx: [Int] = []
var isBalled = false
for i in 0..<4{
if expectation[i] == answer[i]{ //strike๋ผ๋ฉด ๋ค์ ์๋ฆฌ๊ฐ ๊ฒ์ฌ
strikeIdx.append(i)
continue }
if answer.contains(expectation[i]) == false{ //ํ์ฌ ์๋ฆฌ๊ฐ์ด fail์ด๋ผ๋ฉด
var newExpectNum = expectation[i]
while expectation.contains(newExpectNum) == true{ //๊ณ์ฐ๊ฐ์ด ๋ค๋ฅธ์๋ฆฌ์ ์กด์ฌํ์ง ์์ ๋ ๊น์ง ๋ฐ๋ณต
newExpectNum = (newExpectNum + 1) % 10
}
expectation[i] = newExpectNum
continue
}
isBalled = true
}
if isBalled{
var strikes: [Int] = []
for i in strikeIdx{
let strike = expectation[i]
strikes.append(strike)
}
for strike in strikes{
let idx = expectation.firstIndex(of:strike)!
expectation.remove(at:idx)
}
let shiftLast = expectation.removeLast()
expectation.insert(shiftLast, at:0)
for i in strikeIdx.indices{
expectation.insert(strikes[i], at: strikeIdx[i])
}
}
count += 1
}
print(count)
}