-
Notifications
You must be signed in to change notification settings - Fork 0
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
a95d4a3
commit eb7e6b3
Showing
4 changed files
with
254 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
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,62 @@ | ||
|
||
import Advent | ||
import Algorithms | ||
import Foundation | ||
|
||
public enum Day10: Day { | ||
|
||
public static let title = "Pipe Maze" | ||
|
||
public static func part1(_ input: Input) throws -> Int { | ||
|
||
let grid = try Grid<Position2D<Int>, Tile>(rawValues: input.lines.reversed()) | ||
var current = try [grid.firstPosition(of: .start).unwrapped] | ||
var covered: Set<Position2D<Int>> = [] | ||
var steps = 0 | ||
|
||
while Set(current).count == current.count { | ||
covered.formUnion(current) | ||
steps += 1 | ||
current = current.flatMap { position -> [Position2D<Int>] in | ||
guard let tile = grid[position] else { return [] } | ||
return tile | ||
.neighbours | ||
.map { position + $0 } | ||
.filter { !covered.contains($0) } | ||
.filter { next in // Ensure next pipe leads back to us. | ||
guard let tile = grid[next] else { return false} | ||
return tile.neighbours.map { next + $0 }.contains(position) | ||
} | ||
} | ||
} | ||
|
||
return steps | ||
} | ||
|
||
public static func part2(_ input: Input) throws -> Int { | ||
0 | ||
} | ||
} | ||
|
||
private struct Tile: RawRepresentable, Equatable { | ||
|
||
static let start = try! Self("S") | ||
|
||
let rawValue: Character | ||
let neighbours: [Vector2D<Int>] | ||
|
||
init?(rawValue: Character) { | ||
self.rawValue = rawValue | ||
switch rawValue { | ||
case "|": neighbours = [.north, .south] | ||
case "-": neighbours = [.east, .west] | ||
case "L": neighbours = [.north, .east] | ||
case "J": neighbours = [.north, .west] | ||
case "7": neighbours = [.south, .west] | ||
case "F": neighbours = [.south, .east] | ||
case ".": neighbours = [] | ||
case "S": neighbours = Vector2D<Int>.orthogonal | ||
default: return nil | ||
} | ||
} | ||
} |
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,51 @@ | ||
|
||
import Advent | ||
import Year2023 | ||
import XCTest | ||
|
||
final class Day10Tests: XCTestCase { | ||
|
||
func testPart1Example1() throws { | ||
XCTAssertEqual(try Day10.part1([ | ||
".....", | ||
".S-7.", | ||
".|.|.", | ||
".L-J.", | ||
".....", | ||
]), 4) | ||
} | ||
|
||
func testPart1Example2() throws { | ||
XCTAssertEqual(try Day10.part1([ | ||
"..F7.", | ||
".FJ|.", | ||
"SJ.L7", | ||
"|F--J", | ||
"LJ...", | ||
]), 8) | ||
} | ||
|
||
func testPart1Example3() throws { | ||
XCTAssertEqual(try Day10.part1([ | ||
"7-F7-", | ||
".FJ|7", | ||
"SJLL7", | ||
"|F--J", | ||
"LJ.LJ", | ||
]), 8) | ||
} | ||
|
||
func testPart1Puzzle() throws { | ||
let input = try Bundle.module.input(named: "Day10") | ||
XCTAssertEqual(try Day10.part1(input), 7063) | ||
} | ||
|
||
func testPart2Examples() throws { | ||
XCTAssertEqual(try Day10.part2([]), 0) | ||
} | ||
|
||
func testPart2Puzzle() throws { | ||
let input = try Bundle.module.input(named: "Day10") | ||
XCTAssertEqual(try Day10.part2(input), 0) | ||
} | ||
} |
Oops, something went wrong.