advent2021-0.1.0.0: Advent of Code 2021 Solutions
Copyright(c) Eric Mertens 2018
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Advent.Coord

Description

2-dimensional coordinates commonly found in AoC problems where y grows down, x grows right.

   -y
    ↑
-x ←0→ +x
    ↓
   +y
Synopsis

Documentation

data Coord Source #

Two-dimensional coordinate

Constructors

C !Int !Int 

Instances

Instances details
Eq Coord Source # 
Instance details

Defined in Advent.Coord

Methods

(==) :: Coord -> Coord -> Bool #

(/=) :: Coord -> Coord -> Bool #

Num Coord Source #

Paisewise treatment of coordinates

Instance details

Defined in Advent.Coord

Ord Coord Source # 
Instance details

Defined in Advent.Coord

Methods

compare :: Coord -> Coord -> Ordering #

(<) :: Coord -> Coord -> Bool #

(<=) :: Coord -> Coord -> Bool #

(>) :: Coord -> Coord -> Bool #

(>=) :: Coord -> Coord -> Bool #

max :: Coord -> Coord -> Coord #

min :: Coord -> Coord -> Coord #

Read Coord Source # 
Instance details

Defined in Advent.Coord

Show Coord Source # 
Instance details

Defined in Advent.Coord

Methods

showsPrec :: Int -> Coord -> ShowS #

show :: Coord -> String #

showList :: [Coord] -> ShowS #

Ix Coord Source #

Row-major coordinate indexing

>>> range (C 1 1, C 2 2)
[C 1 1,C 1 2,C 2 1,C 2 2]
>>> index (C 1 1, C 2 2) <$> range (C 1 1, C 2 2)
[0,1,2,3]
Instance details

Defined in Advent.Coord

Generic Coord Source # 
Instance details

Defined in Advent.Coord

Associated Types

type Rep Coord :: Type -> Type #

Methods

from :: Coord -> Rep Coord x #

to :: Rep Coord x -> Coord #

HasTrie Coord Source # 
Instance details

Defined in Advent.Coord

Associated Types

data (:->:) Coord :: Type -> Type

Methods

trie :: (Coord -> b) -> Coord :->: b

untrie :: (Coord :->: b) -> Coord -> b

enumerate :: (Coord :->: b) -> [(Coord, b)]

type Rep Coord Source # 
Instance details

Defined in Advent.Coord

newtype Coord :->: a Source # 
Instance details

Defined in Advent.Coord

newtype Coord :->: a = CT (Int :->: (Int :->: a))

coordRow :: Coord -> Int Source #

Row (y) of coordinate

coordCol :: Coord -> Int Source #

Column (x) of coordinate

above :: Coord -> Coord Source #

Decrement y coordinate

below :: Coord -> Coord Source #

Increment y coordinate

left :: Coord -> Coord Source #

Decrement x coordinate

right :: Coord -> Coord Source #

Increment x coordinate

invert :: Coord -> Coord Source #

Swap x and y coordinates

flipX :: Coord -> Coord Source #

Invert the x coordinate

flipY :: Coord -> Coord Source #

Invert the y coordinate

turnLeft :: Coord -> Coord Source #

Rotate coordinate 90-degrees CCW about the origin

turnRight :: Coord -> Coord Source #

Rotate coordinate 90-degrees CW about the origin

turnAround :: Coord -> Coord Source #

Rotate the coordinate 180-degrees about the origin

manhattan :: Coord -> Coord -> Int Source #

Compute the Manhattan distance between two coordinates

cardinal :: Coord -> [Coord] Source #

Compute the 4 cardinal neighbors of a coordinate: north, south, east, west

neighbors :: Coord -> [Coord] Source #

Compute the 8 cardinal neighbors and diagonal neighbors

boundingBox :: [Coord] -> Maybe (Coord, Coord) Source #

Find the upper-left and lower-right coordinates that inclusively contain all the coordinates in a list of coordinates.

origin :: Coord Source #

Coordinate at the origin

north :: Coord Source #

Unit vector pointing up

east :: Coord Source #

Unit vector pointing right

south :: Coord Source #

Unit vector pointing down

west :: Coord Source #

Unit vector pointing left

addCoord :: Coord -> Coord -> Coord Source #

Add two coordinates as vectors from the origin

scaleCoord :: Int -> Coord -> Coord Source #

Scale a coordinate as a vector from the origin

drawPicture :: Map Coord Char -> String Source #

Render a minimal bounding box containing all the characters at the given coordinates. Empty space filled with space characters.

drawCoords :: Foldable t => t Coord -> String Source #

Render a minimal bounding box containing boxes at the given coordinates.

coordLines :: [String] -> [(Coord, Char)] Source #

Given a list of lines pair up each character with its position.

zipCoord :: (Int -> Int -> Int) -> Coord -> Coord -> Coord Source #