day 18, part 2

This commit is contained in:
Sven Weidauer 2020-12-18 07:52:26 +01:00
parent 76cf560634
commit 4c80ebd83a

View file

@ -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
} }