Skip to content

Commit

Permalink
2023-13
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 13, 2023
1 parent 04b6316 commit ded4fc4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions solutions/solutions.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,7 @@ executable sln_2023_12
import: day
main-is: 2023/12.hs
build-depends: array

executable sln_2023_13
import: day
main-is: 2023/13.hs
70 changes: 70 additions & 0 deletions solutions/src/2023/13.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{-# Language QuasiQuotes #-}
{-|
Module : Main
Description : Day 13 solution
Copyright : (c) Eric Mertens, 2023
License : ISC
Maintainer : emertens@gmail.com
<https://adventofcode.com/2023/day/13>
>>> :{
:main +
"#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.\n
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
"
:}
405
400
-}
module Main (main) where

import Advent (format)
import Data.List (inits, tails, transpose)

-- |
--
-- >>> :main
-- 28895
-- 31603
main :: IO ()
main =
do input <- [format|2023 13 (%s%n)*&%n|]
print (sum (concatMap (solver 0) input))
print (sum (concatMap solver' input))

findReflection :: Int {- ^ differences -} -> [String] -> [Int]
findReflection target xs =
[ i
| (i,l,r) <- zip3 [0..] (inits xs) (tails xs)
, not (null l), not (null r)
, target == sum (zipWith (\x y -> sum (zipWith val x y)) (reverse l) r)
]

solver :: Int -> [String] -> [Int]
solver target xs = findReflection target (transpose xs) ++ map (100*) (findReflection target xs)

solver' :: [String] -> [Int]
solver' xs =
take 1
[ new
| old <- solver 0 xs
, new <- solver 1 xs
, new /= old
]

val :: Char -> Char -> Int
val x y = if x == y then 0 else 1

0 comments on commit ded4fc4

Please sign in to comment.