Skip to content

Commit

Permalink
feat(aoc-2024): add Day 6 (part 2) (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
leohidalgo authored Jan 15, 2025
1 parent 104d8ab commit 0d2fee5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ swift test
| 03 | [Mull It Over](https://adventofcode.com/2024/day/3) | ⭐️ | ⭐️ | [Day 03](Sources/AOC2024/Day03.swift) |
| 04 | [Ceres Search](https://adventofcode.com/2024/day/4) | ⭐️ | ⭐️ | [Day 04](Sources/AOC2024/Day04.swift) |
| 05 | [Print Queue](https://adventofcode.com/2024/day/5) | ⭐️ | ⭐️ | [Day 05](Sources/AOC2024/Day05.swift) |
| 06 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | ⭐️ | | [Day 06](Sources/AOC2024/Day06.swift) |
| 06 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | ⭐️ | ⭐️ | [Day 06](Sources/AOC2024/Day06.swift) |
| 09 | [Disk Fragmenter](https://adventofcode.com/2024/day/9) | ⭐️ | | [Day 09](Sources/AOC2024/Day09.swift) |
| 10 | [Hoof It](https://adventofcode.com/2024/day/10) | ⭐️ | ⭐️ | [Day 10](Sources/AOC2024/Day10.swift) |
| 11 | [Plutonian Pebbles](https://adventofcode.com/2024/day/11) | ⭐️ | ⭐️ | [Day 11](Sources/AOC2024/Day11.swift) |
Expand Down
47 changes: 38 additions & 9 deletions Sources/AOC2024/Day06.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,46 @@ struct Day06: Day {

func part1() throws -> Int {
let board = input().lines.map(\.characters)
let startPosition = board.findFirst(element: "^")!

var position = board.findFirst(element: "^")!
var direction = Direction.up
var path: Set<Position> = []
return patrol(startPosition, .up, board).count
}

func part2() throws -> Int {
let board = input().lines.map(\.characters)
let startPosition = board.findFirst(element: "^")!

return patrol(startPosition, .up, board)
.count { isLoop(startPosition, .up, $0, board) }
}

private func isLoop(_ startPosition: Position, _ startDirection: Direction, _ obstacle: Position, _ board: [[Character]]) -> Bool {
var route: Set<Tuple<Position, Direction>> = []
var position = startPosition
var direction = startDirection

while board[position] != nil {
path.insert(position)
route.insert(.init(position, direction))

if board[position.offset(direction)] == "#" || position.offset(direction) == obstacle {
direction = direction.turn()
} else if !route.contains(.init(position.offset(direction), direction)) {
position = position.offset(direction)
} else {
return true
}
}

return false
}

private func patrol(_ startPosition: Position, _ startDirection: Direction, _ board: [[Character]]) -> Set<Position> {
var route: Set<Position> = []
var position = startPosition
var direction = startDirection

while board[position] != nil {
route.insert(position)

if board[position.offset(direction)] == "#" {
direction = direction.turn()
Expand All @@ -23,11 +56,7 @@ struct Day06: Day {
position = position.offset(direction)
}

return path.count
}

func part2() throws -> Int {
-1
return route
}
}

Expand Down
1 change: 1 addition & 0 deletions Tests/AOCTests/AOC2024Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct AOC2024Tests {
......#...
"""
#expect(try sut.part1() == 41, "Part 1")
#expect(try sut.part2() == 6, "Part 2")
}

@Test
Expand Down

0 comments on commit 0d2fee5

Please sign in to comment.