diff options
author | Julien Dessaux | 2023-05-17 01:17:00 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-05-17 01:17:00 +0200 |
commit | 5a7340cc2d338f50316d6f03fbb64a5ff30536bc (patch) | |
tree | 133869e661af161c3f468aab15e06fa7d5951806 /2020 | |
parent | 2020-23 part 2 in haskell (diff) | |
download | advent-of-code-5a7340cc2d338f50316d6f03fbb64a5ff30536bc.tar.gz advent-of-code-5a7340cc2d338f50316d6f03fbb64a5ff30536bc.tar.bz2 advent-of-code-5a7340cc2d338f50316d6f03fbb64a5ff30536bc.zip |
2020-24 part 1 in haskell
Diffstat (limited to '')
-rw-r--r-- | 2020/24-Lobby_Layout/example | 20 | ||||
-rw-r--r-- | 2020/24-Lobby_Layout/first.hs | 65 | ||||
-rw-r--r-- | 2020/24-Lobby_Layout/input | 333 |
3 files changed, 418 insertions, 0 deletions
diff --git a/2020/24-Lobby_Layout/example b/2020/24-Lobby_Layout/example new file mode 100644 index 0000000..3dc2f67 --- /dev/null +++ b/2020/24-Lobby_Layout/example @@ -0,0 +1,20 @@ +sesenwnenenewseeswwswswwnenewsewsw +neeenesenwnwwswnenewnwwsewnenwseswesw +seswneswswsenwwnwse +nwnwneseeswswnenewneswwnewseswneseene +swweswneswnenwsewnwneneseenw +eesenwseswswnenwswnwnwsewwnwsene +sewnenenenesenwsewnenwwwse +wenwwweseeeweswwwnwwe +wsweesenenewnwwnwsenewsenwwsesesenwne +neeswseenwwswnwswswnw +nenwswwsewswnenenewsenwsenwnesesenew +enewnwewneswsewnwswenweswnenwsenwsw +sweneswneswneneenwnewenewwneswswnese +swwesenesewenwneswnwwneseswwne +enesenwswwswneneswsenwnewswseenwsese +wnwnesenesenenwwnenwsewesewsesesew +nenewswnwewswnenesenwnesewesw +eneswnwswnwsenenwnwnwwseeswneewsenese +neswnwewnwnwseenwseesewsenwsweewe +wseweeenwnesenwwwswnew diff --git a/2020/24-Lobby_Layout/first.hs b/2020/24-Lobby_Layout/first.hs new file mode 100644 index 0000000..b0ca43f --- /dev/null +++ b/2020/24-Lobby_Layout/first.hs @@ -0,0 +1,65 @@ +-- requires cabal install --lib megaparsec parser-combinators +module Main (main) where +import Control.Monad (void, when) +import Data.List qualified as L +import Data.Map qualified as M +import Data.Maybe (fromJust) +import Data.Set qualified as S +import Data.Void (Void) +import Text.Megaparsec +import Text.Megaparsec.Char +import System.Exit (die) + +exampleExpectedOutput = 10 + +data Direction = E | W | NE | NW | SE | SW +type Directions = [Direction] +type Coordinates = (Int, Int, Int) +type Floor = M.Map Coordinates Bool +type Input = [Directions] + +type Parser = Parsec Void String + +parseDirection :: Parser Direction +parseDirection = (string "se" *> return SE) + <|> (string "sw" *> return SW) + <|> (string "ne" *> return NE) + <|> (string "nw" *> return NW) + <|> (char 'e' *> return E) + <|> (char 'w' *> return W) + +parseInput' :: Parser Input +parseInput' = some (some parseDirection <* optional (char '\n')) <* eof + +parseInput :: String -> IO Input +parseInput filename = do + input <- readFile filename + case runParser parseInput' filename input of + Left bundle -> die $ errorBundlePretty bundle + Right input' -> return input' + +compute :: Input -> Int +compute input = M.size . M.filter id $ L.foldl' compute' M.empty input + where + compute' :: Floor -> Directions -> Floor + compute' floor directions = case M.lookup destination floor of + Just f -> M.insert destination (not f) floor + Nothing -> M.insert destination True floor + where + destination :: Coordinates + destination = L.foldl' run (0, 0, 0) directions + run :: Coordinates -> Direction -> Coordinates + run (x, y, z) E = (x+1,y-1,z) + run (x, y, z) W = (x-1,y+1,z) + run (x, y, z) NE = (x+1,y,z-1) + run (x, y, z) SW = (x-1,y,z+1) + run (x, y, z) NW = (x,y+1,z-1) + run (x, y, z) SE = (x,y-1,z+1) + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input diff --git a/2020/24-Lobby_Layout/input b/2020/24-Lobby_Layout/input new file mode 100644 index 0000000..c1cce32 --- /dev/null +++ b/2020/24-Lobby_Layout/input @@ -0,0 +1,333 @@ +sweswneswswswswwswswswseneswswnwswwne +swwswswswnwswwnwswswswwswswseenwsesw +eeeeseeeneeenesweweeeswee +swswswwneseneswswwnwswneswswwenewne +seesesesenewsenwseseesesesesesenwesee +nwsenwnwnewnewnwenenewnenenwneenenwne +seseswsesenwnwswsenwneswswsesenwseswswsesw +enwnwneswnewneswnwnenenwnesewsenenenwne +swswnwsweseseswswswseweswswswnwseswsesw +wwwweneswswwswwswswwwwswnewnww +wwwnewwswswseswewwsewswwwswwnwne +wswswwnwswwswwseewwweswwnesww +wnwnwsenenwnwnwsenwnw +nwwwsesenwnewsenenweneneeeswwsese +senenwnwwnwsewwewswnwnwewnwnwwnwnw +seswsesenwseswseseeseeseseewswneseww +ewswseneswseswswswnewnwwswswswnwewnw +nwnwnwnenwsesesenwnwwewnwnwwewwe +swswswswswswnwseneswwswsww +swseneweswswseswseeseswswswseswwnww +swseswswswneswswswewwwswwsw +wswwnwnewnwnwwnwswwnesenwnwwwnww +wwnewnewnwswwwwwsewwwswwwswese +wswsesewswswwwwenewwwwwwnwswsw +seeeeneweeesweeeeneeeeeew +swseswneneseeswweswneswsewnwswsewese +eseseeneswsenweswesewenwseneenww +neswnwnwnenwnwenenwwswnwswnenwnenwenw +swewswswswswswwenwswswwswswnewswnesw +eswwnwnwnewsenwwnewwwsweesesene +swneneswnwneneswnwwnenwneneneneeseswwsene +swnwnenenenwnewenwnenweeneswneswnenwswsw +seneewesesesesenwseswsesesesewseseesese +wseswsenwwnesenwnwenwneesewnwwnwwnw +nwnenenwnenwnwnenenwnwneeneesesweneswnwsw +nesenewneneneneneneenenenenenenewe +seneseseseswswseesenwsee +swsweswenwswneswwswswwswswewsweswsw +ewneswewwwnwwsewwswwwwwwwww +wwwneswnwnweseseewnenwewwswww +swneenwnwnwnwwwwnwsenwwwenwswnwnwww +seenwswseseesenwsenwseseseseseseswsese +seeesweeseeneesee +nwnwwsenewnwwnwnwwsenwnewsesenwsenwnw +wswswseswseneeswswswswseswswseswswnw +neeseesesewsesewseeseseeeeeenwse +swnwnwenewnwnwnwnwnwnwnwsenwnwnwnwnwnwe +wwenweswnweenwnwsewswneenewsenenw +nwseeweswseeswnwneseweenesenwenee +nwnwnwnwnwnwnwnwnwnwswsenewesenwnwnwnw +nwwneseneeneneneneneesenwnenene +neneeneeseneneneswneswnwnenenenenewnene +neneeswenwseneene +nesenenweswswswsesenwnesesewswswwsww +wwwsewnwnwwwnwnwneewse +wwwnwnwneswsenwewswswewnewswnwnw +neewwewneneeeseseneeneenewseneee +wswnweswnewswwswsewnenewwe +neneesesenwnwsweene +swnwenenwnwnwseswswneewnwnenwnesewesesw +seseswseswnwswswweseswseweswsesenwsee +swsesesenwwnwenwnwneeswseswseswesesenw +nwnwnwnwnwnwnwnwnwnenwsenwnw +senweeewewsewewnwesenewneswenwsw +nenwnenwwweswsenwseseswswswwnenewswenw +eseeesweenweeesesesewseeeenwe +neswnenenenweeswseeseswewswnwneneene +sewnwwswwwnwnwnenenwswenwwwnwnwnw +wseneseeeseseseseseseee +seeeeeseweseseseseewwsesesesesew +swseswnwswnwnesewsweswswsw +neseneneeneenenewseeenesweenwene +swseswsenewswseseseswseseseswse +nwnwnwwswwswwwnene +nwnwewnweneneenenwwwsenwswneswee +wnwswnwneswnwwnwwnewwenwwwsenwww +swneswneswswneenenenenwenweneneeew +wwswwswswswwswwseswswswswnwswne +swnwseesesesesesesenweseeseseseesewe +eenenwneneneswnenwneeneneeswnenenwneswne +seeseseeswseewnweenwsenwswesesene +wswswswswswseswsewwnwswswnww +eeeweneeswenenewweeeenenesenee +nwwwewswnwwwwewnwsesew +wswsenweenwnwwnwswewswnene +seseesenweswswseseswsesweswwswnwswse +eeneswenenenwswnwnenenesw +seseneswwnwneswswsweswnenwnwsenewswenesw +neneneneneswneneswnenenenwneswnenenenee +neeswnenenenenenenenwnewe +swswswnwseswseswsenwswswswseswsesw +nenwswswswswswswwsenwseswswwswswswswswesw +wseesenwwnwswseseenwseseseswseeswsenese +sewwnenewwwwwwswwwwwnww +wsewseseseneeeseeweeesew +nwnwnwnwenwnwnwnwnwnwnwenwnwnwswsenwswnw +neswwseswseswseswswswseseswnewseswswne +wswnwwnwenwwnwwnwwewwwww +nwnwnwswwwwnwewnewwwnwnw +eweeeseseeeeseee +wswwnwnwnwnwenwnwnwswnwnwwwnenw +nenenwnenenenenenenenesenenenenw +wneneswneseeneenenenenweneeeneene +wewnwenwwenwnwnwnesenwnwwnwnwew +seseswswneseseseseseseseneswsesese +neneneneneenwsewnesenenenenenwnwnewnene +wwwwwnewswseswwsewnwwwwneww +swwswwwwwwesewswwswwwswnene +nwenwnenwneneswnwnenesewnwnenenwnwnwne +seenwswswseseeswswnwenwenwnweesene +senwsewnenwneswwnwnwenwswenwwnwwnenw +newsenwseweesenewnwnwnwwwswnwnwnwsw +eseseseeewnwesewswseenwenwewne +wswneswswswswswswsw +sewswsesesesesesewswwseneseenesw +enenwnwsenwnwwsenwnwnwwwnwnwnwsewnw +senwewswsenenwwseesweweneeseswnew +senenenwnenenwnenwnwnenwenenwnwnwswwe +neeseseeseseeewseeseseee +wswwwnwsewnewswswnewwwse +nwnwnenwwwwswwsewwwewsewswswswesw +esweneeeneeeeeeenwne +swwwnewswnwwswwsenweseeewswswswnw +nwnwnwsesenwnwwwenenesewnwswwnwnwnwsw +nwswnenenenwnwnwenenenwswnweewnwnwne +nwnenwwnwnwewnwnwnwsenwnwsewnwnwsene +eswnwswswswsweswwswwsweswswswswwnwsw +enewnenenenenewneneeswnenwnenwneew +swswseswswneswewswwseswnwwnwneswneswsesw +nwewswseeswsenwwweswneesewenenwe +eswswseswseseswswnewse +nwneneneseneneneseneneneneneswnenenwnene +sesesenwenweeenwnwswwsweeneneenene +nwwnwnwsesenwnwnwnwnwnenenwwneswesenwne +seewnenwwnwnewwsenwswwwwwnwnew +eswneneeneenwneswneneneneswnewsenee +neneswnenwnenwnwnenene +eesweseeeeneeweeeeeenwenw +seseseseneseseseseswswnewnwsesewswswse +nwnwsenwswwnwnesenwenenenwneeswnwnwne +wneswwwwnwnewnwwswswwnwwnwwsene +eswneneswnweneneneneneeneene +nwnwwwsenwswswnwswenenwnew +swswswswwwnwwenwwswswswneswswseswe +ewwnwseseswnewswwneswwswnwswswee +eesewwneeeseneeseseweseeseee +eeweweeeeesenweeeeeeeesw +enwnenwnwnenwnwnwnenwswneswnwnwnwenwnw +nwnwwwswsewwnwnweswnwwnewswnewnwnw +eeneneewseneneeenenweeeweeseese +nwnwswsenewwwsewnwwwneww +newnenenwnwwnweseswsenenesenwnenew +eseseneseeeseseswwnwseeeenwseesese +seswswswnewnwnwewswsenweswseenwswse +swsenenwseswseseswwseneswsewswsenenew +wnenenwnenwnwewnwnenwseneenwnwnewnw +neneneenwwseeenwswseeewseneeewse +swewsesenwneseneeswenwsese +eswseesweeenenenweenenenewnenwne +newnenenenewnenesweneneneneenwnwnenesw +wnwwnwwnwwnwnwsewwnwnwnww +sewwwsewwwwwwnenwenenwwww +eseewnesenwseseseseneswsenwsesesenenww +neswswnewseswseeswswswsewswseseswswswe +seswwnwsweweswnwnesenwswseeenwsesw +nenenenenenenweneewwenesweswenenw +seseneseswsesewnwwseesenwnwsw +nwswnwweswnenwnwnenenenwnwnwswnenwnwnwnw +nwswnwewswswswwewwwswswswwswnewsw +seneseneeeeswswsenwnewnwwenenwwswne +seeseseseenwswsewseswwneswsenewnwswne +wwwwnwwewewswswwwwwwewnw +neeswewwwwwwswwwseenwnewwseew +neneswnwwnenwnwwswwnwnwesese +eeneneeneneneneneneewee +nwwswseneeeenewneeeeneenewsweesw +nweswsweswneswenwsweeneswswwswwnw +swneneneseneeeswnwswnweneneneenwneee +seenwswnwnwswswswswseswswesweswseswsene +swswswneswswnwenwsenesewenwnwsenwswswswe +eeneweeseseweeseeesesenesesese +nwnesenenwnwnenwnenwnewswneenenenenene +wnewnenwswwnwwsenwseseeswnenewewwsw +eeeseneweenenwwweewsweneeee +neneswenwswnwenesweneneeeneneeneeene +eneswweeswesenwnenenenee +nwswweswneesesenenwswswswe +neswseswseneswwneneneeswnwnenenewnenene +swswesesenwsenewseseseseswseeswswswswse +eswnwswswswseswswswswswsw +nwnwnwewnwnwneneenwwsenwnwnwnwwnene +enewswwseseswnenwneenenwnwsesesewnenene +eenwnwwswenwnwnwwenesenwewneswne +seseseseswseneseseneewswsesewsese +neneneneneneswswnewneenwneneewnenenee +ewswwswswwnwnwwwswswswseswwwwse +wnwneswwswswnwwnwweenenwnwewsenwne +sewneseenwseseseneenwswseseesew +enweeneseswswneeneweeswsesweenw +nwnwnwsenwnwnwnenwnwnwwnwnwnwnwsew +nwnenwwnwnwswnwnwwnwnwwwnwenwenww +nwnenenwneswswnwnwnenwnesenwsenenenenenenw +sesenesewnwseseeseew +swnwwnwsenwneswnwneenenwsenesene +seeseswwnewwnwewwwswesenwnewsenw +swsweswswweswwswewswswnwswwneswswsw +seeseswnwweseenesesesenesesenwwseew +swseswswswswsweswswsewswswenwswnesww +nwnwwwnwwnwswnwnwseenwenwsenwnwnenw +nwsewsewnwsesesenweswseseseseeesese +seeneeenwneneneeeenenenee +swnwwnwnwnwnwnwnenwnwnwnwnw +swseeswswseneeseswseeswsesenwwswswswnw +wnewwswsewwwwww +nwseeswnwnweewweswnewswnwweee +swseswsesenesesenwwseswseeswseseseenwse +swswnwwseswneeswwswneseswnew +seseeseneseseweeseesesenenwseswww +nesenwwewsesweesewseswswwswnwne +swnwwneeenwnwnwswswenwnwnenwenwwswnw +senwnwsenwwsenwnwne +swswswnwswwseneeneswwswnewneswnesewswse +wsenenweewwewnwwnwnenwnwnwesenw +nwnwnwnwwnwnwnwnwnwsenwswnwnwnwnwnwsene +neenenwneneseneesweneswwswne +eweeeesenweeeenwseweweeee +wswwweswsewwnewseswwwnewwwnesw +wnwnenwswnwnwnwnwwwnwwnwnw +nwnwewnwswneenenwnenwnwnwnenw +eneeweseeswswesenwnwseeswenwseene +eswnwnwsweeeswwswnwswwswwseenww +weswsesewnwwseeseweseseeeneenenew +nwnwnwnwnwwewnwnwnwwnwnwsenwnw +nwnwnwswnwnwnwnenewnwenwnwne +nwseneewneseneeseseseswsewwsesewsew +swsenweseseeeseswseeeseseewesenwne +wswnwnwwwewswnwwnewsenwnwweww +seesewswnwesenwswswsenwswswseswesw +neesewnwsenwsewnwsweswsenenewneewse +nwnwwwnwnwsewnewwwnwnwwnwnwswenw +neneswswswswsesenwswswswswwswseseswswsw +nweeswswneneeeneneewswneenenenwswne +wseeeweseeeeseneeeeenweeew +nenenewneneneneswneneseneeneeenenewne +nwnwnwenwnwnwnwnwnenwswnw +sewsesesenenesewseseseseneeesesewe +eeneseswseweeeeeeweseeeenwe +nwnwnwnwewnwwswnwnwnwwenwenwnwenw +nwnwnwsewseeseenww +nwenwnenenwswnwenenwneswneswnwnwnwnwnwne +wwswseneswseneweswenwseseswswsweswsw +swwwnweswswwwnesewsw +eneeeeeeswwneneeeee +nwnewwwwwseewswnewwwwwwnww +senwnwnwnwnwnwnwnwwsenenwnwnwnwnwsenwnwnw +seswnwsesewseswseeseseswewnwesenwswse +newneswneneeneenenenenenewne +swwwswswswweeswnwnewseseneseneenesew +wwewwnewswnwwwwnwnwswwsewwse +nenenesenesenewsenenwnwnesenewnwnenenenw +seeeenwesweweeeneneseeneseswsw +nwnwswnwsenewnenwnwnwnwsenwnenwnwnwnwnenw +swneneneneneenesewneeeseswewswnenee +sweewenwneenesenwsweneneeneneswne +sesweseseenwseseseswsenwseneseseseseese +nwsewwenenwseewnwewwsweswewnwse +seswswseseneesewwenwswwswneseeneswswse +sweswsweseswswswsweswswswswswnwswswwnw +newnwseeenwnwenesewwneswswne +nenwnenwwwneenenwnenwnenwswnwneseese +eseenwesesesenwseweseeeeswswnwe +nwnwwnwnwwenwnwnwwnwnwwsw +swwwseswwnwwswswenewwswswwwnwww +wnenwnenenwnwwnenwsenwnwsenwnwnesewsene +swnwwsewswnewwswwwewewwnwwsw +wsweswswneneswseneswswnwswswenwswse +swnwseseenewseswsenw +eesenesweeneesenwswseeseewnweee +eneeeeneeeneneewnesene +seeseneseswwneweseneeeswenwsesenesw +wswseswwswswwwneswneseswneeswwwwwsw +nwesesweewswnwenwnweswnwswsw +nwnwnwnenwnwsenwnesenwnwenwwnwnwwnwnw +swneswenwnwnweneenwnwswnwnenwnwnwnenenwse +nesweswnwswnwneneeneewnwneseneeswnenese +neeenwseneewneswnenwseeeneenenenenew +swnwnwenwnwnweswnwnwnwnwnwnwnenwnwnwnese +nesenewswwneneswnweneenwnenee +swswswswswneswswswswswswswswsw +neneneneeneneewswenenenwewneswenene +weneneseeneneeenenenweeeeneewswe +wwwsenwwsewnwenenenwneswwnewseseesw +newnwnesenesenwneesewnwwnwwnwenenw +seenwnwneeeneeeweenesweeesese +nwnenewnesenwwnwnenwnwnwnwwnwseenwwnwse +ewnenwswneseneeese +wneewseeswneneswsweeeenwnesenwne +swswseswnwsewsewnwswswwswnwswswnesesw +seseseseseeseesesesewsese +nwwswswwswsewwwswsww +swneswseeneswsewswswwswsw +neswwswswnewewnesewwesenesenesww +newwnenwneneseesenenenenenenenweswnw +weswsweswswswwswwneswswswswswswnwwsw +swswswswswneswenwswweswswseswseseswnw +wsewnwsewnwwwsweneww +seeeeeenweesweeseee +sesesesesesesesesewesenwswneneseseswnw +nwnenwnwnwsenwnwnwwnwnweneswwe +wnenwnwsewnwwnewnwsesenwnwsenewnw +seeseneswesewnwe +swwnenwnwenenwseswneswwnesesweeswnwnw +swwswneswswwwswwneseeswwswswswsenwsw +eewwwneeseneeewwewenenewne +swswswwseneeneswwswseswswnewswnewnwsw +newnenenewswenesenenwneneesewneenesw +neeseswnweswswsenwwsenwweesesesesesw +seeseeswenwnweeeeeeeenw +seswswswseswwseenwseseswseseseesesw +wswswswnenwswswseswswseswswseswswnesesww +esesenweeswseeeseenesenwese +eseseenwseseeseseweesewseseesesw +enwnwnwsenwnwswnwnwnwnwnwnwnenwne +esesewneneeseswseewwnweeseeswnwse +wsewwenwswnwwsewwnwwwwswwwew +nwswswswseneswswswneseseeswwwnesenesesw +nwsesenwseeseesesweseseseeseeseeew +enewwwswwewwewwnenwsewwwwse +nwnweneeswewwnwnenwenwwwenenwnw +wwwwwneswswswwwwwewwwswnew +eswseseswnwswsewsw +wsweswwwenwwwsesewweswnwswwnwsw +nwsewwwnwnwsenwnweewwnwwnenewnw |