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? {
|
||||
guard var result = primary() else { return nil }
|
||||
while let op = self.op() {
|
||||
guard let second = primary() else { fatalError() }
|
||||
switch op {
|
||||
case "+": result += second
|
||||
case "*": result *= second
|
||||
default: fatalError()
|
||||
guard var result = addition() else { return nil }
|
||||
while string("*"), let second = addition() {
|
||||
result *= second
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func addition() -> Int? {
|
||||
guard var result = primary() else { return nil }
|
||||
while string("+"), let second = primary() {
|
||||
result += second
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue