boB Rudis
3 years ago
7 changed files with 146 additions and 10 deletions
@ -0,0 +1,106 @@ |
|||
import Foundation |
|||
|
|||
public class advent04 { |
|||
|
|||
public static func day_04_01() -> Int { |
|||
|
|||
let reqKeys: Set = [ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ] |
|||
|
|||
guard var input = try? String(contentsOfFile: "/tmp/test.txt", encoding: .utf8) |
|||
.components(separatedBy: "\n") else { |
|||
return(-1) |
|||
} |
|||
|
|||
input.insert("", at: 0) |
|||
|
|||
let res: [[String : String]] = input.reduce(into: []) { |
|||
($1 == "") ? $0.append([$1]) : $0[$0.index(before: $0.endIndex)].append($1) |
|||
} |
|||
.filter { $0.count > 1 } |
|||
.map { |
|||
$0.filter { !$0.isEmpty } |
|||
.joined(separator: " ") |
|||
.components(separatedBy: " ") |
|||
.filter { !$0.starts(with: "cid") } |
|||
.reduce(into: [String: String]()) { |
|||
let res = $1.split(separator: ":") |
|||
$0[String(res[0])] = String(res[1]) |
|||
} |
|||
} |
|||
|
|||
return( |
|||
res.map { reqKeys.symmetricDifference(Set($0.keys)) } |
|||
.filter { $0.count == 0 } |
|||
.count |
|||
) |
|||
|
|||
} |
|||
|
|||
static func validateHeight(_ x: String) -> Bool { |
|||
let amt = Int(x.replacingOccurrences(of: "[[:alpha:]]", with: "", options: [.regularExpression]))! |
|||
let unit = x.replacingOccurrences(of: "[[:digit:]]", with: "", options: [.regularExpression]) |
|||
return((unit == "in") ? amt.between(59, 76) : amt.between(150, 193)) |
|||
} |
|||
|
|||
public static func day_04_02() -> Int { |
|||
|
|||
let reqKeys: Set = [ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ] |
|||
let hairColors = [ "amb", "blu", "brn", "gry", "grn", "hzl", "oth" ] |
|||
|
|||
guard var input = try? String(contentsOfFile: "/Users/hrbrmstr/Development/2020-code-advent/input/04-01.txt", encoding: .utf8) |
|||
.components(separatedBy: "\n") else { |
|||
return(-1) |
|||
} |
|||
|
|||
input.insert("", at: 0) |
|||
|
|||
let res: [[String : String]] = input.reduce(into: []) { |
|||
($1 == "") ? $0.append([$1]) : $0[$0.index(before: $0.endIndex)].append($1) |
|||
} |
|||
.filter { $0.count > 1 } |
|||
.map { |
|||
$0.filter { !$0.isEmpty } |
|||
.joined(separator: " ") |
|||
.components(separatedBy: " ") |
|||
.filter { !$0.starts(with: "cid") } |
|||
.reduce(into: [String: String]()) { |
|||
let res = $1.split(separator: ":") |
|||
$0[String(res[0])] = String(res[1]) |
|||
} |
|||
} |
|||
|
|||
return( |
|||
res.filter { reqKeys.symmetricDifference(Set($0.keys)).count == 0 } |
|||
.filter { |
|||
$0.filter { |
|||
var res: Bool |
|||
switch($0.key) { |
|||
case "byr": res = $0.value.matches("^[0-9]{4}$") && Int($0.value)!.between(1920, 2002) |
|||
case "iyr": res = $0.value.matches("^[0-9]{4}$") && Int($0.value)!.between(2010, 2020) |
|||
case "eyr": res = $0.value.matches("^[0-9]{4}$") && Int($0.value)!.between(2020, 2030) |
|||
case "hgt": res = $0.value.matches("^[0-9]{2,3}(cm|in)$") && validateHeight($0.value) |
|||
case "hcl": res = $0.value.matches("^#[a-f0-9]{6}$") |
|||
case "ecl": res = hairColors.contains($0.value) |
|||
case "pid": res = $0.value.matches("^[0-9]{9}$") |
|||
default: res = false |
|||
} |
|||
return(res) |
|||
}.count == 7 |
|||
}.count |
|||
) |
|||
|
|||
} |
|||
|
|||
} |
|||
|
|||
extension String { |
|||
func matches(_ regex: String) -> Bool { |
|||
return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil |
|||
} |
|||
} |
|||
|
|||
extension Comparable { |
|||
func between( _ lb: Self, _ ub: Self) -> Bool { |
|||
return( (self >= lb) && (self <= ub) ) |
|||
} |
|||
} |
Binary file not shown.
Binary file not shown.
@ -1,6 +0,0 @@ |
|||
#FileLock |
|||
#Sun Dec 06 09:30:25 EST 2020 |
|||
server=localhost\:52982 |
|||
hostName=localhost |
|||
method=file |
|||
id=1763875fe7b64f87d49c291c70f4d4b27b6f62d8428 |
@ -0,0 +1,33 @@ |
|||
/* |
|||
* Scala (https://www.scala-lang.org) |
|||
* |
|||
* Copyright EPFL and Lightbend, Inc. |
|||
* |
|||
* Licensed under Apache License 2.0 |
|||
* (http://www.apache.org/licenses/LICENSE-2.0). |
|||
* |
|||
* See the NOTICE file distributed with this work for |
|||
* additional information regarding copyright ownership. |
|||
*/ |
|||
|
|||
package scala.annotation |
|||
|
|||
/** An annotation to be applied to a match expression. If present, |
|||
* the compiler will verify that the match has been compiled to a |
|||
* [[http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.10 tableswitch or lookupswitch]] |
|||
* and issue a warning if it instead compiles into a series of conditional expressions. |
|||
* Example usage: |
|||
{{{ |
|||
val Constant = 'Q' |
|||
def tokenMe(ch: Char) = (ch: @switch) match { |
|||
case ' ' | '\t' | '\n' => 1 |
|||
case 'A' | 'Z' | '$' => 2 |
|||
case '5' | Constant => 3 // a non-literal may prevent switch generation: this would not compile |
|||
case _ => 4 |
|||
} |
|||
}}} |
|||
* |
|||
* Note: for pattern matches with one or two cases, the compiler generates jump instructions. |
|||
* Annotating such a match with `@switch` does not issue any warning. |
|||
*/ |
|||
final class switch extends scala.annotation.StaticAnnotation |
Loading…
Reference in new issue