heh
2017/1: 14.23
bendn 6 months ago
parent 0935c3c · commit c130f8c
-rw-r--r--solve.hs89
-rw-r--r--src/inp.txt5
2 files changed, 8 insertions, 86 deletions
diff --git a/solve.hs b/solve.hs
index df7389d..2e20017 100644
--- a/solve.hs
+++ b/solve.hs
@@ -1,4 +1,5 @@
import Control.Exception
+import Data.Char
import Data.Foldable
import Data.Function
import Data.Functor
@@ -33,98 +34,22 @@ j a = string a & s
skipSpace = L.space space1 empty empty
-integer = read <$> some numberChar & s
+integer :: Parser Int = read <$> some numberChar & s
ch = s anySingle
-swapP :: Parser SwapP
-swapP = do
- x <- j "position" *> integer
- y <- j "with position" *> integer
- pure (SwapP (x, y))
-
-swapL :: Parser SwapL
-swapL = do
- x <- j "letter" *> ch
- y <- j "with letter" *> ch
- pure (SwapL (x, y))
-
-lr = (j "left" $> True) <|> (j "right" $> False)
-
-rotateS :: Parser RotateS
-rotateS = do
- side <- lr
- count <- integer <* (j "steps" <|> j "step")
- pure (RotateS (side, count))
-
-rotateP :: Parser RotateP
-rotateP = RotateP <$> (j "based on position of letter" *> ch)
-
-reverse :: Parser Reverse
-reverse = do
- x <- j "reverse positions" *> integer
- y <- j "through" *> integer
- pure (Reverse (x, y))
-
-move :: Parser Move = do
- x <- j "position" *> integer
- y <- j "to position" *> integer
- pure (Move (x, y))
-
s :: Parser a -> Parser a
s = L.lexeme skipSpace
-line :: Parser Line =
- s
- (j "swap" *> (SwapP_ <$> swapP <|> SwapL_ <$> swapL))
- <|> (j "rotate" *> (RotateP_ <$> rotateP <|> RotateS_ <$> rotateS))
- <|> (Reverse_ <$> Main.reverse)
- <|> (j "move" *> fmap Move_ move)
-
--- swap position X with position Y means that the letters at indexes X and Y (counting from 0) should be swapped.
--- swap letter X with letter Y means that the letters X and Y should be swapped (regardless of where they appear in the string).
--- rotate left/right X steps means that the whole string should be rotated; for example, one right rotation would turn abcd into dabc.
--- rotate based on position of letter X means that the whole string should be rotated to the right based on the index of letter X (counting from 0) as determined before this instruction does any rotations. Once the index is determined, rotate the string to the right one time, plus a number of times equal to that index, plus one additional time if the index was at least 4.
--- reverse positions X through Y means that the span of letters at indexes X through Y (including the letters at X and Y) should be reversed in order.
--- move position X to position Y means that the letter which is at index X should be removed from the string, then inserted such that it ends up at index Y.
-
-apply :: String -> Line -> String
-apply x (SwapP_ (SwapP (a, b))) = valid x & swapTwo a b
-apply x (SwapL_ (SwapL (a, b))) =
- ( \x ->
- ( case x of
- c | c == a -> b
- c | c == b -> a
- c -> c
- )
- )
- <$> valid x
-apply x (RotateS_ (RotateS (True, n))) = rotateL n (valid x)
-apply x (RotateS_ (RotateS (False, n))) = rotateR n (valid x)
-apply x (RotateP_ (RotateP l)) =
- let index = fromMaybe 0 (elemIndex l (valid x))
- in let times = index + 1 + if index >= 4 then 1 else 0
- in rotateR times x
-apply l (Reverse_ (Reverse (x, y))) =
- let middle = take (y + 1) (valid l) & drop x & Data.List.reverse
- in take x l ++ middle ++ drop (y + 1) l
-apply l (Move_ (Move (x, y))) = do
- let elem = valid l !! x
- let removed = take x l ++ drop (x + 1) l
- take y removed ++ elem : drop y removed
-
-valid l = assert (length l == 8) l
-
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]
- let x = runParser (many line <* eof) "inp" contents
- x <- case x of
- Left err -> exitFailure
- Right output -> pure (foldl' apply "abcdefgh" output, head [x | x <- permutations "abcdefgh", foldl' apply x output == "fbgdceah"])
-
- print x
+ print (sum e)
swapTwo f s xs =
zipWith
diff --git a/src/inp.txt b/src/inp.txt
index bade1ea..547f1fe 100644
--- a/src/inp.txt
+++ b/src/inp.txt
@@ -1,4 +1 @@
-The first floor contains a 1 generator, a 2 generator, a 2-compatible microchip, a 3 generator, a 4 generator, a 5 generator, a 5-compatible microchip, a 6 generator, a 6-compatible microchip, a 4-compatible microchip, a 7 generator, and a 7-compatible microchip.
-The second floor contains a 1-compatible microchip and a 3-compatible microchip.
-The third floor contains nothing relevant.
-The fourth floor contains nothing relevant.
+36743676522426214741687639282183216978128565594112364817283598621384839756628424146779311928318383597235968644687665159591573413233616717112157752469191845757712928347624726438516211153946892241449523148419426259291788938621886334734497823163281389389853675932246734153563861233894952657625868415432316155487242813798425779743561987563734944962846865263722712768674838244444385768568489842989878163655771847362656153372265945464128668412439248966939398765446171855144544285463517258749813731314365947372548811434646381595273172982466142248474238762554858654679415418693478512641864168398722199638775667744977941183772494538685398862344164521446115925528534491788728448668455349588972443295391385389551783289417349823383324748411689198219329996666752251815562522759374542652969147696419669914534586732436912798519697722586795746371697338416716842214313393228587413399534716394984183943123375517819622837972796431166264646432893478557659387795573234889141897313158457637142238315327877493994933514112645586351127139429281675912366669475931711974332271368287413985682374943195886455927839573986464555141679291998645936683639162588375974549467767623463935561847869527383395278248952314792112113126231246742753119748113828843917812547224498319849947517745625844819175973986843636628414965664466582172419197227695368492433353199233558872319529626825788288176275546566474824257336863977574347328469153319428883748696399544974133392589823343773897313173336568883385364166336362398636684459886283964242249228938383219255513996468586953519638111599935229115228837559242752925943653623682985576323929415445443378189472782454958232341986626791182861644112974418239286486722654442144851173538756859647218768134572858331849543266169672745221391659363674921469481143686952478771714585793322926824623482923579986434741714167134346384551362664177865452895348948953472328966995731169672573555621939584872187999325322327893336736611929752613241935211664248961527687778371971259654541239471766714469122213793348414477789271187324629397292446879752673 \ No newline at end of file