Created
May 19, 2021 12:33
-
-
Save koher/c1f8687b2c85563cab85d3a9333bce3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let pp = readLine()!.split(separator: " ") | |
let e = pp[0] | |
let y = Int(pp[1])! | |
let m = Int(pp[2])! | |
let d = Int(pp[3])! | |
let dd = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
struct YMD: Comparable { | |
let y: Int | |
let m: Int | |
let d: Int | |
init(_ y: Int, _ m: Int, _ d: Int) { | |
self.y = y | |
self.m = m | |
self.d = d | |
} | |
static func <(lhs: YMD, rhs: YMD) -> Bool { | |
if lhs.y < rhs.y { | |
return true | |
} else if lhs.y > rhs.y { | |
return false | |
} else { | |
if lhs.m < rhs.m { | |
return true | |
} else if lhs.m > rhs.m { | |
return false | |
} else { | |
return lhs.d < rhs.d | |
} | |
} | |
} | |
} | |
let mRange = ...YMD(1912, 7, 29) | |
let tRange = YMD(1912, 7, 30)...YMD(1926, 12, 25) | |
let sRange = YMD(1926, 12, 25)...YMD(1989, 1, 7) | |
let hRange = YMD(1989, 1, 8)...YMD(2019, 4, 30) | |
let rRange = YMD(2019, 5, 1)... | |
func solve() -> YMD? { | |
let ymd: YMD | |
switch e { | |
case "M": | |
ymd = YMD(y + 1867, m, d) | |
guard mRange.contains(ymd) else { return nil } | |
case "T": | |
ymd = YMD(y + 1911, m, d) | |
guard tRange.contains(ymd) else { return nil } | |
case "S": | |
ymd = YMD(y + 1925, m, d) | |
guard sRange.contains(ymd) else { return nil } | |
case "H": | |
ymd = YMD(y + 1988, m, d) | |
guard hRange.contains(ymd) else { return nil } | |
case "R": | |
ymd = YMD(y + 2018, m, d) | |
guard rRange.contains(ymd) else { return nil } | |
default: | |
preconditionFailure() | |
} | |
let isLeapYear = ymd.y.isMultiple(of: 4) && (!ymd.y.isMultiple(of: 100) || ymd.y.isMultiple(of: 400)) | |
if ymd.m == 2, isLeapYear { | |
guard d <= 29 else { return nil } | |
} else { | |
guard d <= dd[ymd.m - 1] else { return nil } | |
} | |
return ymd | |
} | |
if let ymd = solve() { | |
print(ymd.y, ymd.m, ymd.d) | |
} else { | |
print(-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment