-
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
Showing
4 changed files
with
56 additions
and
6 deletions.
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
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 |
---|---|---|
@@ -1,15 +1,62 @@ | ||
import Aoc2024.Utils | ||
import Aoc2024.Day06.Examples | ||
import Aoc2024.Day06.Parser | ||
import Std | ||
open Std (HashSet) | ||
|
||
private def solvePart1 (input : PuzzleInput) : Int := sorry | ||
private structure PatrolState where | ||
position : Point | ||
direction : Direction := Direction.Up | ||
obstacles : HashSet Point | ||
bounds : Rectangle | ||
visited : HashSet Point | ||
deriving Repr | ||
|
||
private def PatrolState.hasObstacle (s : PatrolState) (p : Point): Bool := s.obstacles.contains p | ||
|
||
private def PatrolState.initial (input : PuzzleInput) : PatrolState := | ||
{ position := input.start, obstacles := input.obstacles, bounds := input.bounds, visited := singleton input.start } | ||
|
||
private def PatrolState.positionAhead (s : PatrolState) : Point := s.position.step s.direction | ||
|
||
private def PatrolState.turnRight (s : PatrolState) : PatrolState := { s with direction := s.direction.turnRight } | ||
|
||
private def PatrolState.stepForward (s : PatrolState) : PatrolState := | ||
let newPosition := s.position.step s.direction | ||
{ s with position := newPosition, visited := s.visited.insert newPosition } | ||
|
||
-- returns whether the guard has left the area | ||
private def stepGuard : StateM PatrolState Bool := do | ||
let state ← get | ||
let ahead := state.positionAhead | ||
if state.hasObstacle ahead then | ||
modify PatrolState.turnRight *> return false | ||
else if state.bounds.contains ahead then | ||
modify PatrolState.stepForward *> return false | ||
else | ||
return true | ||
|
||
-- returns true if simulation has ended early by running out of fuel | ||
private def simulatePatrol : Nat -> StateM PatrolState Bool | ||
| 0 => return true | ||
| fuel + 1 => do | ||
let hasLeftArea <- stepGuard | ||
if (hasLeftArea) then | ||
return false | ||
else | ||
simulatePatrol fuel | ||
|
||
private def solvePart1 (input : PuzzleInput) : Int := | ||
let initialState := PatrolState.initial input | ||
let (simulationEndedEarly, finalState) := (simulatePatrol 100000).run initialState | ||
if simulationEndedEarly then -1 else finalState.visited.size | ||
|
||
def parseAndSolvePart1 (s : String): Except String Int := parseInput s |>.map solvePart1 | ||
|
||
-- #guard parseAndSolvePart1 exampleInput == Except.ok 41 | ||
#guard parseAndSolvePart1 exampleInput == Except.ok 41 | ||
|
||
private def solvePart2 (input : PuzzleInput) : Int := sorry | ||
|
||
def parseAndSolvePart2 (s : String): Except String Int := parseInput s |>.map solvePart2 | ||
|
||
-- #guard parseAndSolvePart2 exampleInput == Except.ok -1 | ||
-- #guard parseAndSolvePart2 exampleInput == Except.ok 6 |
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