-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.swift
53 lines (44 loc) · 1.08 KB
/
solution.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Foundation
let snafu_digits: [Character] = ["=", "-", "0", "1", "2"]
func snafu2dec(snafu: String) -> Int {
var dec = 0
for (index, char) in snafu.reversed().enumerated() {
let value = snafu_digits.firstIndex(of: char)! - 2
let multiplier = NSDecimalNumber(decimal: pow(5, index)).intValue
dec += value * multiplier
}
return dec
}
func dec2snafu(dec: Int) -> String {
var remaining = dec
var out = ""
while remaining > 0 {
switch remaining % 5 {
case 0:
out += "0"
remaining /= 5
case 1:
out += "1"
remaining /= 5
case 2:
out += "2"
remaining /= 5
case 3:
out += "="
remaining = (remaining + 2) / 5
case 4:
out += "-"
remaining = (remaining + 1) / 5
default:
fatalError()
}
}
return String(out.reversed())
}
let content = try String(contentsOfFile: "./input.txt")
let lines =
content
.trimmingCharacters(in: .whitespacesAndNewlines)
.components(separatedBy: .newlines)
let result = dec2snafu(dec: lines.map { snafu2dec(snafu: $0) }.reduce(0, +))
print(result)