Skip to content

Commit

Permalink
Add 2023, day 10, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
danielctull committed Dec 10, 2023
1 parent a95d4a3 commit eb7e6b3
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Advent/Sources/Conversion/RawRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public struct UnexpectedRawValue<RawValue>: Error {

extension UnexpectedRawValue: CustomStringConvertible {

public var description: String { #"Unexpected rawValue: "\(rawValue)"."# }
public var description: String { #"Unexpected rawValue: "\#(rawValue)"."# }
}
62 changes: 62 additions & 0 deletions Year2023/Sources/Day10.swift
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
}
}
}
51 changes: 51 additions & 0 deletions Year2023/Tests/Day10Tests.swift
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)
}
}
Loading

0 comments on commit eb7e6b3

Please sign in to comment.