Skip to content

Commit 04a4511

Browse files
committed
1 parent 37924cd commit 04a4511

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

17.hs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,30 @@ parse s = Grid { items = M.fromList xs, lastNode = fst (last xs) }
1515
enum :: [a] -> [(Int, a)]
1616
enum = zip [0..]
1717

18-
data Direction = L | R | U | D deriving (Eq, Ord)
18+
data Direction = H | V deriving (Eq, Ord)
1919
data Cell = Cell { node :: Node, direction :: Direction } deriving (Eq, Ord)
2020
data Neighbour = Neighbour { cell :: Cell, distance :: Int }
2121

2222
neighbours :: Grid Int -> [Int] -> Cell -> [Neighbour]
2323
neighbours Grid { items } range = concat . adjacent
2424
where
25-
adjacent Cell { node = (x, y), direction = d }
26-
| d `elem` [L, R] = [cells (\m -> Cell (x, y - m) U),
27-
cells (\m -> Cell (x, y + m) D)]
28-
| otherwise = [cells (\m -> Cell (x - m, y) R),
29-
cells (\m -> Cell (x + m, y) L)]
25+
adjacent Cell { node = (x, y), direction } = case direction of
26+
H -> [cells (\m -> Cell (x, y - m) V), cells (\m -> Cell (x, y + m) V)]
27+
V -> [cells (\m -> Cell (x - m, y) H), cells (\m -> Cell (x + m, y) H)]
3028
cells mkCell = snd $ foldl (mkNeighbour mkCell) (0, []) [1..maximum range]
31-
mkNeighbour mkCell (d, xs) m = let cell = mkCell m in case M.lookup (node cell) items of
32-
Just d2 -> (d + d2, if m `elem` range then Neighbour cell (d + d2) : xs else xs)
33-
_ -> (d, xs)
29+
mkNeighbour mkCell (d, xs) m =
30+
let c = mkCell m in case M.lookup (node c) items of
31+
Just d2 -> let dc = d + d2
32+
in (dc, if m `elem` range then Neighbour c dc : xs else xs)
33+
_ -> (d, xs)
3434

3535
shortestPath :: [Int] -> Grid Int -> Int
3636
shortestPath moveRange grid@Grid { lastNode } = go startDist S.empty startQ
3737
where
38-
-- Start in both directions so that we never have to go straight and can
39-
-- just always turn. This way, we don't even need to track moves.
40-
startCells = [Cell { node = (0, 0), direction = L },
41-
Cell { node = (0, 0), direction = D }]
38+
-- Start on both axes so that we never have to go straight and can just
39+
-- always turn. This way, we don't even need to track moves.
40+
startCells = [Cell { node = (0, 0), direction = H },
41+
Cell { node = (0, 0), direction = V }]
4242
startDist = M.fromList $ zip startCells [0, 0]
4343
startQ = S.fromList $ zip [0, 0] startCells
4444
isEnd Cell { node } = node == lastNode

17.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func readInput() -> Grid {
2222
}
2323

2424
enum Direction {
25-
case l, r, u, d
25+
case h, v
2626
}
2727

2828
struct Cell: Hashable {
@@ -32,20 +32,20 @@ struct Cell: Hashable {
3232

3333
func shortestPath(grid: Grid, moveRange: ClosedRange<Int>) -> Int {
3434
let startNode = Node(x: 0, y: 0)
35-
// Create two starting cells, one for each axis of movement, so that when
36-
// considering neighbours, we never need to go straight, we can always turn.
37-
let startL = Cell(node: startNode, direction: .l)
38-
let startD = Cell(node: startNode, direction: .d)
35+
// Create two starting cells, one for each axis of movement, so that we only
36+
// need to turn.
37+
let startH = Cell(node: startNode, direction: .h)
38+
let startV = Cell(node: startNode, direction: .v)
3939
func isEnd(_ cell: Cell) -> Bool { cell.node == grid.lastNode }
4040

4141
func adj(_ u: Cell) -> some Sequence<Neighbour> {
4242
neighbours(grid: grid, moveRange: moveRange, cell: u)
4343
}
4444

45-
var dist: [Cell: Int] = [startL: 0, startD: 0]
45+
var dist: [Cell: Int] = [startH: 0, startV: 0]
4646
var seen: Set<Cell> = Set()
4747
// Use the inverse of the distance map to simulate a priority queue.
48-
var inverseDistance = [0: Set([startL, startD])]
48+
var inverseDistance = [0: Set([startH, startV])]
4949

5050
func popNearest() -> (Cell, Int)? {
5151
while let d = inverseDistance.keys.min() {
@@ -94,12 +94,12 @@ func neighbours(
9494

9595
let (x, y) = (cell.node.x, cell.node.y)
9696
switch cell.direction {
97-
case .l, .r:
98-
return [seq({ make((x, y - $0), .u) }),
99-
seq({ make((x, y + $0), .d) })].joined()
100-
default:
101-
return [seq({ make((x - $0, y), .r) }),
102-
seq({ make((x + $0, y), .l) })].joined()
97+
case .h:
98+
return [seq({ make((x, y - $0), .v) }),
99+
seq({ make((x, y + $0), .v) })].joined()
100+
case .v:
101+
return [seq({ make((x - $0, y), .h) }),
102+
seq({ make((x + $0, y), .h) })].joined()
103103
}
104104
}
105105

0 commit comments

Comments
 (0)