-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2051b7c
commit 6ec3f5a
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Algorithms | ||
import Collections | ||
import Foundation | ||
|
||
extension ClosedRange { | ||
func contains(_ other: ClosedRange) -> Bool { | ||
other.clamped(to: self) == other | ||
} | ||
} | ||
|
||
struct Day4: Day { | ||
var input: String | ||
|
||
init(input: String) { | ||
self.input = input | ||
} | ||
|
||
func partOne() -> String { | ||
input | ||
.split(separator: "\n") | ||
.lazy | ||
.map(parse) | ||
.filter { $0.0.contains($0.1) || $0.1.contains($0.0) } | ||
.count | ||
.description | ||
} | ||
|
||
func partTwo() -> String { | ||
input | ||
.split(separator: "\n") | ||
.lazy | ||
.map(parse) | ||
.filter { $0.0.overlaps($0.1) } | ||
.count | ||
.description | ||
} | ||
|
||
func parse(_ line: Substring) -> (ClosedRange<Int>, ClosedRange<Int>) { | ||
let numbers = line.split(whereSeparator: { $0.isNumber == false }).map { Int($0)! } | ||
return (numbers[0] ... numbers[1], numbers[2] ... numbers[3]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters