diff --git a/13/input.txt b/13/input.txt new file mode 100644 index 0000000..b3ff455 --- /dev/null +++ b/13/input.txt @@ -0,0 +1,1339 @@ +#..#..##.#. +#..#..##.#. +......#..## +##########. +..####.#.## +#.#.##....# +..#.##....# +..####.#.## +##########. +......#..## +#..#..##.#. + +#..#####.##.#.#.. +####..#.#....#### +..##.......##.#.# +#..######.#.##### +#..######.#.##### +..##.......##.#.# +####..#.#....#### +#..#####.##.#.#.. +#..#...###..#..#. +#..#.###.#..#...# +###.##..#.####.#. +.##.###.##...#.## +##.###.#..###..## +#..###.#..###..## +.##.###.##...#.## +###.##..#.####.#. +#..#.###.#..#...# + +##.##.# +..#...# +###.##. +###.##. +..#...# +##.##.# +..#..#. +##.#.## +.####.# + +#..#...#..#.. +####.##....## +.##.####.#### +......######. +.##..#......# +.##.##..##..# +#..###.#..#.# +.....#......# +#####..#..#.. +.##..#.#..#.# +#..##..#..#.. +#..#..##..##. +#####..#..#.. + +.##.....##... +.##.....##.#. +.#.###...###. +#..##.#.##### +#####.##..... +#####.##..... +#..##.#.##### +.#.###...###. +.##.....##.#. + +#.#..#### +.##..#..# +.##..#..# +#.#..#### +###...... +#.#.#.### +..##.##.# +.#.#.##.. +.#.#.##.. +..##.##.# +#.#.#.#.# + +####..##..# +..##..#.##. +#####...##. +#..##.#.#.. +####..##### +.####..#### +#.#.##.#..# +####...#### +##.#.#..... +......#.... +##...###..# +..#.#..#..# +..#.#..#..# +##...###..# +......#.... + +.....#. +#..#..# +..#.#.# +####... +#..#... +.##..#. +#..###. +.##.... +.##.... + +...######..##..## +.#...#....####... +###..#..##....##. +#.....#.###..###. +#.....#.###..###. +###..#..##....##. +.#.#.#....####... +...######..##..## +.##..#.....##.... +####.#.########## +...####.#.#..#.#. +.##.#.#..#....#.. +.###.#.....##.... +..#...#.##....##. +.##...#..##..##.. + +.#..#.#.. +.......## +######.#. +#.##.###. +#....#.## +#....###. +######.#. +.####.... +.####.#.. +######.#. +#....###. + +.####....####.#.# +.#..#.##.#..#.### +#.##.#..#.##.#..# +..##......##..### +##.########.###.. +.####....####.#.# +#....#..#....#.## +................# +#....####....##.# +#....####....#..# +..............#.# +..##..##..##..... +......##.......#. + +.#..#.. +.#..#.. +##..... +..#.#.# +##.##.. +.###.## +...##.# +#.#.### +#.#.### +#..##.# +.###.## +##.##.. +..#.#.# +##..... +.#..#.. + +..#....#..# +##.##..#..# +###...##..# +.....#..##. +##...#..##. +##.####.... +#####...... +##.###.#..# +##......... +..###.##.## +###.#.#.... +###...##### +..##.#..... + +..####### +.#####... +###..##.. +#..##.#.. +.##...... +#..##.#.# +###.###.. +.#.#.#... +.#.#.#... +###.###.. +#..##.#.# +.##...... +#..##.#.. +###..##.. +.#####... +..####### +.######## + +######.##..#.## +#####.###.#..#. +..#.######.##.# +.#.#.##...##... +###..##.##....# +###..##...##... +..#....#..#.... +##..##.#.#..##. +##..##.#.#..##. +..#....#..#.... +###..##...##... + +###.#.##..#.##.## +###.#.##..#.##.## +.##..##.####.###. +##...##.##.....## +..#.##......#.#.. +.#####..#.###..## +.#..##..#.#..#.## +.##.#.#....#..##. +.###.#.#.###..### +#.#.#.###.#.#.##. +#.#.#.###.#.#.#.. + +#..#.########.# +.#.#####....... +#...#...#.#.... +.####....##..#. +.###.....##..#. +#...#...#.#.... +.#.#####....... +#..#.########.# +#..#.########.# + +#.#....#### +...#####..# +..######### +.#.#.#.#..# +......#.##. +.##.##..##. +###.##..##. + +##..##...###.#. +##..##...###.#. +....#####.##.#. +##.#####...#.## +#..#..#.#.##### +..#...#.#.##### +##.#..#.....### +#..#...######## +.#..##..#####.# +##..#....#.#... +##..#....#.#..# + +.########.##..#.. +.###..###.####... +.########..##.#.. +.###..###.#.#..## +####..####....... +..#.##.#...####.. +###....###....#.. +##.#..#.####...## +.#......#...#..## +.###..###.#.###.. +##.#.##.##.####.. +...#..#....#..#.. +.#......#.#.##.## + +#.#...##. +#.#...##. +##..###.# +.###.#.#. +##....### +#.....#.. +.######.. +###.##.#. +##....### +.##.####. +.......## +.......## +.##.####. +##....### +###..#.#. +.######.. +#.....#.. + +..##.#.#.##..#.#. +##.#.#.#.....#.#. +#..####.#.###..#. +.#.#.#...##.##### +.#.#.#..###.##### +#..####.#.###..#. +##.#.#.#.....#.#. +..##.#.#.##..#.#. +##.#.##.########. +....#.#..#....#.. +....#.#..#....#.. + +#..##..#.##.# +.#....#.####. +###..####..## +.#....##....# +.........##.. +..####...##.. +..#..#..#..#. +...##....##.. +#......#....# +...##........ +...##....##.. + +.#.....#..#..## +#..##..####..## +##.##.#...##.## +.##.###.#..##.# +.##.###.#..##.# +##.##.#...##.## +#..##..####..## +.#.....#..#..## +#.#.##.#.#.#.## +..#......#.###. +##..#..##.####. +.#..#...###..#. +.#......###..#. + +..##...####.#.. +#########..#### +.####.#.###.### +.####..##.#.#.. +######.###..### +.#####.###..### +#.##.#.#..##... +.#..#.#.#.#..## +.......#...##.. +.#..#.#...#.... +##..##.##.##### + +..##.## +....#.. +....#.. +..##.## +..#..## +##...#. +..###.# +###.##. +.####.# + +.#..#..###..#.. +..##.##.#.....# +......#.###.#.# +........#.#.#.. +#######....#... +######..#...##. +#.##.#...#.##.# +.####.#..##..#. +.####......#### +#.##.##....###. +......###.####. +......###.####. +#.##.##....###. + +..#..#.#..###..## +#####.#..######.. +..#....#.#######. +#.#.####...###### +..###..#.####..#. +##.######..##.#.. +##..####...#..#.. +..#.#.##..##.#..# +####...####.....# +####.##...##..#.. +#####.#..###....# +..##.#.####..#..# +..##.#.####..#..# + +###.##.#..#.##.## +##....#.##.#....# +..##..#.##.#..##. +##..#...#....#..# +...###.####.###.. +....#.#....#.#... +###.###.##.###.## + +..##........# +.####..##..## +##..###..###. +.#..#..#...#. +#....##..##.. +#....#....#.. +######.##.### + +..#...###..## +.##..##....#. +.#.#.#####... +.#.#.#####... +.##..##....#. +..#...###.### +..#....#.#### +#...#..##.#.. +#...#..##.#.. +..#....#.#### +..#...###.### +.##..##....#. +.#.#.#####... + +#.##### +...#..# +####..# +.#.#..# +#..#### +#.##### +#...... +#.##..# +#.##..# +#...... +#..#### + +#####.####.#####. +....#......#...#. +.#..###..###..#.# +.##.#..##..#.##.. +##..#.#..#.#..##. +################. +..###.#..#.###... +###..#.##.#..###. +###..#.##.#..###. + +#......#.##.# +..#..#..####. +.....##..##.. +....####.#..# +.####.#..##.. +#.#.###...... +.##....###### +#####.##....# +...####..##.. +...####..##.. +#####.##....# +.##....###### +#.#.###...... + +..#..#.## +.##.#..#. +.##..#### +###...#.# +.##.##.## +.#.#..#.. +##..##.## +##..##.#. +#.###.... +#.##.#.#. +.###..##. +.#.#.##.# +...#.##.# +...#.##.# +.#.#.##.# + +.........#.##.# +...........##.. +.#.##.#....###. +.#....#.#.#..## +#.####.#..####. +...##.....##... +...##....#..#.. +#.#..#.#.###.#. +...##....##...# +.#.##.#..##.### +.#.##.#..####.. +.##..##......## +.##..##......## +.#.##.#..####.. +.#.##.#..##.### +...##.....#...# +#.#..#.#.###.#. + +..#..#.##.##. +....####.#### +..#..#.#..... +####......##. +####.#.#...#. +##..#........ +..##.#.#.#..# + +#..##.... +.#.##.... +##....##. +#.#.#.##. +.####.##. +###..#..# +#.#.#.##. +.#.#.#### +..#.#.##. +.##..#### +#####.##. +.#.#.#### +.#...#### +.#.#.#### +.#.#.#### +#####.##. +.##..#### + +#.###.##.## +#.###.##.## +..#.#....#. +####..##..# +......##..# +##......... +####.#..#.# +.###..##..# +#.......... + +#..#...#...##.#.# +#..#.#.#....#.#.# +.#.#####...#..#.# +.#.##...#.#.##.## +.#.##...#.#.##.## +.#.#####...#..#.# +#..#.#.#....#.#.# +#.##...#...##.#.# +..#..###.#....##. +#..#.#..#.##....# +..###.##..#####.# +..###....###.#### +..###....###.#### + +.##.###..#.#... +####.#.##..###. +...##...#.###.# +####..###.###.. +....#..#.#.#..# +#..#.#.##...... +#..#.#.##...... + +...##########.... +#.#..######..#.## +#...#..##..#...## +##...######...### +##...#.##.#...### +#####.#..#.###### +..#.##....##.#... +..###.####.###... +.#.#.######.#.#.. +..#####..#####... +######.##.####### +#...##....##...## +.#..########..#.. +#...########...## +#............#.## +#.###.#..#.###.## +.##.##.##.##.##.. + +.#....#.....#.# +###..###.#...#. +###..###.#...#. +.#....#.....#.# +.#.##.#.###..## +..#..#....##### +#.#..#.#...#.#. +##########.###. +.#....#.#.##### +.#.##.#.#...##. +###..###..#..#. +.#....#.##.##.# +##....##.....#. +...##.....####. +##....#....#### + +.#..#.##...#. +##..##...#.#. +##..##...#.#. +.#..#.##...#. +.####..##...# +###.###...##. +##..####...#. +#######....## +######.#.###. +.......##..## +######.###.## +..##...###... +#....#.#..### +######.#...#. +#.##.#####... + +#..#..#...#.# +.##.####.#.#. +.###.##.##### +...#.####.### +.....#####.#. +.....#####.#. +..##.####.### +#####..##.### +#####..##.### +..##.####.### +.....#####.#. +.....#####.#. +...#.####.### + +..#.### +####.## +...#### +.##.... +#..##.. +####... +.###... + +..##...#### +#...##.#... +#.#.#.##... +#.#.#.##... +#...##.#... +..##...###. +##..#..#... +##..###.... +..#.....### +....###..## +#.#..###.#. +.#.#..##.## +.#.#..##.## + +##.##.### +...#####. +###.#...# +##.###.## +..##..##. +...#.###. +.....#### +..#.##... +.##.#.... +....###.. +....#...# +##...#..# +##...#..# + +#####...# +#####.### +####..##. +#..#.##.# +.##.##.#. +.##.##.#. +#..#.##.# +####..##. +#####.### +#####...# +####.#.## +.....#... +#####..#. +#..#..#.. +.##..#### +##.#.#... +#..#..#.. + +##.#... +##...#. +##..##. +###.#.# +....#.# +..#.### +..#.### +....#.# +###.#.# +##..##. +##...#. +##.#..# +...#... + +#####..###.#.###. +#####..###.#.###. +.#..#.##..#.#.... +.#.#....####..... +...##..###.####.# +.#####..#...##..# +.#####..#...##..# +...##.####.####.# +.#.#....####..... +.#..#.##..#.#.... +#####..###.#.###. + +..####.###.##.# +.#...##...#.##. +#...###....#... +#...#..#.#...## +#..##..#.#...## +#...###....#... +.#...##...#.##. +..####.###.##.# +###.###...##### +#.##.#..#####.# +#.##.#..#####.# + +..####.#..###.# +..####.#..###.# +#..##.###...### +.#...#...##...# +..#..#####..##. +..#..####...##. +.#...#...##...# +#..##.###...### +..####.#..###.# + +#.######.##..##.# +#...##...##..##.. +##.####.########. +#.##..##.##..##.# +#..####..######.. +####..####....### +###....####..#### +.##....##..##..## +.##.##.##..##..## +###.##.####..#### +.#......#.#..#.#. +...#..#...#..#... +#.##..##.#.##...# +.########......## +#........#....#.. +#..#..#..#....#.. +##......##.##.##. + +...........#.#.#. +.#..#.#..##.#.... +.#..#.#..##.#.... +...........#.#.#. +.#..#....#.##...# +..##.#.#..##.#... +.####...#####...# +#....###.#####.#. +######.###..##.#. +.####.##.#..#.... +.#..#..##.....#.# +..##..#..####.##. +######.#...###... +#....##.#..#...#. +..##.....#.###... + +##..#.# +#...#.# +#..###. +#...##. +.###.#. +##.#.## +...##.# +.#.##.. +.#.##.. +...##.# +##.#.## +.###.#. +#...##. +#..###. +#...#.# + +#.#..#. +##.##.# +.#....# +#.####. +#.####. +.#....# +##.##.# +#.#..#. +.##..#. +#..##.. +##....# +.#....# +#...... + +.####.#..## +.#..#..##.. +######.#### +#.##.####.. +#......##.. +.####..#... +#....###### +..##..#.... +##..####### +#....#.#... +.####.##### +######.##.. +######.#### +..##...#### +.####...... +######.#.## +..##....### + +##.#.## +##..#.# +....#.# +##.#.## +##.#.## +....#.# +###.#.# +##.#.## +##.#### +....#.. +##.#... +..##### +##.#.## + +##.#.###. +##...###. +##..#.### +#.#...#.# +.#....### +.#.####.. +...#.#### +.#...#### +.#..##.#. +#..#...## +.##...#.# +#.##..#.. +.#...#... +....###.. +....###.. +.#...#... +#.##..#.. + +.#....#.. +##....#.. +#...#.#.. +#..#....# +.####.... +.#.###.## +.##.###.# +.##.###.# +.#.###.## + +###..#....# +...######## +###.#...... +##..##....# +####.###### +##..#.#..#. +..###.#..#. +..#.#.####. +....#.#..#. +.......##.. +#####.####. +##.#######. +###.###..## + +#..#...########.. +#######...##...## +.##...#........#. +....###..####..## +.##.............. +#..##..########.. +....##..#....#..# +.##.#..##....##.. +#..#.###..##..### +....##....##....# +....#...##..##..# + +.#.#....#.#.##.#. +..###..###..##..# +##..#..#..##..##. +....####......... +.#.#.##.#.#....#. +#.########.#..#.# +.##......##....## +..########......# +#..#.##.#..####.. +#..#.##.#..#..#.. +..#.#..#.#..##..# +....####......#.. +##.######.######. +#.#.####.#.####.# +#..######..#..#.. + +######.###..####. +######.....##.#.. +####..##.##....## +####...####.#.##. +#####.#.##.###.#. +....##..##.##..#. +......######.###. +####..#.#.###...# +.##.#.#.####.##.# + +#..##..###..### +..#..#...#..#.. +.........####.. +#......##.##.## +..####..#....#. +...##.....##... +#.####.##.##.## +..........##... +...##...##..##. +.#....#...##... +.#....#.#....#. +.#....#......#. +##....##..##..# + +....#..#... +..##....##. +..########. +##.#.##.#.# +...##..##.. +##...##...# +..########. +..#..##..#. +###.......# +##.#.##.#.# +##.######.# + +##..##.#......... +##....#..###..### +..###..##...##... +....#..#......... +.#.####..#......# +##...#....##..##. +##.#..#..#.#..#.# +....#..#.#.####.# +####.#.##.##..##. + +#....####....## +#.##.#..##..##. +#....#.##....## +.####..#..##..# +#######.##..##. +.####..######.# +######.##.##.## +##..##.##....## +.#..#.#..####.. +.####.#..#..#.. +########..##..# +.#..#..#..##..# +#....#....##... +#.##.#.#.#..#.# +######..#....#. +.........#..#.. +#....#.###..### + +##..######..##.## +#...#..#.#...#..# +###.######.#####. +..##......##..### +#.#........#.#.#. +..###.##.###..#.. +..###.##.###..#.. + +#.#..#.#.#. +###..###..# +..####...## +#..##..#### +.#....#.#.# +#.####.###. +.##..##...# +##....####. +########..# +##....##.## +##.....#.#. +#..##..##.. +..####....# +###..###... +#..##..#### +##....###.. +##....###.. + +####..#..#..# +.#####....### +#.##..#..#..# +#...##....##. +.############ +#..#..####..# +#######..#### +.######..#### +.######..#### +##.....##.... +.##.##....##. +#.#.##.##.##. +#.#....##.... +..#.......... +.##.##.##.##. + +.####.#.##. +##..#####.. +...#...#### +..##..#.... +......##### +#....####.# +.#..#..#..# +..##....### +.#..#...### +..##...##.. +.####...#.# +.####...#.# +..##...##.. +.#..#...### +..##....### +.#..#..#..# +#....####.# + +..#..#...######## +#.####.###......# +.........#.#..#.# +.######..###..### +##.##.##......... +##.##.##.#.#..#.# +#.####.#####..### +.######.###....## +#.#..#.#.#.#..#.# +........###....## +.........#.#..#.. +########...####.. +#.#..#.##.#....#. + +##...###.#..#.# +..###.##.##.### +...#..#.####..# +..####.##.#.... +..#.##...#..#.. +###...#.#.....# +###...#.#.....# +..#.##...#..#.. +..####.##.#.... +......#.####..# +..###.##.##.### + +..####...#### +#.#..#.##.##. +.#....#.##### +...#....##..# +..####...#..# +..#..#..#.##. +##########..# +.#.##.#.#.##. +##....###.##. +.######.#.##. +#......#..... + +##.##......#. +##..#.##..##. +##..#.##..##. +##.##......#. +####.###....# +...#...#.#... +...##...#.#.. +#.##..#.#...# +.........#### +...#.#####.## +########....# +###..####.#.# +..#.#.###.... + +#.#.##.###. +#.######.## +..######..# +#........## +##########. +#..#..#..#. +#..#..#..#. + +##..###.#.# +......##### +.#..#.##... +#...###.... +######.#### +##..##..### +.####.#.### +#.##.#.#.#. +#....#..### +.#..#.....# +.#..#.....# +#....#..### +#.##.#.#.#. +.####.#.### +##..##..### + +#..#.###... +###.....### +.....###.## +......##... +.##.#.###.. +.##.#.###.. +......##... +.....###.## +###.....### +#..#.###... +....#..#### +#..##..#... +......####. + +#.#....## +#.#....## +###.#.#.. +###..#### +...##..#. +#.###...# +####...## +####..### +#.###...# +...##..#. +###..#### + +....#.#..##..#.#. +..##.####..####.. +#..###.#....#.### +###.###..##..###. +#.#.....####..... +.....####..####.. +.#............... +##...###....###.. +..#.#.#......#.#. +..#.#.#......#.#. +##...###....###.. +.#............... +.....####..####.. +#.#.....####..... +###.###..##..###. + +#.##.#..#...# +#.##.#..#.#.# +.#..#...#..## +#.##.#....#.# +#.##.####.#.. +......####### +##..##.##.... + +####... +##..#.. +..#.#.. +..#.#.. +#####.# +...#... +.##..#. +##.#.## +####... +##..#.. +##.#.## +###...# +##.##.. +..#.... +...##.# +..#.#.# +..#.#.# + +.#.###. +...#.#. +....### +##....# +##....# +....### +...#.#. +.#..##. +###.##. +###.### +###.### + +.##...#.. +.##...#.# +.###.##.# +..####.#. +#..#.#.#. +..#...#.. +##...#..# +#.##.#.## +...##.### +#..#.#.## +#..#.#.## +...##.### +#.##.#.## +##...#..# +..#...#.. + +.#.##..##.#..#. +.#.######.#..#. +........#...... +..###..###....# +...#.##.#...... +..###..###....# +#.##....##.##.# +##........####. +##.#.##.#.####. + +.###### +..#.... +..#.##. +###.... +#..#... +####..# +#.#.... +#.#.##. +##..... +##.#### +###.... +###.... +.#..##. +#..#### +#..#### +.#..##. +###.... + +##..#..#....# +##..#..##.### +##..#..##.### +##..#..#....# +###.#......#. +....#..#.###. +###..###.#... +####.#.#####. +..##.######.# +##.#..##.#.## +##..#####..#. +##.#.###..#.. +...#####...## +.#..#..##.#.. +..#......#.## +..#.#####.... +##...#.#.#### + +####..##.#.##.#.# +##.##.########### +...##....##..##.. +####.###..####..# +...#.####.#..#.## +.......#.#.##.#.# +.....#.#.#..#.#.# + +..#...##... +.###.#..#.# +.#.##.##.## +.#..######. +.##.#.##.#. +.##.######. +#...#....#. +..######### +.#..##..##. +.#.#.####.# +#..######## +##.#......# +.#..######. +##...#..#.. +#####....## +#.###....## +##...#..#.. + +.##.##.##..#..... +#.##.#########.## +##..##..####.#.## +#.####...#.####.. +......#.#.##..#.. +#....##..#..#..## +....#.#....#.#### +....#.#....#.#### +#....##..#..#..## +......#.#.##..#.. +#.####...#.###... +##..##..####.#.## +#.##.#########.## +.##.##.##..#..... +....#.##..##.#... + +#.#.##.#.#... +..#.##.#....# +####..####.#. +..#.##.#...#. +#.##..##.#... +#..####..#.## +.#.#..#.#.#.# +#.######.#..# +.#......#.... +..#.##.#...#. +.#.#..#.#.#.. +.#..##..#.#.# +.###..###.### +..........##. +.########.#.. +.#..##..#.#.# +.#..##..#.### + +.....#..#.#....## +.....#..#.#.#..## +###....#........# +###....#.#.#.#### +...#.#..#.##.###. +..#.####...#....# +######...##.#.#.. +..##.####.####..# +..#######.##.##.. +##...#.#####..#.# +..#..#.##.#.#.#.. +...#..#.....#.### +##..#.#.##..####. +##....#...#...#.. +...#.###...###..# +##.##.#.##.###.#. +..#.#....#.#.##.# + +..#..#...#..#..#. +.#.....#..##....# +..###.#.#....##.. +#..#..#..###.##.# +###...##.###.##.# +#..##..###.#....# +.###.######.#..#. +###.#..#....####. +...#..#..#..#..#. +##.#..###.#...... +.##.#..###.###### +....#.##..##.##.# +..###...####....# +..#...#...##.##.# +..#...#.#.##.##.# + +....#.#.. +......#.. +.##.#.... +....#.### +####..##. +.#.#.##.# +.##.##... +.#.#.#.## +.#.#.#.## + +#....#.#. +..##..#.. +..##..#.# +#....#.#. +##..###.. +##..###.. +#....#### + +#######.#.. +#..#.#..##. +#..#.#.#..# +.##.....##. +.##...##... +#..#..##... +.....##.#.# +.....##..#. +.....##..#. +.....##.### +#..#..##... +.##...##... +.##.....##. + +###..##..###. +..#.####.#... +..#.####.#..# +###..##..###. +##........### +#.##.##.##.## +#.########.## + +###..## +...##.. +..#..#. +##.##.# +###..## +..####. +#####.# diff --git a/13/part1.pl b/13/part1.pl new file mode 100644 index 0000000..2c436c9 --- /dev/null +++ b/13/part1.pl @@ -0,0 +1,44 @@ +:- use_module(library(pio)). +:- initialization(main, main). + +main([FileName|_]) :- + input(FileName, Blocks), + convlist(vert_reflect, Blocks, Vs), + convlist(horz_reflect, Blocks, Hs), + sum_list(Vs, V), + sum_list(Hs, H), + Answer is 100*V + H, + writef('Answer=%t\n', [Answer]). + +vert_reflect(Block, N) :- reflect(Block, N). +horz_reflect(Block, N) :- maplist({N}/[Row]>>(reflect(Row, N)), Block). + +reflect(Items, N) :- perfect_reflect(Items, N). +reflect(Items, N) :- + append(SubItems, [_|_], Items), + perfect_reflect(SubItems, N). +reflect(Items, N) :- + append(Prefix, SubItems, Items), + perfect_reflect(SubItems, SubN), + length(Prefix, Len), N is SubN + Len. + +% reflect right down the middle. N is half list length. +perfect_reflect([A, A], 1). +perfect_reflect(Items, N) :- + append([[Item], OtherItems, [Item]], Items), + perfect_reflect(OtherItems, N_sub1), + N is N_sub1 + 1. + +input(FileName, Blocks) :- phrase_from_file(blocks(Blocks), FileName). + +blocks([]) --> eos, !. +blocks([Block|Blocks]) --> block(Block), blocks(Blocks). + +block([Line]) --> line(Line), ("\n"; eos), !. +block([Line|Lines]) --> line(Line), block(Lines). + +line([]) --> ("\n"; eos), !. +line([#|Chars]) --> "#", line(Chars). +line([o|Chars]) --> ".", line(Chars). + +eos([], []). diff --git a/13/part2.pl b/13/part2.pl new file mode 100644 index 0000000..67d549f --- /dev/null +++ b/13/part2.pl @@ -0,0 +1,55 @@ +:- use_module(library(pio)). +:- use_module(library(clpfd)). +:- initialization(main, main). + +main([FileName|_]) :- + input(FileName, Blocks), + convlist(almost_reflect, Blocks, Vs), sum_list(Vs, V), + maplist(transpose, Blocks, TBlocks), + convlist(almost_reflect, TBlocks, Hs), sum_list(Hs, H), + Answer is 100*V + H, + writef('Answer=%t\n', [Answer]). + +almost_reflect(Items, N) :- almost_perfect_reflect(Items, N). +almost_reflect(Items, N) :- + append(SubItems, [_|_], Items), + almost_perfect_reflect(SubItems, N). +almost_reflect(Items, N) :- + append(Prefix, SubItems, Items), + almost_perfect_reflect(SubItems, SubN), + length(Prefix, Len), N is SubN + Len. + +almost_perfect_reflect([A, B], 1) :- diff(A, B, 1). +almost_perfect_reflect(Items, N) :- + append([[A], OtherItems, [A]], Items), + almost_perfect_reflect(OtherItems, N_sub1), + N is N_sub1 + 1. +almost_perfect_reflect(Items, N) :- + append([[A], OtherItems, [B]], Items), + diff(A, B, 1), + perfect_reflect(OtherItems, N_sub1), + N is N_sub1 + 1. + +% reflect right down the middle. N is half list length. +perfect_reflect([A, A], 1). +perfect_reflect(Items, N) :- + append([[Item], OtherItems, [Item]], Items), + perfect_reflect(OtherItems, N_sub1), + N is N_sub1 + 1. + +diff(L1, L2, N) :- + foldl([A, B, Prev, Curr]>>(Curr is Prev + abs(A - B)), L1, L2, 0, N). + +input(FileName, Blocks) :- phrase_from_file(blocks(Blocks), FileName). + +blocks([]) --> eos, !. +blocks([Block|Blocks]) --> block(Block), blocks(Blocks). + +block([Line]) --> line(Line), ("\n"; eos), !. +block([Line|Lines]) --> line(Line), block(Lines). + +line([]) --> ("\n"; eos), !. +line([1|Chars]) --> "#", line(Chars). +line([0|Chars]) --> ".", line(Chars). + +eos([], []). diff --git a/13/test.txt b/13/test.txt new file mode 100644 index 0000000..3b6b5cc --- /dev/null +++ b/13/test.txt @@ -0,0 +1,15 @@ +#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..#