2022-12-20 AoC 09
Read the 2022 Advent of Code Day 9: Rope Bridge.
You may use your own method to solve or follow this outline.
Discuss at least one problem that arises, and how you handle it. Hopefully there will be some debugging.
Defintions
Use the following starter definitions. Make sure to remove any leading spaces if you copy and paste this code.
type Posn = (Int,Int)
type RState = (Posn, Posn) -- "Rope State"
Outline
-
(4 pts) The data file comes in the form “L 27” - a single letter, a space, then a number.
parse_one_line :: String -> (String, Int) parse_one_line _ = ("FIX",0) check_parse = [ parse_one_line "L 27" == ("L",27), parse_one_line "U 8" == ("U",8) ]
-
(4 pts) The letters “L”, “U”, “D”, “R” represent left, up, down, right. Use the standard coordinate system where up increases the y coordinate. Decode the letter to represent the change in x and y.
delta :: String -> Posn delta _ = (0,0) check_delta = [ delta "U" == (0,1) ]
-
(4 pts) Given a
Posn
representing a position and anotherPosn
represesnting the change in x and y, produce a newPosn
.move :: Posn -> Posn -> Posn move start change = (0,0) check_move = [ move (2,7) (10,-1) == (12,6) ]
-
(4 pts code, 4 pts checks) The tail of the rope moves to follow the head. Given a (new) head position and an old tail position, produce the new tail position. This will require reading the question. Add two more tests to the
check_tail
variable. You can get those tests from the problem description.new_tail :: Posn -> Posn -> Posn new_tail headpos oldtail = (0,0) check_tail = [ new_tail (2,7) (3,9) == (2,8), new_tail (4,2) (5,3) == (5,3) ]
-
(4 pts) Given a string and a number, produce a list with a string repeated that number of times.
unrolla :: (String, Int) -> [String] unrolla _ = ["nope"] check_unrolla = [ unrolla ("U", 5) == ["U","U","U","U","U"]
-
(4 pts) Given a list of
(String,Int)
pairs, produce a list of repeated strings, similar to the above.unrollb :: [(String, Int)] -> [String] unrollb _ = ["X"] check_unrollb = [unrollb [("U",2),("R",3)] == ["U","U","R","R","R"]]
-
(4 pts) Given the current head and tail position of a rope, along with a a single letter direction that the head moves (“U”,“D”,“L”, or “R”), produce a new head and tail position.
update_ht :: (Posn, Posn) -> String -> (Posn, Posn) update_ht (rHead, rTail) dir = (rHead, rTail)
-
(4 pts for code, 4 pts for checks) Given a starting position of the rope (head and tail) and a list of single letter directions the head moves, produce a list of positions occupied by the head and tail. Include the starting positions before any movement as well as the ending position after all movement is done. Write at least one test that includes at least two moves.
ht_trace :: RState -> [String] -> [RState] ht_trace (rHead, rTail) _ = [] check_ht_trace = [ ht_trace ((1,2),(3,4)) [] == [((1,2),(3,4))] ]
-
(4 pts code, 4 pts check) Produce a list counting of all of the unique tail positions from a given list of states. You plan to use this function on the output of
ht_trace
. Write at least two checks, at least one of which must show that your function does not double count repeated tail positions.uniq_tail :: [RState] -> Int uniq_tail _ = 0
-
Good, put it together and solve the challenge.