๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ™‚ | Silver

[Kotlin, S4] ๋ฐฑ์ค€ 9242๋ฒˆ ํญํƒ„ ํ•ด์ฒด

by immgga 2024. 9. 20.

์ถœ์ฒ˜: unsplash.com

 

ํญํƒ„ ํ•ด์ฒด(9242๋ฒˆ)

Silver 4

#๊ตฌํ˜„ #๋ฌธ์ž์—ด #ํŒŒ์‹ฑ

https://www.acmicpc.net/problem/9242

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

๋ณ„ ๊ธฐํ˜ธ(*)์™€ ๊ณต๋ฐฑ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆซ์ž๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.

์ˆซ์ž๋Š” 7 segment ์ˆซ์ž ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

๊ทธ๋ƒฅ ์ˆซ์ž๋ฅผ ํŒŒ์‹ฑ ํ•ด์„œ 6์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด beer๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด boom์„ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ด๋‹ค.

 

๋˜ํ•œ ์•”ํ˜ธ๊ฐ€ ๋ฌด์กฐ๊ฑด 7 segment์˜ ์ˆซ์ž๋“ค๋งŒ ๋‚˜์˜ค๋Š” ๊ฒƒ๋„ ์•„๋‹ˆ๋‹ค.

์•”ํ˜ธ๋Š” 3 * 5์˜ ํ˜•ํƒœ๋กœ 1์นธ์”ฉ ๊ณต๋ฐฑ์„ ๋„๊ณ  ์ฃผ์–ด์ง€๊ฒŒ ๋˜๋Š”๋ฐ, 3 * 5์˜ ์•”ํ˜ธ ํ˜•์‹์„ ์ง€ํ‚ค์ง€ ์•Š์€ ์ฝ”๋“œ๊ฐ€ ์˜ฌ ์ˆ˜ ๋„ ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ์œ„ 2๊ฐ€์ง€ ์ƒํ™ฉ์— ๋งž๋Š” ์ฒ˜๋ฆฌ(boom)๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val codes = mutableListOf<List<String>>()
    for (i in 0 until 5) {
        val codeLine = readLine().map { it.toString() }
        codes.add(codeLine)
    }

    try {
        var codeParseResult = ""
        val subCode = mutableListOf<List<String>>()
        for (j in codes[0].indices) {
            if (j % 4 == 0) {
                for (k in 0 until 5) {
                    val subCodeLine = codes[k].subList(j, j + 3)
                    subCode.add(subCodeLine)
                }

                val res = parseCode(subCode)
                codeParseResult += res
                subCode.clear()
            }
        }

        if (codeParseResult.toInt() % 6 == 0) println("BEER!!")
        else println("BOOM!!")
    } catch (e: Exception) {
        println("BOOM!!")
    }
}

private fun parseCode(compareValue: List<List<String>>): String {
    val zero = listOf(
        listOf("*", "*", "*"),
        listOf("*", " ", "*"),
        listOf("*", " ", "*"),
        listOf("*", " ", "*"),
        listOf("*", "*", "*"),
    )
    val one = listOf(
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*")
    )
    val two = listOf(
        listOf("*", "*", "*"),
        listOf(" ", " ", "*"),
        listOf("*", "*", "*"),
        listOf("*", " ", " "),
        listOf("*", "*", "*"),
    )
    val three = listOf(
        listOf("*", "*", "*"),
        listOf(" ", " ", "*"),
        listOf("*", "*", "*"),
        listOf(" ", " ", "*"),
        listOf("*", "*", "*"),
    )
    val four = listOf(
        listOf("*", " ", "*"),
        listOf("*", " ", "*"),
        listOf("*", "*", "*"),
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*")
    )
    val five = listOf(
        listOf("*", "*", "*"),
        listOf("*", " ", " "),
        listOf("*", "*", "*"),
        listOf(" ", " ", "*"),
        listOf("*", "*", "*"),
    )
    val six = listOf(
        listOf("*", "*", "*"),
        listOf("*", " ", " "),
        listOf("*", "*", "*"),
        listOf("*", " ", "*"),
        listOf("*", "*", "*")
    )
    val seven = listOf(
        listOf("*", "*", "*"),
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*"),
        listOf(" ", " ", "*")
    )
    val eight = listOf(
        listOf("*", "*", "*"),
        listOf("*", " ", "*"),
        listOf("*", "*", "*"),
        listOf("*", " ", "*"),
        listOf("*", "*", "*")
    )
    val nine = listOf(
        listOf("*", "*", "*"),
        listOf("*", " ", "*"),
        listOf("*", "*", "*"),
        listOf(" ", " ", "*"),
        listOf("*", "*", "*")
    )

    return when (compareValue) {
        zero -> "0"
        one -> "1"
        two -> "2"
        three -> "3"
        four -> "4"
        five -> "5"
        six -> "6"
        seven -> "7"
        eight -> "8"
        nine -> "9"
        else -> throw Exception("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ์ˆซ์ž ํ˜•์‹")
    }
}

 

๋ฌธ์ œ ํ’€์ด

์ฝ”๋“œ ํ˜•์‹์ด ํ•œ ์ค„์— 3๊ฐœ์˜ ๋ฌธ์ž ๋˜๋Š” ๊ณต๋ฐฑ๊ณผ 1๊ฐœ์˜ ๊ณต๋ฐฑ์ด ๋ฐ˜๋ณต๋˜๋Š” ํ˜•์‹์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ฝ”๋“œ์˜ ๋‹จ์–ด์˜ ์ฒซ ๋ฒˆ์งธ ์ฐจ๋ก€๊ฐ€ ๋˜๋ฉด ๋ฐ˜๋ณต์œผ๋กœ ์ž…๋ ฅ๋ฐ›์€ ์ฝ”๋“œ์˜ 3๊ฐœ์˜ ๋‹จ์–ด๋ฅผ 5๋ฒˆ ๋ฐ˜๋ณตํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฃผ์–ด์งˆ ๋•Œ,

***   * * * *** *** *** ***
* *   * * *   *   *   * *  
* *   * *** *** *** *** ***
* *   *   * *     * *   * *
***   *   * *** *** *** ***

์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ ๋‹จ์–ด๋ฅผ 1๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

***
* *
* *
* *
***

  *
  *
  *
  *
  *
  
* *
* *
***
  *
  *
  
. . .

์•„๋ž˜์™€ ๊ฐ™์ด ๋‹จ์–ด๋ฅผ 1๊ฐœ์”ฉ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ถˆ๋Ÿฌ์˜จ ๋‹จ์–ด๋ฅผ 2์ค‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜๊ณ , parseCode๋ฅผ ์ด์šฉํ•ด ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

 

parseCode์—๋Š” 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ 7 segment ์ˆซ์ž ํ‘œ๊ธฐ๋ฒ• ํ˜•ํƒœ๋กœ ์ •์˜ํ•œ ํ›„, ๋“ค์–ด์˜จ code์™€ ๊ฐ™์€ ์ˆ˜๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆ˜๊ฐ€ ๋ชจ๋‘ ํ•ด๋‹น์ด ์•ˆ ๋˜๋ฉด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ์ฝ”๋“œ๊ฐ€ ์˜จ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ Exception์„ throw ํ•ด์ฃผ๊ณ  parseCode ๋กœ์ง์„ try catch์—์„œ ์ง„ํ–‰ํ•œ๋‹ค.

catch๋กœ ์˜จ ๊ฒฝ์šฐ์—๋Š” parseCode์—์„œ error๊ฐ€ ๋˜์ ธ์ง„ ์ƒํ™ฉ์— ์˜ค๊ธฐ ๋•Œ๋ฌธ์— boom์„ ์ถœ๋ ฅํ•˜๊ณ , ์ œ๋Œ€๋กœ ๋œ ์ˆซ์ž๋ฅผ ํŒŒ์‹ฑ ํ•œ ํ›„, int๋กœ ๋ฐ”๊พผ ์ˆ˜๊ฐ€ 6์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง„ ๊ฒฝ์šฐ๋Š” beer๋ฅผ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด boom์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

๋‹จ์ˆœ ๋…ธ๊ฐ€๋‹ค ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค.

0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ 7 segment ์ˆซ์ž ํ‘œ๊ธฐ๋ฒ•์„ ๋ฏธ๋ฆฌ ์ •์˜ํ•˜๊ณ  ์˜ฌ๋ฐ”๋ฅธ ์ˆ˜๋ฅผ parsing ํ•ด์•ผ ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Š” ๊ทผ๋ฐ ๋งŽ์ด ์–ด๋ ต์ง€ ์•Š๋‹ค.

์ฝ”๋“œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๋นผ๋จน์–ด ํ‹€๋ฆฌ๋Š” ์ผ์ด ์—†๋„๋ก ํ•˜์ž.

728x90