Day15: initial
This commit is contained in:
parent
5b2b7f3ba4
commit
f5960de7e9
2 changed files with 73 additions and 0 deletions
59
day15.hs
Normal file
59
day15.hs
Normal file
|
@ -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
|
||||
|
14
day15short
Normal file
14
day15short
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue