From 4c80ebd83abb1c250513d7b057067e795d97c50a Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 18 Dec 2020 07:52:26 +0100 Subject: [PATCH] day 18, part 2 --- day18/main.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/day18/main.swift b/day18/main.swift index d2cc3cb..ab97645 100644 --- a/day18/main.swift +++ b/day18/main.swift @@ -24,14 +24,17 @@ extension Scanner { } func expression() -> Int? { + 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 let op = self.op() { - guard let second = primary() else { fatalError() } - switch op { - case "+": result += second - case "*": result *= second - default: fatalError() - } + while string("+"), let second = primary() { + result += second } return result }