sln_2022_09
Copyright(c) Eric Mertens 2022
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Main

Description

https://adventofcode.com/2022/day/9

>>> :{
:main +
  "R 4\n\
  \U 4\n\
  \L 3\n\
  \D 1\n\
  \R 4\n\
  \D 1\n\
  \L 5\n\
  \R 2\n"
:}
13
1
>>> :{
:main +
  "R 5\n\
  \U 8\n\
  \L 8\n\
  \D 3\n\
  \R 17\n\
  \D 10\n\
  \L 25\n\
  \U 20\n"
:}
88
36
>>> uniqueLocations [(CR,4),(CU,4),(CL,3),(CD,1),(CR,4),(CD,1),(CL,5),(CR,2)] !! 1
13
>>> uniqueLocations [(CR,5),(CU,8),(CL,8),(CD,3),(CR,17),(CD,10),(CL,25),(CU,20)] !! 9
36
Synopsis

Documentation

data C Source #

Rope movement instructions

Constructors

CD

move down

CR

move right

CU

move up

CL

move left

Instances

Instances details
Show C Source # 
Instance details

Defined in Main

Methods

showsPrec :: Int -> C -> ShowS #

show :: C -> String #

showList :: [C] -> ShowS #

main :: IO () Source #

Print the answers to both parts of day 9. Automatically finds input file unless overridden with a command line argument.

>>> :main
5930
2443

uniqueLocations :: [(C, Int)] -> [Int] Source #

Generate the number of unique locations each knot in an infinitely long rope visits given a list of movement commands.

cToVec :: C -> Coord Source #

Generate the unit vector corresponding to an input command.

stepRope Source #

Arguments

:: [Coord]

knot locations

-> C

next step direction

-> [Coord]

updated knot locations

Update all the knot locations in a rope given a step direction for the head knot.

updateTails Source #

Arguments

:: Coord

head

-> [Coord]

tails

-> [Coord]

updated rope

Update all the tail knots in the rope given a new head position.

isNearOrigin :: Coord -> Bool Source #

Predicate for coordinates at or adjacent to the origin.

>>> all isNearOrigin [C y x | y <- [-1..1], x <- [-1..1]]
True
>>> any isNearOrigin [C 2 0, C 0 2, C 2 1, C (-2) 0, C (-1) 2]
False

countUnique :: Ord a => [a] -> Int Source #

Return the number of unique elements in a list.