From 4293c978fa03b2acb1561b0e766f85ee3303b526 Mon Sep 17 00:00:00 2001 From: Sang Yu Lee Date: Wed, 4 Feb 2026 03:51:08 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B0=A4=20=EC=83=8C=20=EB=82=A0=EB=B3=B4?= =?UTF-8?q?=EB=8B=A4=20=EC=9E=94=20=EB=82=A0=20=EB=8D=94=20=EB=A8=B8?= =?UTF-8?q?=EB=A6=AC=EA=B0=80=20=EC=95=88=20=EB=8F=8C=EC=95=84=EA=B0=80?= =?UTF-8?q?=EB=8A=94=20=EC=9D=B4=EC=9C=A0=EB=8A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Liv.swift" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "WEEK03/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_\353\260\251\352\270\210\352\267\270\352\263\241/Liv.swift" diff --git "a/WEEK03/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_\353\260\251\352\270\210\352\267\270\352\263\241/Liv.swift" "b/WEEK03/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_\353\260\251\352\270\210\352\267\270\352\263\241/Liv.swift" new file mode 100644 index 0000000..66b4279 --- /dev/null +++ "b/WEEK03/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_\353\260\251\352\270\210\352\267\270\352\263\241/Liv.swift" @@ -0,0 +1,44 @@ +// 프로그래머스 - 방금그곡 + +func solution(_ m: String, _ musicinfos: [String]) -> String { + var selected: (String, Int) = ("(None)", 0) + let heard = convertSheet(m) + + for music in musicinfos { + let info = music.split(separator: ",").map { String($0) } + guard info.count == 4 else { continue } + + let duration = toNumber(info[1]) - toNumber(info[0]) + let sheet = convertSheet(info[3]) + guard !sheet.isEmpty, duration > 0 else { continue } + + let repeatCount = duration / sheet.count + 1 + let played = String((String(repeating: sheet, count: repeatCount)).prefix(duration)) + + if played.contains(heard) && duration > selected.1 { + selected = (info[2], duration) + } + } + + return selected.0 +} + +func toNumber(_ str: String) -> Int { + let sections = str.split(separator: ":").map { String($0) } + guard sections.count == 2, + let min = Int(sections[0]), + let sec = Int(sections[1]) + else { return 0 } + return min * 60 + sec +} + +func convertSheet(_ str: String) -> String { + var sheet: String = str + while let point = sheet.firstIndex(of: "#") { + let target = sheet.index(before: point) + let note = sheet[target].lowercased() + sheet.removeSubrange(target...point) + sheet.insert(contentsOf: note, at: target) + } + return sheet +}