Skip to content

Commit

Permalink
simplify Direction
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Jan 9, 2020
1 parent e34a08a commit 6b8462f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 63 deletions.
56 changes: 9 additions & 47 deletions src/Adjacency.elm
@@ -1,50 +1,12 @@
module Adjacency exposing (Direction(..), DraftRules, Rule, Rules, combineRules, finalize, fromIds)
module Adjacency exposing (DraftRules, Rule, Rules, combineRules, finalize, fromIds)

import Array
import Dict exposing (Dict)
import Direction exposing (Direction)
import Grid exposing (Grid)
import Set exposing (Set)


type Direction
= Up
| Down
| Left
| Right


directionToComparable : Direction -> Int
directionToComparable d =
case d of
Up ->
0

Down ->
1

Left ->
2

Right ->
3


directionFromComparable : Int -> Direction
directionFromComparable c =
case c of
0 ->
Up

1 ->
Down

2 ->
Left

_ ->
Right


type alias Rule comparable =
{ direction : Direction
, to : Set comparable
Expand All @@ -61,7 +23,7 @@ finalize (DraftRules draft) =
(\( id, direction ) values dict ->
let
rule =
{ direction = directionFromComparable direction
{ direction = direction
, to = values
}
in
Expand All @@ -86,7 +48,7 @@ combineRules original =
original
|> List.foldl
(\rule ->
Dict.update (directionToComparable rule.direction)
Dict.update rule.direction
(\maybeRule ->
case maybeRule of
Just existing ->
Expand All @@ -105,7 +67,7 @@ combineRules original =


type DraftRules comparable
= DraftRules (Dict ( comparable, Int ) (Set comparable))
= DraftRules (Dict ( comparable, Direction ) (Set comparable))


fromIds : Grid comparable -> DraftRules comparable
Expand All @@ -121,13 +83,13 @@ fromIds grid =
(\colNum id ->
List.filterMap identity
[ Grid.getWrapping { row = rowNum, column = colNum - 1 } grid
|> Maybe.map (\dest -> ( id, directionToComparable Left, dest ))
|> Maybe.map (\dest -> ( id, Direction.left, dest ))
, Grid.getWrapping { row = rowNum, column = colNum + 1 } grid
|> Maybe.map (\dest -> ( id, directionToComparable Right, dest ))
|> Maybe.map (\dest -> ( id, Direction.right, dest ))
, Grid.getWrapping { row = rowNum - 1, column = colNum } grid
|> Maybe.map (\dest -> ( id, directionToComparable Up, dest ))
|> Maybe.map (\dest -> ( id, Direction.up, dest ))
, Grid.getWrapping { row = rowNum + 1, column = colNum } grid
|> Maybe.map (\dest -> ( id, directionToComparable Down, dest ))
|> Maybe.map (\dest -> ( id, Direction.down, dest ))
]
)
row
Expand Down
32 changes: 32 additions & 0 deletions src/Direction.elm
@@ -0,0 +1,32 @@
module Direction exposing (Direction, down, left, move, right, up)


type alias Direction =
( Int, Int )


up : Direction
up =
( -1, 0 )


down : Direction
down =
( 1, 0 )


left : Direction
left =
( 0, -1 )


right : Direction
right =
( 0, 1 )


move : { row : Int, column : Int } -> Direction -> { row : Int, column : Int }
move { row, column } ( adjRow, adjCol ) =
{ row = row + adjRow
, column = column + adjCol
}
22 changes: 6 additions & 16 deletions src/Wave.elm
Expand Up @@ -2,6 +2,7 @@ module Wave exposing (Wave, getEntropy, init, step, view)

import Adjacency
import Dict exposing (Dict)
import Direction
import Grid exposing (Grid)
import Heap exposing (Heap)
import Html.Styled as Html exposing (Html)
Expand Down Expand Up @@ -183,22 +184,11 @@ propagateHelp coordses (Wave wave) =
rules
|> List.filterMap
(\rule ->
let
target =
case rule.direction of
Adjacency.Up ->
{ coords | row = coords.row - 1 }

Adjacency.Down ->
{ coords | row = coords.row + 1 }

Adjacency.Left ->
{ coords | column = coords.column - 1 }

Adjacency.Right ->
{ coords | column = coords.column + 1 }
in
propagateAndGetEntropy target rule.to wave.weights wave.items
propagateAndGetEntropy
(Direction.move coords rule.direction)
rule.to
wave.weights
wave.items
)
|> List.foldl
(\( target, propagated, propagatedEntropy ) ( guts, toPropagate ) ->
Expand Down

0 comments on commit 6b8462f

Please sign in to comment.