Skip to content

Instantly share code, notes, and snippets.

@groob
Created December 4, 2019 18:44
Show Gist options
  • Select an option

  • Save groob/839944a64e03541fd323f3fcf0278995 to your computer and use it in GitHub Desktop.

Select an option

Save groob/839944a64e03541fd323f3fcf0278995 to your computer and use it in GitHub Desktop.

Revisions

  1. groob created this gist Dec 4, 2019.
    24 changes: 24 additions & 0 deletions Advent of Code Day 4.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    extension Int {
    func digits() -> [Int] {
    var newN = self
    var result: [Int] = []
    while newN > 0 {
    let r = newN % 10
    newN = newN / 10
    result.append(r)
    }
    result.reverse()
    return result
    }
    }

    func isValid(_ digits: [Int]) -> Bool {
    return digits == digits.sorted() && digits.contains { d in digits.filter { $0 == d }.count == 2 }
    }

    let result =
    (136760...595730)
    .map { $0.digits() }
    .filter { isValid($0) }
    .count
    print(result)