@@ -11,7 +11,7 @@ defmodule AdventOfCode.Y2023.Day17 do
1111 def input , do: InputReader . read_from_file ( 2023 , 17 )
1212
1313 def run ( input \\ input ( ) ) do
14- grid = parse_grid ( input )
14+ grid = parse ( input )
1515 { max_x , max_y } = grid |> Map . keys ( ) |> Enum . max ( )
1616
1717 p1 = solve ( grid , max_x , max_y , 0 , 3 )
@@ -21,20 +21,16 @@ defmodule AdventOfCode.Y2023.Day17 do
2121 end
2222
2323 defp solve ( grid , max_x , max_y , min_turn , max_straight ) do
24- # State: {x, y, dir_atom, consecutive_count}
25- # dir_atom: :r, :l, :d, :u, :start
2624 Dijkstra . implicit_dijkstra_by (
2725 from: { 0 , 0 , :start , 0 } ,
2826 successors_with_cost: fn { x , y , dir , count } ->
29- # Possible directions to try
3027 dirs = if dir == :start , do: [ :r , :d ] , else: next_dirs ( dir , count , min_turn , max_straight )
3128
3229 Enum . flat_map ( dirs , fn d ->
3330 { nx , ny } = move ( x , y , d )
3431
3532 case Map . fetch ( grid , { nx , ny } ) do
3633 { :ok , cost } ->
37- # New count is count + 1 if straight, else 1
3834 ncount = if d == dir , do: count + 1 , else: 1
3935 [ { { nx , ny , d , ncount } , cost } ]
4036
@@ -55,14 +51,7 @@ defmodule AdventOfCode.Y2023.Day17 do
5551 end
5652
5753 defp next_dirs ( dir , count , min_turn , max_straight ) do
58- # Rules:
59- # 1. Can always go straight if count < max_straight
60- # 2. Can turn if count >= min_turn
61- # 3. NO REVERSE
62-
63- # Straight?
6454 straight = if count < max_straight , do: [ dir ] , else: [ ]
65- # Turns?
6655 turns = if count >= min_turn , do: turns ( dir ) , else: [ ]
6756
6857 straight ++ turns
@@ -78,19 +67,17 @@ defmodule AdventOfCode.Y2023.Day17 do
7867 defp move ( x , y , :u ) , do: { x , y - 1 }
7968 defp move ( x , y , :d ) , do: { x , y + 1 }
8069
81- defp parse_grid ( input ) do
70+ defp parse ( input ) do
8271 input
8372 |> Transformers . lines ( )
8473 |> Enum . with_index ( )
85- |> Enum . reduce ( % { } , fn { line , y } , grid ->
74+ |> List . foldl ( % { } , fn { line , y } , grid ->
8675 line
8776 |> String . graphemes ( )
8877 |> Enum . with_index ( )
89- |> Enum . reduce ( grid , fn { char , x } , acc ->
78+ |> List . foldl ( grid , fn { char , x } , acc ->
9079 Map . put ( acc , { x , y } , String . to_integer ( char ) )
9180 end )
9281 end )
9382 end
94-
95- def parse ( data \\ input ( ) ) , do: data
9683end
0 commit comments