heh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Control.Exception
import Data.Char
import Data.Foldable
import Data.Function
import Data.Functor
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Void (Void)
import Debug.Trace
import System.Exit
import Text.Megaparsec
import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer
import Text.Megaparsec.Char.Lexer qualified as L

type Parser = Parsec Void String

newtype SwapP = SwapP (Int, Int) deriving (Show)

newtype SwapL = SwapL (Char, Char) deriving (Show)

newtype RotateS = RotateS (Bool, Int) deriving (Show)

newtype RotateP = RotateP Char deriving (Show)

newtype Reverse = Reverse (Int, Int) deriving (Show)

newtype Move = Move (Int, Int) deriving (Show)

data Line = SwapP_ SwapP | SwapL_ SwapL | RotateS_ RotateS | RotateP_ RotateP | Reverse_ Reverse | Move_ Move deriving (Show)

j a = string a & s

skipSpace = L.space space1 empty empty

integer :: Parser Int = read <$> some numberChar & s

ch = s anySingle

s :: Parser a -> Parser a
s = L.lexeme skipSpace

main :: IO ()
main = do
  contents :: String <- readFile "src/inp.txt"
  let nums = map digitToInt contents
  print (length nums)
  let w = take (length nums) (windows (nums ++ nums) ((length nums `div` 2) + 1)) -- or 2
  let e = [head x | x <- w, head x == last x]

  print (sum e)

swapTwo f s xs =
  zipWith
    ( \x y ->
        if x == f
          then xs !! s
          else
            if x == s
              then xs !! f
              else y
    )
    [0 ..]
    xs

windows c n = (\x -> take n (drop x c)) <$> [n .. length c - n]

rotateL n xs
  | n >= 0 = take (length xs) $ drop n $ cycle xs
  | otherwise = rotateL (length xs + n) xs

rotateR n = rotateL (-n)