day 18, part 2
This commit is contained in:
parent
76cf560634
commit
4c80ebd83a
1 changed files with 10 additions and 7 deletions
|
@ -24,14 +24,17 @@ extension Scanner {
|
||||||
}
|
}
|
||||||
|
|
||||||
func expression() -> Int? {
|
func expression() -> Int? {
|
||||||
guard var result = primary() else { return nil }
|
guard var result = addition() else { return nil }
|
||||||
while let op = self.op() {
|
while string("*"), let second = addition() {
|
||||||
guard let second = primary() else { fatalError() }
|
result *= second
|
||||||
switch op {
|
|
||||||
case "+": result += second
|
|
||||||
case "*": result *= second
|
|
||||||
default: fatalError()
|
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func addition() -> Int? {
|
||||||
|
guard var result = primary() else { return nil }
|
||||||
|
while string("+"), let second = primary() {
|
||||||
|
result += second
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue