Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
initial day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
mstksg committed Dec 13, 2023
1 parent 98779bc commit 0e59757
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
73 changes: 68 additions & 5 deletions src/AOC/Challenge/Day13.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
-- will recommend what should go in place of the underscores.

module AOC.Challenge.Day13 (
-- day13a
-- , day13b
day13a
, day13b
) where

import AOC.Prelude
Expand All @@ -45,16 +45,79 @@ import qualified Text.Megaparsec as P
import qualified Text.Megaparsec.Char as P
import qualified Text.Megaparsec.Char.Lexer as PP

-- negate (i-x-0.5) + x+0.5
-- -i+x+0.5+x+0.5
-- -i+x+x+1

findRefl :: Set Point -> IntSet
findRefl pts = IS.fromList findHoriz
<> IS.fromList ((*100) <$> findVert)
where
Just (V2 (V2 xMin yMin) (V2 xMax yMax)) = boundingBox' pts
cols :: IntMap IntSet
cols = IM.fromListWith (<>)
[ (x, IS.singleton y)
| V2 x y <- toList pts
]
rows :: IntMap IntSet
rows = IM.fromListWith (<>)
[ (y, IS.singleton x)
| V2 x y <- toList pts
]
findHoriz = flip filter [xMin+1 .. xMax] $ \x ->
let (lt, gt) = IM.spanAntitone (< x) cols
in and $ zipWith (==) (snd <$> IM.toDescList lt) (snd <$> IM.toAscList gt)
-- let cols = IntMap IntSet
-- cols = IM.fromListWith (<>) $ toList pts
-- let (lt,gt) = S.partition ((< x) . view _x) pts
-- gt' = S.map (over _x ((+ (x-1)) . negate . subtract x)) gt
-- inters = lt `S.intersection` gt'
-- in inters == lt || inters == gt'
findVert = flip filter [yMin+1 .. yMax] $ \y ->
let (lt, gt) = IM.spanAntitone (< y) rows
in and $ zipWith (==) (snd <$> IM.toDescList lt) (snd <$> IM.toAscList gt)
-- findVert = flip find [yMin+1 .. yMax] $ \y ->
-- let (lt,gt) = S.partition ((< y) . view _y) pts
-- gt' = S.map (over _y ((+ (y-1)) . negate . subtract y)) gt
-- inters = lt `S.intersection` gt'
-- in inters == lt || inters == gt'
-- let flipped = S.map (\(V2 i j) -> V2 (-i + 2*x+1) j) pts
-- -- symDiff = (flipped S.// pts) <> (pts S.// flipped)
-- -- inters = S.intersection flipped pts
-- in shiftToZero' (flipped `S.difference` pts) == shiftToZero' (pts `S.difference` flipped)
-- x <$ guard (S.map (\(V2 i j) -> V2 (negate (i - x)) j) pts == S.map (\(V2 i j) -> V2 (i - x) j) pts)
-- findVert = flip find [yMin .. yMax] $ \y ->
-- let flipped = S.map (\(V2 i j) -> V2 i (-j + 2*y+1)) pts
-- -- symDiff = (flipped S.// pts) <> (pts S.// flipped)
-- -- inters = S.intersection flipped pts
-- in shiftToZero' (flipped `S.difference` pts) == shiftToZero' (pts `S.difference` flipped)
-- -- y <$ guard (S.map (\(V2 i j) -> V2 i (-j + 2*y)) pts == S.map (\(V2 i j) -> V2 i (j - y)) pts)

-- condenseRefl :: Maybe Int -> Maybe Int -> Maybe Int
-- condenseRefl horiz vert = horiz <|> ((* 100) <$> vert)

day13a :: _ :~> _
day13a = MkSol
{ sParse = Just . lines
{ sParse = noFail $ map (parseAsciiSet (== '#')) . splitOn "\n\n"
, sShow = show
, sSolve = Just
-- , sSolve = noFail $ map findRefl
, sSolve = fmap sum . traverse (fmap fst . IS.minView . findRefl)
}

findSmudge :: Set Point -> Maybe Int
findSmudge pts = flip firstJust (V2 <$> [xMin .. xMax] <*> [yMin .. yMax]) \pt ->
let newRefl = findRefl (over (contains pt) not pts)
isNew = newRefl `IS.difference` origRefl
in fst <$> IS.minView isNew
-- in guard (newRefl /= origRefl) *> (fst <$> IS.minView isNew)
-- mfilter (/= origRefl) $ findRefl (over (contains pt) not pts)
where
origRefl = findRefl pts
Just (V2 (V2 xMin yMin) (V2 xMax yMax)) = boundingBox' pts

day13b :: _ :~> _
day13b = MkSol
{ sParse = sParse day13a
, sShow = show
, sSolve = Just
, sSolve = fmap sum . traverse findSmudge
}
32 changes: 32 additions & 0 deletions test-data/2023/13a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
>>> 405
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
>>> 5
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
>>> 400
32 changes: 32 additions & 0 deletions test-data/2023/13b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
>>> 400
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
>>> 300
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
>>> 100

0 comments on commit 0e59757

Please sign in to comment.