From feb0858e53367274ce7885b1d7b4eec3f570e369 Mon Sep 17 00:00:00 2001 From: cdimitroulas Date: Fri, 15 Dec 2023 16:56:36 +0000 Subject: [PATCH] Day13 --- advent-of-code2023.cabal | 3 + data/day13.txt | 1321 ++++++++++++++++++++++++++++++++++++++ package.yaml | 1 + src/Main.hs | 4 +- src/Solutions/Day13.hs | 92 +++ test/Day13.hs | 67 ++ 6 files changed, 1487 insertions(+), 1 deletion(-) create mode 100644 data/day13.txt create mode 100644 src/Solutions/Day13.hs create mode 100644 test/Day13.hs diff --git a/advent-of-code2023.cabal b/advent-of-code2023.cabal index b37c3c7..61a24d4 100644 --- a/advent-of-code2023.cabal +++ b/advent-of-code2023.cabal @@ -31,6 +31,7 @@ library Solutions.Day09 Solutions.Day11 Solutions.Day12 + Solutions.Day13 other-modules: Lib.AOC Lib.Common @@ -109,6 +110,7 @@ executable advent-of-code2023 Solutions.Day09 Solutions.Day11 Solutions.Day12 + Solutions.Day13 Solutions.DayX Paths_advent_of_code2023 autogen-modules: @@ -170,6 +172,7 @@ test-suite unit-test Day09 Day11 Day12 + Day13 Paths_advent_of_code2023 autogen-modules: Paths_advent_of_code2023 diff --git a/data/day13.txt b/data/day13.txt new file mode 100644 index 0000000..9b14c12 --- /dev/null +++ b/data/day13.txt @@ -0,0 +1,1321 @@ +.##.##.##.... +#.######.##.. +##......###.. +...####...... +...####...### +..#....#..#.. +.#.####.#.### +..#....#..### +.##.##.##.... +..######..### +#.##..##.#### +#.#.##.#..... +##..##..##.## +.#..##..#.... +#.##..##.##.. + +.##.#.##.#. +#..##..#### +#..##..##.# +.##.#.##.#. +....#.##... +.#.###..##. +...#.##..## +.#....#.### +.#....#.### + +##.#.####.#.####. +#..##.##.##..#..# +..###....###..### +.###.####.###..## +..#........#..##. +#..###..###..#.## +#..###..###..#.## +..#........#...#. +.###.####.###..## + +.....####.. +##...####.# +##..#....#. +.....####.. +####.####.# +##..#.##.#. +##.###..### +...#......# +..#.######. +##.##.##.## +..#..#..#.. +##.#.####.# +####..##..# + +##..##.#.##.#.# +..##..#....#... +#.#...#.#.###.# +####.##..#.#..# +#..#....#..#### +##.#.#..#....## +#.#.####.#..### +#.#.####.#..### +##.#.#..#.#..## +##.#.#..#.#..## +#.#.####.#..### +#.#.####.#..### +##.#.#..#....## + +#..##...#.####.## +#..##...#.####.## +....###.#..#.###. +#####..####..#.## +#..#..##...###..# +.##.##..#..###... +.##.#.#.#...#..#. +##.#...#.#.....#. +........######.#. +....#..#....#..## +#######..#.##..#. +#..#...#######..# +....###..###.##.# + +#.#..#.##..## +##.##.##....# +###..##...... +###..###.##.# +.######.#..#. +##.##.##....# +#......##..## + +#.#..#.###..... +..####..#..#..# +#......##..#### +#..##..#.#.#..# +...##....#.#... +.######...#.... +#..##..##.#.... + +..##... +..#.#.. +..#.#.. +..##... +.#..### +##....# +....#.. +#..#... +.##.... +...#... +.####.. + +##..##..### +#.##.###### +#.##.###### +##..##..### +#######..## +.#####..#.# +.####....#. + +#....#...## +.####...... +##.#..#...# +...#.#..### +...#.#..### +##.#..#...# +.####...... +#....#...## +.#.##...... +..##.#.#..# +..##.#.#..# +.#.##...... +#....#...## +.####...#.. +##.#..#...# + +##.#... +##.#... +.#.#.## +#.###.# +......# +..#.##. +#.#..## +#.#...# +#.#...# +#.#..## +..#.##. +......# +#.#.#.# +.#.#.## +##.#... + +.#...###.##.. +#..#.##..#... +#.#.#..###.#. +.#.#####....# +..##..#..#.## +..##..#..#.## +.#.#####....# +#.#.#..###.#. +#..#.##..#... +.#...###.##.. +.#...###..#.. +#..#.##..#... +#.#.#..###.#. +.#.#####....# +..##..#..#.## + +#..#.#.##...#.#.. +..##....####.##.# +.###.###...#.#### +###....#.......#. +.#.#######..#.### +#..#..##..##.#..# +#..#..##..##.#..# +.#.#######..#.### +###....#.......#. +.###.###...#.#### +..##....####.##.# +#..#.#.###..#.#.. +#..#.#.###..#.#.. +..##....####.##.# +.###.###...#.#### + +######....##..##. +..#..#####.#.###. +.#####...######## +######...######## +..#..#####.#.###. +######....##..##. +.##...#.#....#... +##..##.......###. +.###..###.###...# +....#.##.###.##.. +..##.#...####...# +..##.#...#.###.#. +#.#...#.#....##.# +##.#.##..#......# +##.#.##.##.##..#. +...#...#.#.###.## +...#...#.#.###.## + +..#.##..# +##.#..### +##..#...# +###.##.## +###.##.## +##..#...# +##.#..### +..#.##..# +.##..##.# +###.###.. +#..##.... +#.#...#.. +..####..# +..####..# +#.#...#.. +#..##.... +###.###.# + +..#.##.#.##.#.. +##..##.#.####.# +.....#...#...#. +.####....###### +.####....###### +.....#...#...#. +##..##.#.####.# +..#.##.#.##.#.. +#..#####.#.#.#. +####..#.###..#. +.....##...##..# +..#.#.###..#... +#...#.##..#...# +.##.##..##.#... +.##.##..##.#... +....#.##..#...# +..#.#.###..#... + +......#..#. +.###.###### +###...#..#. +.#.###.##.# +#..##...... +##..#.#..#. +##..#.#..#. +#..##...... +.#.###.##.# +###...#..#. +.########## +......#..#. +...###....# +..#...####. +.#.#.#.##.# + +#.##..##.## +###.##.#### +#..####..#. +#..####..#. +###.##.#### +#.##..##.## +.........#. +#...##...## +###.##.###. +....##....# +#..####..#. + +.#.##.....#.##.#. +.##...####.####.# +......###........ +.#...##..#......# +.#...#.#.##....## +#.#.#..###......# +.#.##.....######. +.#.##.....######. +#.#.#...##......# + +###..##.. +######..# +...##.... +##.#.#.## +###..#... +###..#... +##.#.#.## +...##.... +###.##..# + +.....#....#.# +.##.#.##.#### +#####..###..# +.##.###.#.##. +....#.#.....# +#..#..#....#. +......##.#... +#######..#.## +###.###.####. +####.##...#.# +...........## +....#.#.##.#. +#####.#.##.#. +#####..#.#.#. +#..##..####.. +#..##..####.. +#####..#.#.#. + +.#.#....#.###.# +####...#..##.## +#.#...#..#...## +#.####..#.#..#. +..#..##.#.#...# +##..#..###.#### +##..#..###.#### +..#..##.#.#...# +#.####..#.#..#. +#.#......#...## +####...#..##.## +.#.#....#.###.# +.#.#....#.###.# + +##..#..#..# +##.######## +###..##..## +..###..###. +##.######.# +##........# +..##.##.##. +....####... +##..####..# +...#....#.. +..##.##.##. + +##...##.####. +#...#..##.##. +.##.##.#.#..# +..###..#####. +..###..#####. +.##.##.#.#..# +#...#..##.##. +##...##.####. +##...##.####. +#..##..##.##. +.##.##.#.#..# + +.........#. +##.#.#..### +##.#.#..### +.........#. +#.#.##.#.#. +#.##.#.#..# +.##.##.#.#. +###.###.... +#.#.####.## +#.#.####.#. +###.###.... +.##.##.#.#. +#.##.#.#..# +#.#.##.#.#. +.........#. + +###.... +####.## +###..## +...##.. +...#### +....#.# +##.#.## +..#..## +....### + +.#..#.###.# +..##....### +#.##.#..#.. +##..##.#... +##..##.##.# +##..####.#. +##..####.#. +##..##.#..# +##..##.#... +#.##.#..#.. +..##....### + +.##...# +.##.... +####... +.##..#. +....#.# +#####.. +#..##.. +....#.# +.##..#. + +.....###..### +#..#.######## +####.#......# +#.########### +#.########### +#.##.#......# +#..#.######## +.....###..### +.##.##.####.# +..#....#..#.. +..##.#.####.# +#.###........ +....#.#....#. +.#######..### +.###.#.####.# +#.#.#...##... +##...#.####.# + +#....##.#.##. +..#.#..#.#..# +..##....#.... +###..####.##. +#..##.....##. +.#...#.###### +.#...#.###### +#..##.....##. +###..###..##. +..##....#.... +..#.#..#.#..# + +.##.... +##.#### +#.##### +#..#..# +.###### +...#### +...#### +###.... +.#.#### +##..... +#.##### +...#### +#..#### +...#### +#...... + +....###.# +#..###... +####.##.# +.....#..# +####...## +#..#..#.. +....##... +.....#..# +.##....#. +.##..#... +#####.... +.##...#.# +.##...... +.##..#... +.##...#.# +#####.... +.##..#... + +###.##.########## +...####...#..#... +.#.#..#.#......#. +##..##..########. +#...##...##...#.. +###.##.########## +##......###..###. +.##....##......## +..######..####..# +##.####.###..###. +.###..###..##..## +....##........... +##########....### +.#.#..#.#.#..#.#. +.#..##..#.#..#.#. +..##..##...##...# +..#....#........# + +#..#....#..#### +....#..#.....#. +##.##..##.##.#. +...######...... +#..#....#..#.## +#..#....#..#.## +...######...... +##.##..##.##.#. +....#..#.....#. +#..#....#..##.# +..#..##..#....# +#...####...###. +#.#......#.#..# +....#..#....#.. +...#.##.#....#. + +##..##..##. +.#..#....#. +######..### +.#..#.##.#. +#.##.####.# +.####....## +#.##.#..#.# +.####....## +.#..#.##.#. +######..### +######..### +######..### +......##... +.##.#....#. +#.##.####.# + +#...#.#.#........ +#.#.####.######## +####.#.#..##..##. +#.#######.######. +#.###.###.##..##. +#.###.#.######### +##.#.############ +.#.....##........ +..#.###..######## + +#..##.#...# +#...#..###. +##...#.#### +.##.##.#.## +.##.##.#.## +##...#.#### +#......###. +#..##.#...# +.##..#.#.## +.##..#.#.## +#..##.#...# + +.###.##.##. +#.##..#.... +#..#.###..# +#..#.###..# +#.###.#.... +.###.##.##. +.#.#####..# +...#.#..... +##.....#### + +##..#..#..# +##..#..#..# +...#..#.#.. +.#.##..##.# +###......## +#.###..###. +###.####.## +###.#..#.## +#..#.##.#.. +##........# +..#.####.#. +..#..##..#. +....#..#... + +###...####....# +..#..#.#.#.##.# +..#..#.#.#.##.# +###.#.####....# +#.#..##........ +..##.########## +##..#..#....... +#...#.#.####### +#..###.###.##.# +##..########### +#..#..#.##.##.# +#...###..##..## +.##....#..####. +#.###...#.#..#. +###.....#..##.. + +....####. +.#....... +....####. +#........ +#........ +....####. +.#....... +.#..####. +#.###..## + +.#.##.##.....##.. +.######.#..###### +#....#..#.##....# +..####.###.##..## +..#..##..#...###. +.##.##.#.....##.. +.##.##.#.....##.. + +..........##### +.#..##..#..#.## +#..#..##.##.#.. +####..####.#### +#.##..##.##.### +...#..#...#..## +.#.####.#.##### +...#..#...#.#.. +.#......#...... +...#..#...#..## +#..####..##..## + +...##.#.. +##.#...## +...#..#.# +...#..#.# +##.#..### +...##.#.. +..##...## +#####.### +####.##.. +..#.##### +####..#.# +....##... +##.###.#. +##..###.# +####.#.## + +..#.#.# +.##.##. +#####.. +####.#. +##.#..# +##.#..# +####.#. + +..##..#.##.#. +#.###.######. +##..##.#..#.# +.#.##...##... +..####.####.# +...#.###..### +...#.###..### +#.####.####.# +.#.##...##... +##..##.#..#.# +#.###.######. + +..#####..#..... +.##.#.#......## +.#####.###..### +##......###.#.. +###..##.#..#.## +..##.###....#.. +.#.##...####... +.#.##...###.... +..##.###....#.. +###..##.#..#.## +##......###.#.. + +.##......##.#.... +#####..######.... +##.#....#.####..# +..##.##.##..#.##. +..#......#..##..# +#.#..##..#.###### +...#....#.....##. +...######...#.... +...##..##....#..# +#.###..###.#..... +.#...##...#...##. +#.#.#..#.#.#..... +.#...##...#.##... +##...##...###.... +..#.#..#.#....##. +..##.##.##..##### +.##.#..#.##.#.##. + +#....#.#.#..#.#.. +..####..#.####### +..####..#.####### +#....#.#.#..#.#.. +.#.#####..#####.. +#..###.#..#.##..# +#..#.#...###.###. +#.#.##.##....##.# +#.#.#....##...#.. +..##.##.......#.. +..##.##.......#.. +#.#.#....##...#.. +###.##.##....##.# +#..#.#...###.###. +#..###.#..#.##..# + +.##......##.. +.##..##..##.. +#..#.##.#..## +#..######..#. +#..#....#..## +.##.#..#.##.. +.##.####.##.# +##.#....#.### +####....##### + +#..#####. +.....#... +#..#..#.. +#..#..#.. +.....#... +#..#####. +.####.... +######.## +#..#.#.## +....##.#. +....##... +####.#..# +#..#..... +######.#. +.##.##..# + +......# +#....## +....#.. +##..##. +......# +##..### +......# +..##... +..##..# +#....## +#....## + +.#..####..#..## +..#.#..#.#..### +##..#..#..##... +...#.##.#...### +##..####..##### +.#...##...#.### +.##..##..##.### +..#......#..### +#.#......#.#### +############... +#.#.##.#.#.#.## +#..........#### +..##.##.##...## +.##.####.##.#.. +#..######..#... + +##.###..#.###.### +.###.##......#.## +..#..##..##.##.## +..#..##..##.##.## +.###.##......#.## +##.###..#.###.### +..#.#.#..#..###.. +.#.#.#.#.#.#####. +.#.#.#.#.#.#####. +..#.#.#..#..###.. +######..#.###.### +.###.##......#.## +..#..##..##.##.## + +##..##..####..# +.#.#.....##.... +..##.#.##..##.# +.#..##.##..##.# +.###..#.#..#.#. +##...###....### +#...####....### +#..#..#......#. +...#..#......#. + +#.#..#.#.## +#########.. +..####..#.. +#......#.## +#......#... +#.####.#### +..#..#..### +#########.. +..#.....### +.#.##.#.### +#.#..#.##.. + +####..##. +####..##. +..###..## +#...#...# +...#.##.. +#...#.### +#####...# +..###.#.. +..###.#.. +#####...# +#...#.### +...#.##.. +#...#...# +..###..## +####...#. + +.######.... +..#........ +###.#.##### +###..#.#..# +##..#.#.... +.#.###.#..# +##.####.... +#..##..#..# +.#..####..# +....####..# +#..##..#..# +##.####.... +.#.###.#..# +##..#.#.... +###..#.#..# +###.#.##### +..#........ + +#.####.###..###.# +..####..#.......# +#.#..#.###..###.# +..####...#..#...# +########......### +##.##.###....###. +.##..##..#..#..## +..####..#....#..# +#......##.##.##.. +###..#####..##### +########.#..#.### + +#.#.#.# +..####. +##.#### +#..##.# +#.#..#. +.#..... +####.## +.##.#.# +.#..#.# +..##### +..##### +.#..#.# +.##.#.# + +..#.... +...#### +..##..# +##.#..# +...#..# +##..... +....##. +.##.... +##..... +####..# +###.... + +.##.##.##..#..#.. +.........#.#..#.# +#...##...##....## +#.#....#.#.#..#.# +#.######.#.####.# +...#..#...######. +##......######### + +..#.###.. +....####. +..#.####. +###...##. +.....#.## +#####.##. +###...... +#.#.#.##. +##.#.##.. +##......# +##.#.##.. +..#.#...# +...##...# +...##...# +..#.#...# + +.####.#.####.## +.##...##.#..#.# +#..##.##.###..# +.##........#### +#..##..#.#..##. +....##...###... +.##...###...### +.##...###...### +....##...###... + +..#.#..##.#..## +#.#.##.###.##.. +#.#####.....#.# +..#....######## +.#.#.#.##.#..## +.#.#.#.##.#..## +..#....######## + +##.#####..####### +###..#.#...##..## +#....#..#.#...... +#.#..#...####..## +....#.########### +.....####..###### +.#..###.....#..#. +##.###.#.#.##..## +###.#.##.#.##..## + +#..#..#...##. +#..###...#..# +###.##..##### +.#..#.#...... +.####.###.##. +#.##..#.#.... +####..#.#.... + +#.#..####..#.## +..#...##...#... +#.##########.## +..####..####..# +#####.##.###### +.#.##.##.##.#.# +##..######..##. +.#...#####..#.# +.###..##..###.# +#.#.#.##.#.#.## +.#.#..##..#.#.. +##..######..##. +.#.##.##.##.#.. +.#..#....#..#.# +.#..#....#..#.# + +.#####... +##....#.# +#..#.#.#. +..#.##... +#.#.##... +#..#.#.#. +##....#.# +.#####... +.#.###.#. +...###... +...###... + +##.######.### +####.##.####. +.##.####.##.. +###..##...### +#####..#####. +###.####.###. +#...####...#. +##.#.##.#.##. +.#.#.##.#.#.. +##.##..##.### +##........### +##........### +##.##..##.### +.#.#.##.#.#.. +##.#.##.#.##. +#...####...#. +###.####.###. + +#.##.##..####.# +......##....#.. +..##..#...##..# +......#####.... +..........#.##. +..##..####.#### +.#....##......# +##..##.##..#### +.......#####... +##..####..#.#.# +.......#.##.#.. +..##....##...#. +.#..#......#### +#######.#.##... +#######.#.##... + +#.##.##.# +##..#..#. +.####..## +##..####. +.#..#..#. +#....##.. +#.##.##.# + +##.##.#.. +##..##.## +###.#.... +##....... +##.#..#.. +..#.###.. +..####### +......#.. +###.#.... +....#.### +..###..## +.......## +...###### +..#....#. +..#..#.## + +##........... +......#..#... +##........... +#..#.#.##.#.# +#..#.#.##.#.# +##........... +......#..#... +##........... +#.#.##.##.##. +.....######.. +..###......## +#.##.#....#.# +.##.##.##.##. +.....######.. +####.#.##.#.# +#####.####.#. +##.###....### + +#.#..#..#.# +..##.##.### +..##.##.### +#.#..#..#.# +##...#.##.# +###...#.##. +##.###..... +#..#.##.#.# +#....##.#.# +##.###..... +###...#.##. + +####.#.##...# +.##...##.#### +#####...#.... +.....#.##.### +########.#... +####.#.##.### +....###.####. +.....#.##..#. +######..##.## + +..#.##.#.## +.#....#.... +####...##.. +####...##.. +.#......... +..#.##.#.## +.......##.. +###.#.##.## +...#.#..... + +..#..#.####.# +#####.##..### +##.#.##.##.## +..#..###..### +###.###.##.## +..#...##..##. +###.#........ +##.#......... +####..#....#. +...#...#..#.. +##...##....## +...#..#.##.#. +....#.######. +###.#..#..#.. +..#...#....#. + +####.#... +....#.... +.##..#.#. +.##..#.#. +....#...# +####.#... +########. + +..####.#. +.#..##... +.#..##... +..####.#. +.###...#. +##.#..#.# +##.#..#.. +##.#..#.. +##.#..#.# +.###...#. +.#####.#. + +..######. +##.####.# +...####.. +##..##..# +####..### +###.##.## +..#.##.#. +##......# +..#.##.#. +...####.. +####..### +...#..#.. +###.##.## +###.##.## +##.####.# +...###... +###....## + +#..#..#..#.###... +.##....##..####.# +...#..#...##..#.. +####..####..#...# +.########..#..#.# +###.##.####...#.# +...####......#..# +##..##..###..#... +.#.#..#.#..#.#... +#........###.##.# +#..#..#..####.### +.##.##.##.#..#.#. +###....###.##..## +###....###.##..## +.##.##.##....#.#. +#..#..#..####.### +#........###.##.# + +####.#.## +#..###.## +#..#.#### +.##.##..# +.##...##. +####..#.. +....#.##. +#..#..### +####..... +.....#... +.##..##.# +....#.#.. +....#.#.. +.##..##.# +.....#.#. +####..... +#..#..### + +###.### +##...#. +....#.# +##.#..# +##.#..# +....### +##...#. +###.### +....##. +..#.### +###..## +...###. +###.#.. + +###....## +...#..#.. +###.##.## +##.####.# +##.#..#.# +...#..#.. +###....## +###....## +...#####. +####..### +##.####.# + +.#..##.#.##..###. +.#...##.#.#.#...# +.#...#.#....#.#.# +..#..#####...##.# +..#..#####...##.# +.#...#.#....#.#.# +.#...##.#.#.#...# +.#..##.#.##..###. +##..#.##.#.###..# +#...#..###...###. +..#..#.#.####.### +..##.#.#.##.##... +..##.#.#.##.##... +..#..###.####.### +#...#..###...###. +##..#.##.#.###..# +.#..##.#.##..###. + +##.############.# +###.#.######.#.## +..##.#..##..####. +##.#..#.##.#..#.# +..#####....#####. +##...#..##..#...# +..#.#.#....#.#.#. +...#..#.##.#..#.. +..#.##..##..##.#. +........##....... +###..##.##.##..## +##...#.####.#...# +..#.##.####.##.#. +.....#......#.... +#####.#....#.#### +..#..########..#. +#####.######.#### + +.###.#.#....# +..##.#.#....# +#.#.#.##....# +###.#.##....# +.##.###..##.. +#.#..#.###### +#..###....... + +....##..##... +##..#....#..# +###.######.## +##..##..##..# +######..##### +.....####.... +#####....#### +#...#....#... +.....#..#.... +...#.####.#.. +##.#..##..#.# + +..###..#### +..#..##.### +.###..#.... +.###..#.... +..#..##.### +..####.#### +#..##.#.... + +##.##...#.... +###.#.#.##..# +........##.#. +######....##. +#..#.....##.. +###.##.#.#..# +##...#..#..#. +##.##..#.#.## +##.##..###### +....#..#####. +...#.....###. +...#.....###. +....#..#####. +##.##..###### +##.##..#.#.## + +...#...#####. +..##.##..##.. +..##.##..##.. +...#...#####. +##...#.###..# +###....#.#... +#.###.#..##.. +#.##.#..#.... +#.##.#..#.#.. + +#....###..#.# +#....###..#.# +.####.##..... +.####..##.#.. +.#..#..#.###. +..##....#...# +.####.#...#.. +#....#.##...# +.........#.## +..##..#..#..# +........#.#.. +#.##.#....##. +#....#.##.### +.####..#.##.# +.#..#....##.. +#.##.##.....# +#.#..#..###.. + +...###. +..##### +...#.## +####.## +##.###. +#.#...# +....##. +##...## +##...## + +..######. +##..##..# +##..##..# +..#....#. +..#.##.#. +###.##.## +..#....#. +....##... +.##....## +##.#..#.# +##.#..#.# +###.##.## +...#..#.. +##..##..# +..#.##.#. +...####.. +..#....#. + +####.##..#### +...#.#.#..#.# +##.###.....#. +##.###.....#. +.....#.#..#.# +####.##..#### +###.#...##.#. + +#.#............ +###.#.......... +...######..#### +###.#..#.##.#.. +.#....##.##.##. +###...##.##.##. +..#.#....##.... + +########.##.# +#.####.#####. +##########.## +######.#.###. +..#..#..##... +#.#..#.#..### +..#..#...###. +...##...###.. +#..##..##...# +##.##.###.##. +##.##.###.##. + +###.#####.# +###.#####.# +#..##.####. +####.#...#. +#.....#..## +.##.#..##.# +##.##.#...# +##.##.#...# +.#..#..##.# +#.....#..## +####.#...#. diff --git a/package.yaml b/package.yaml index 06e5f21..7bab130 100644 --- a/package.yaml +++ b/package.yaml @@ -81,6 +81,7 @@ library: - Solutions.Day09 - Solutions.Day11 - Solutions.Day12 + - Solutions.Day13 tests: unit-test: diff --git a/src/Main.hs b/src/Main.hs index fba6aca..6253a82 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -10,6 +10,7 @@ import Solutions.Day08 (day08) import Solutions.Day09 (day09) import Solutions.Day11 (day11) import Solutions.Day12 (day12) +import Solutions.Day13 (day13) main :: IO () main = do @@ -23,4 +24,5 @@ main = do -- day08 -- day09 -- day11 - day12 + -- day12 + day13 diff --git a/src/Solutions/Day13.hs b/src/Solutions/Day13.hs new file mode 100644 index 0000000..aebeea3 --- /dev/null +++ b/src/Solutions/Day13.hs @@ -0,0 +1,92 @@ +module Solutions.Day13 (day13, parser, part1, part2) where + +import Data.Foldable (toList) +import Data.List (partition, findIndices, find) +import Data.List.Split (splitOn) +import qualified Data.Sequence as S +import Lib.AOC (runSolution) +import Lib.Matrix (Matrix) +import qualified Lib.Matrix as Mat +import Data.Bifunctor (Bifunctor(bimap)) + +type Input = [[String]] + +data ReflectionType = Vertical | Horizontal deriving (Show, Eq) + +isVertical :: ReflectionType -> Bool +isVertical Vertical = True +isVertical _ = False + +parser :: String -> Input +parser = map lines . splitOn "\n\n" + +pairs :: (Eq a) => [a] -> [(a, a)] +pairs [] = [] +pairs [_] = [] +pairs (x1 : x2 : xs) = (x1, x2) : pairs (x2 : xs) + +isReflectionIdx :: [String] -> Int -> Bool +isReflectionIdx list idx | idx >= length list - 1 = False +isReflectionIdx list idx = + let (l, r) = splitAt (idx + 1) list + minSublength = min (length l) (length r) + in take minSublength (reverse l) == take minSublength r + +findReflectionIndex :: Matrix Char -> (Int, ReflectionType) +findReflectionIndex mat = case find (isReflectionIdx $ Mat.toList mat) potentialHorizIdxs of + Just idx -> (idx, Horizontal) + Nothing -> + case find (isReflectionIdx $ toList cols) potentialVertIdxs of + Just idx -> (idx, Vertical) + Nothing -> error "could not find reflection" + where + cols = map toList $ Mat.getCols mat + colPairs = pairs cols + rowPairs = pairs (toList mat) + potentialHorizIdxs = findIndices (uncurry (==)) rowPairs + potentialVertIdxs = findIndices (uncurry (==)) colPairs + +part1 :: Input -> Int +part1 input = + (\(colIdxs, rowIdxs) -> sum colIdxs + (100 * sum rowIdxs)) + $ bimap (map ((+ 1) . fst)) (map ((+ 1) . fst)) + $ partition (isVertical . snd) + $ map findReflectionIndex matrices + where + matrices = map (S.fromList . map S.fromList) input + +strDiff :: String -> String -> String +strDiff [] [] = "" +strDiff [] _ = "" +strDiff _ [] = "" +strDiff (c1:c1s) (c2:c2s) = if c1 == c2 then strDiff c1s c2s else c1 : strDiff c1s c2s + +isSmudgeIdx :: [String] -> Int -> Bool +isSmudgeIdx list idx | idx >= length list - 1 = False +isSmudgeIdx list idx = + let (l, r) = splitAt (idx + 1) list + diff = concatMap (uncurry strDiff) (zip (reverse l) r) + in length diff == 1 + +findSmudgeIndex :: Matrix Char -> (Int, ReflectionType) +findSmudgeIndex mat = case find (isSmudgeIdx $ Mat.toList mat) [0 .. Mat.height mat - 1] of + Just idx -> (idx, Horizontal) + Nothing -> + case find (isSmudgeIdx $ toList cols) [0 .. Mat.width mat - 1] of + Just idx -> (idx, Vertical) + Nothing -> error "could not find smudge" + where + cols = map toList $ Mat.getCols mat + + +part2 :: Input -> Int +part2 input = + (\(colIdxs, rowIdxs) -> sum colIdxs + (100 * sum rowIdxs)) + $ bimap (map ((+ 1) . fst)) (map ((+ 1) . fst)) + $ partition (isVertical . snd) + $ map findSmudgeIndex matrices + where + matrices = map (S.fromList . map S.fromList) input + +day13 :: IO () +day13 = runSolution "13" parser part1 part2 diff --git a/test/Day13.hs b/test/Day13.hs new file mode 100644 index 0000000..f9645f3 --- /dev/null +++ b/test/Day13.hs @@ -0,0 +1,67 @@ +{-# OPTIONS_GHC -Wno-missing-export-lists #-} + +module Day13 where + +import Solutions.Day13 (parser, part1, part2) +import Test.Tasty +import Test.Tasty.HUnit + +exampleInput :: String +exampleInput = "#.##..##.\n\ + \..#.##.#.\n\ + \##......#\n\ + \##......#\n\ + \..#.##.#.\n\ + \..##..##.\n\ + \#.#.##.#.\n\ + \\n\ + \#...##..#\n\ + \#....#..#\n\ + \..##..###\n\ + \#####.##.\n\ + \#####.##.\n\ + \..##..###\n\ + \#....#..#" + +test_day13 :: TestTree +test_day13 = + testGroup + "Day13" + [ + testCase "parser" $ do + parser exampleInput @?= + [ + [ + "#.##..##." + , "..#.##.#." + , "##......#" + , "##......#" + , "..#.##.#." + , "..##..##." + , "#.#.##.#." + ], + [ + "#...##..#" + , "#....#..#" + , "..##..###" + , "#####.##." + , "#####.##." + , "..##..###" + , "#....#..#" + ] + ] + + , testCase "part 1 - example input" $ do + part1 (parser exampleInput) @?= 405 + + , testCase "part 1" $ do + input <- parser <$> readFile "data/day13.txt" + part1 input @?= 27664 + + , testCase "part 2 - example input" $ do + part2 (parser exampleInput) @?= 400 + + , testCase "part 2" $ do + input <- parser <$> readFile "data/day13.txt" + part2 input @?= 33991 + ]