Browse Source

Day 4 Swift

master
boB Rudis 3 years ago
parent
commit
c7e1638daa
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 9
      Swift/2020-code-advent.playground/Contents.swift
  2. 106
      Swift/2020-code-advent.playground/Sources/04-01.swift
  3. 2
      js/04.js
  4. BIN
      scala/04/04/.bloop/root-04/bloop-bsp-clients-classes/classes-Metals-WftNm-PZT_-cgA5nv602vw==/META-INF/semanticdb/src/main/scala/Main.scala.semanticdb
  5. BIN
      scala/04/04/.metals/metals.h2.db
  6. 6
      scala/04/04/.metals/metals.lock.db
  7. 33
      scala/04/04/.metals/readonly/scala/annotation/switch.scala

9
Swift/2020-code-advent.playground/Contents.swift

@ -5,7 +5,10 @@
//print(advent02.day_02_01())
//
//print(advent02.day_02_02())
//
//print(advent03.day_03_01())
//
//print(advent03.day_03_02())
print(advent03.day_03_01())
print(advent03.day_03_02())
advent04.day_04_01()
advent04.day_04_02()

106
Swift/2020-code-advent.playground/Sources/04-01.swift

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

2
js/04.js

@ -17,7 +17,7 @@ Number.prototype.between = function(a, b) {
return(this >= min && this <= max)
};
req_keys = new Set([ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ])
req_keys = new Set([ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ])
input = []

BIN
scala/04/04/.bloop/root-04/bloop-bsp-clients-classes/classes-Metals-WftNm-PZT_-cgA5nv602vw==/META-INF/semanticdb/src/main/scala/Main.scala.semanticdb

Binary file not shown.

BIN
scala/04/04/.metals/metals.h2.db

Binary file not shown.

6
scala/04/04/.metals/metals.lock.db

@ -1,6 +0,0 @@
#FileLock
#Sun Dec 06 09:30:25 EST 2020
server=localhost\:52982
hostName=localhost
method=file
id=1763875fe7b64f87d49c291c70f4d4b27b6f62d8428

33
scala/04/04/.metals/readonly/scala/annotation/switch.scala

@ -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…
Cancel
Save