@@ -45,6 +45,12 @@ struct Formula {
4545 return startingLeft
4646 }
4747 var ns = s.trimmingCharacters(in: .whitespaces)
48+ if ns == "0" {
49+ return .constant(false)
50+ }
51+ if ns == "1" {
52+ return .constant(true)
53+ }
4854 if ns.trimmingCharacters(in: .whitespaces).reduce(true, {$0 && CharacterSet.letters.contains($1.unicodeScalars.first!)}) {
4955 vars.insert(String(s))
5056 return .variable(String(s))
@@ -59,6 +65,16 @@ struct Formula {
5965 print("Internal error: weird variable name first charecter \(ns.first!)")
6066 exit(1)
6167 }
68+ switch ns.first! {
69+ case "0":
70+ ns = ns.dropFirst().trimmingCharacters(in: .whitespaces)
71+ return "0"
72+ case "1":
73+ ns = ns.dropFirst().trimmingCharacters(in: .whitespaces)
74+ return "1"
75+ default:
76+ break
77+ }
6278 var i = ns.startIndex
6379 while i < ns.endIndex {
6480 if !CharacterSet.letters.contains(ns[i].unicodeScalars.first!) {
@@ -123,6 +139,7 @@ private func extractBrackets(from s: String) -> (wasInside: Substring, after: Su
123139private typealias Variable = String
124140private indirect enum FormulaElement {
125141 case variable(Variable)
142+ case constant(Bool)
126143 case operation(FormulaElement, Operator, FormulaElement)
127144 func evaluate(with variables: [Variable: Bool]) -> Bool {
128145 switch self {
@@ -135,6 +152,8 @@ private indirect enum FormulaElement {
135152 }
136153 case .operation(let l, let op, let r):
137154 return op.value(l.evaluate(with: variables), r.evaluate(with: variables))
155+ case .constant(let b):
156+ return b
138157 }
139158 }
140159}