-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnailfish.swift
More file actions
203 lines (184 loc) · 4.77 KB
/
Snailfish.swift
File metadata and controls
203 lines (184 loc) · 4.77 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import Foundation
var data = try String(contentsOfFile: "data.txt")
/*data = """
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
"""*/
let lines = data.split(char: "\n").map()
{$0.trimmingCharacters(in: .whitespacesAndNewlines)}
indirect enum Fish {
case LITERAL(Int)
case PAIR(Fish, Fish)
func add(_ amount: Int, _ l: Bool) -> Fish {
switch self {
case .LITERAL(let val):
return .LITERAL(val + amount)
case .PAIR(let lef, let rig):
if l {
return .PAIR(lef.add(amount, true), rig)
}
else {
return .PAIR(lef, rig.add(amount, false))
}
}
}
func value() -> Int {
switch self {
case .LITERAL(let val):
return val
default:
return 0
}
}
func __explode__(_ level: Int = 0) -> (Fish, (l: Int, r: Int)?) {
switch self {
case .LITERAL(_):
return (self, nil)
case .PAIR(let lef, let rig):
if level >= 4 {
return (.LITERAL(0), (lef.value(), rig.value()))
}
var expl = lef.__explode__(level + 1)
if let result = expl.1 {
if result == (l: 0, r: 0) {
return (.PAIR(expl.0, rig), (l: 0, r: 0))
}
return (.PAIR(expl.0, rig.add(result.r, true)), (l: result.l, 0))
}
var expr = rig.__explode__(level + 1)
if let result = expr.1 {
if result == (l: 0, r: 0) {
return (.PAIR(lef, expr.0), (l: 0, r: 0))
}
return (.PAIR(lef.add(result.l, false), expr.0), (l: 0, result.r))
}
return (self, nil)
}
}
func explode() -> (Fish, Bool) {
switch self {
case .LITERAL(_):
return (self, false)
case .PAIR(let lef, let rig):
var expl = lef.__explode__(1)
if let result = expl.1 {
if result == (l: 0, r: 0) {
return (.PAIR(expl.0, rig), true)
}
return (.PAIR(expl.0, rig.add(result.r, true)), true)
}
var expr = rig.__explode__(1)
if let result = expr.1 {
if result == (l: 0, r: 0) {
return (.PAIR(lef, expr.0), true)
}
return (.PAIR(lef.add(result.l, false), expr.0), true)
}
return (self, false)
}
}
func magnitude() -> Int {
switch self {
case .LITERAL(let value):
return value
case .PAIR(let lef, let rig):
return lef.magnitude() * 3 + rig.magnitude() * 2
}
}
func split() -> (Fish, Bool) {
switch self {
case .LITERAL(let value):
if value > 9 {
return (.PAIR(.LITERAL(Int((Float(value) / 2.0))), .LITERAL(Int(ceil(Float(value) / 2.0)))), true)
}
return (self, false)
case .PAIR(let lef, let rig):
let spl = lef.split()
if spl.1 {
return (.PAIR(spl.0, rig), true)
}
let spr = rig.split()
if spr.1 {
return (.PAIR(lef, spr.0), true)
}
return (self, false)
}
}
func sum(_ other: Fish) -> Fish {
return .PAIR(self, other)
}
func reduce() -> Fish {
var cond = true
var copy = self
while cond {
let expl = copy.explode()
if expl.1 {
copy = expl.0
}
else {
let spl = copy.split()
if spl.1 {
copy = spl.0
}
else {
break
}
}
}
return copy
}
func description() -> String {
switch self {
case .LITERAL(let value):
return value.description
case .PAIR(let lef, let rig):
return "[" + lef.description() + ", " + rig.description() + "]"
}
}
}
func parse(_ str: String) -> Fish {
let list = str.trimmingCharacters(in: .whitespacesAndNewlines)
if let ans = Int(list) {
return .LITERAL(ans)
}
let interior = list[1 ..< list.count]
var level = 0
var i = 0
for char in interior {
if char == "," && level == 0 {
return .PAIR(parse(interior[0..<i+1]), parse(interior[i+1...interior.count]))
}
if char == "[" {
level += 1
}
if char == "]" {
level -= 1
}
i += 1
}
return .LITERAL(-69)
}
/*let fish = parse("[[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]]")
print(fish.reduce())*/
let twofish = lines.map() {parse($0)}
let bluefish = twofish
var best = 0
for onefish in twofish {
for redfish in bluefish {
if !(onefish.magnitude() == redfish.magnitude()) {
let ans = onefish.sum(redfish).reduce().magnitude()
if ans > best {
best = ans
}
}
}
}
print(best)