From f5960de7e90a3737692ea383dcf9e90acd650f42 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Thu, 15 Dec 2022 15:52:01 +0100 Subject: [PATCH] Day15: initial --- day15.hs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ day15short | 14 +++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 day15.hs create mode 100644 day15short diff --git a/day15.hs b/day15.hs new file mode 100644 index 0000000..bd2c6da --- /dev/null +++ b/day15.hs @@ -0,0 +1,59 @@ +data Position = Pos {x::Int, y::Int} +data PositionRotated = PosR {xR::Int, yR::Int} + +instance Show Position where + show (Pos x y) = show (x,y) + +instance Show PositionRotated where + show (PosR x y) = show (x,y) ++ "'" + + +data Sensor = Sensor { position :: Position, beacon::Position } deriving Show +data SensorRotated = SensorRotated { positionR :: PositionRotated, beaconR :: PositionRotated } deriving Show + +maxX (Sensor (Pos x _) (Pos x' _)) = maximum [x,x'] +maxY (Sensor (Pos _ y) (Pos _ y')) = maximum [y,y'] + +class RotatePair a b where + rotateS :: Int -> a -> b + +instance RotatePair Sensor SensorRotated where + rotateS = rotateSensor + +instance RotatePair SensorRotated Sensor where + rotateS = rotateSensor' + +rotate :: Int -> Position -> PositionRotated +rotate width (Pos x y) = PosR (x+y) (width - x + y) + +rotate' :: Int -> PositionRotated -> Position +rotate' width (PosR a b) = let y = ((b+a-width) `div` 2) in Pos (a-y) y + +-- parseLine :: String -> Sensor +parseLine l = Sensor (Pos (read sx) (read sy)) (Pos (read bx) (read by)) + where (sx,(_:_:_:_:ys)) = span(/=',') (drop (length "Sensor at x=") l) + (sy,beaconLine) = span(/=':') ys + (bx,(_:_:_:_:by)) = span(/=',') (drop (length ": closest beacon is at x=") beaconLine) + +parseInput = map parseLine.lines + +short = readFile "day15short" >>= pure.parseInput + +rotateSensor :: Int -> Sensor -> SensorRotated +rotateSensor width (Sensor p b) = SensorRotated (rotate width p) (rotate width b) + +rotateSensor' :: Int -> SensorRotated -> Sensor +rotateSensor' width (SensorRotated p b) = Sensor (rotate' width p) (rotate' width b) + +oneBeacon :: SensorRotated -> ((Int,Int),(Int, Int)) +oneBeacon (SensorRotated (PosR x y) (PosR bx by)) = ((x-distance, y-distance),(x+distance, y+distance)) + where distance = max (abs (x-bx)) (abs (y-by)) + +puzzle1 = do + input <- short + let (height, width) = (maximum $ map maxX input,maximum $ map maxY input) + print (width, height) + let rotatedSensors = map (rotateS width) input + -- let relevantSensors = filter (relevant 10) rotatedSensors + print rotatedSensors + diff --git a/day15short b/day15short new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/day15short @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3