From 59ea108299f63c803e42871a707d5c4a1f2c6b00 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Wed, 4 Dec 2019 12:55:38 -0500 Subject: [PATCH] Add nicer formatting to day03 Signed-off-by: Alek Ratzloff --- day03/Day03.hs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/day03/Day03.hs b/day03/Day03.hs index 27dd692..9bea669 100644 --- a/day03/Day03.hs +++ b/day03/Day03.hs @@ -8,15 +8,27 @@ main = do let input = parseInput inputText putStr "Part 1: " putStrLn $ part1 input - --putStrLn $ show $ growBoard (-5, -6) $ growBoard (5, 6) emptyBoard + putStrLn $ show $ applyAt (-4, 4) emptyBoard -part1 :: ([Dir], [Dir]) -> String +part1 :: (Wires, Wires) -> String part1 _ = " " -parseInput :: String -> ([Dir], [Dir]) +parseInput :: String -> (Wires, Wires) parseInput input = (wires1, wires2) where - wires1:wires2:_ = input |> lines |> map (splitOn "," .> map parseDir) + wires1:wires2:_ = input |> lines |> fmap (splitOn "," .> fmap parseDir) + +applyAtPos :: Board -> Board +applyAtPos board = applyAt (pos board) board + +applyAt :: (Int, Int) -> Board -> Board +applyAt (x, y) board = + board |> growBoard (x * 2, y * 2) |> mapBoardMat (updateMat (x, y)) + +--applyWires :: Wires -> Board -> Board +--applyWires wires board = +--applyWire :: Dir -> Board -> Board +type Wires = [Dir] type Mat = [[Bool]] @@ -30,9 +42,6 @@ data Board = emptyBoard :: Board emptyBoard = Board {mat = [], pos = (0, 0), origin = (0, 0)} -ensureBoardPos :: (Int, Int) -> Board -> Board -ensureBoardPos _ board = board - growBoard :: (Int, Int) -> Board -> Board growBoard (x, y) board = growBoardX x (growBoardY y board) @@ -131,6 +140,14 @@ dirPair (Dn n) = (0, n) dirPair (Lt n) = (-n, 0) dirPair (Rt n) = (n, 0) +updateMat :: (Int, Int) -> Mat -> Mat +updateMat (x, y) mat = newMat + where + (rowHead, _:rowTail) = splitAt y mat + row = mat !! y + (colHead, _:colTail) = splitAt x row + newMat = rowHead ++ [colHead ++ True : colTail] ++ rowTail + growMatDir :: Dir -> Mat -> Mat growMatDir dir mat | dirMag dir == 0 = mat @@ -160,7 +177,7 @@ matW :: Mat -> Int matW mat = if matH mat == 0 then 0 - else mx (map length mat) + else mx (fmap length mat) matH :: Mat -> Int matH mat = length mat