diff --git a/2023/data/10/example1.txt b/2023/data/10/example1.txt new file mode 100644 index 0000000..2417530 --- /dev/null +++ b/2023/data/10/example1.txt @@ -0,0 +1,5 @@ +-L|F7 +7S-7| +L|7|| +-L-J| +L|-JF \ No newline at end of file diff --git a/2023/data/10/example2.txt b/2023/data/10/example2.txt new file mode 100644 index 0000000..1ddc980 --- /dev/null +++ b/2023/data/10/example2.txt @@ -0,0 +1,5 @@ +7-F7- +.FJ|7 +SJLL7 +|F--J +LJ.LJ \ No newline at end of file diff --git a/2023/data/10/example3.txt b/2023/data/10/example3.txt new file mode 100644 index 0000000..6933a28 --- /dev/null +++ b/2023/data/10/example3.txt @@ -0,0 +1,9 @@ +........... +.S-------7. +.|F-----7|. +.||.....||. +.||.....||. +.|L-7.F-J|. +.|..|.|..|. +.L--J.L--J. +........... \ No newline at end of file diff --git a/2023/data/10/example4.txt b/2023/data/10/example4.txt new file mode 100644 index 0000000..fbc0300 --- /dev/null +++ b/2023/data/10/example4.txt @@ -0,0 +1,10 @@ +FF7FSF7F7F7F7F7F---7 +L|LJ||||||||||||F--J +FL-7LJLJ||||||LJL-77 +F--JF--7||LJLJ7F7FJ- +L---JF-JLJ.||-FJLJJ7 +|F|F-JF---7F7-L7L|7| +|FFJF7L7F-JF7|JL---7 +7-L-JL7||F7|L7F-7F7| +L.L7LFJ|||||FJL7||LJ +L7JLJL-JLJLJL--JLJ.L \ No newline at end of file diff --git a/2023/data/10/input.txt b/2023/data/10/input.txt new file mode 100644 index 0000000..ee4ba57 --- /dev/null +++ b/2023/data/10/input.txt @@ -0,0 +1,140 @@ +7-LJ7.F-F77FF-77FJ-J-F7FF|777F7--..JJ.F.7.|.F-J7-J777F7FF77F|.|7L-7.F-|7F7FF-J7LF|.7--7J-F.F--7-L--77.|F-J77F7F|-F-F7-J-FFF---F--77|FF7--L7. +L7.F--J.L7J7|---FJ-|JLF77L7J7F|-J7|J.FLLJ.FFF7|F7L7-J7LF7.-|L-77-LL7J.|.L-7JFJ7-7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L7|F7FJL7L|F-J|FJ.F-JFJ||L-7FJ|FJ|F-JJ|LJLJL7|||||||FJL--7L--J|FFJL7FJLJL7FJ|FL--J|LJL-JLJF-JL-7|F7LL-.|J|7.LL-- +L|--JJJF-L7.|7-|JLL7LJLJL-JL-J|||L-7L7|L-7|L7FJF-JFJ|F7|L7||FJL-7FJF----J|||||LJ|F7F-JF---JFJF-JL-7F-JL7L-----JF7F7F--J7F7FJLJL-7F|JFL|.|7F. +||.FJFF|77|7JF7JLF7L---------7LJ|F-JFJL7FJ|FJ|FJF7|FJ|||FJ|||F--JL7|.F7F7||||L-7|||L7FJF7F7|FJF7F7||F7||F---7F7||||L-7F-JLJF----J--7|F-7|F|. +-J.-FF-F-LF7-|L7F|L---------7L-7LJF7L-7||FJ|FJL7|LJL7||||FJ|||F--7||FJ||||||L-7|||L7||FJ|||||FJ|||||||FJL7F7LJ||LJL--J|F7F7L--7F-7.L-|-L|-77 +.L7LLJ|.FL||LL7L7L-----7F7F7L--JF7|L7F||LJFJ|FFJL-7FJ|||||FJ|||F-J|LJ|||||||F7|||L7|||L7||LJ||FJ|||||||F7LJL-7|L--7F-7LJ||L--7LJFJ77.--7L7F7 +|.|F|F||77|L--JFJF7JF-7LJLJL----JLJFJFJL-7L7|FJF-7|L7|||||L7||||F7L7F-J|||LJ|LJ||FJ||L-J|L7FJ|L7|||||LJ|L----J|F--JL7|.FJL7F7L--JJ-LF|FFL-FL +F-F77-LJF-JF--7|FJ|FJFJF-----------J-L7F-JJ|||FJ||L7|||||L7||||||L7|L7FJLJF7L7FJ|L7|L--7|FJL7|7LJ|||L-7|F7F-7FJL7F--JL-JF-J|L----7.L-F-L.L7| +|F|JJ|7-L7FJ|FJLJFJL7L7L-7F7F----7F--7|L-7FJ||L-7|FJ||LJ|FJ||||||FJL7|L---J|-||FJFJ|F7FJ||F-J|F-7LJ|F-JLJLJFJL--J|F----7L7||F----J-|.|FFJ7L7 +L-L7-J7.LLJ7FJF-7L-7L7|F7LJLJF--7LJF7|L7FJ|FJL7FJLJ.|L7FJL7LJLJLJL7FJ|F7F--JFJ|L7|FJ||L7|||F7|L7L--JL7F----JF--77|L---7L7|FJL---7J-7--.|.--J +F|LJ.7||J7JFJFJFJF-JFJLJ||F-7L-7|F7|LJL|L7|L7FJL---7L-J|F-JF--7F7F|L7LJ|L7F7L7|FJ||FJL7|LJ||||FJF7F--JL--7F7|F-JFJF7F7L7|LJF7F--J-J.FFL-7.LJ +L7J.--777|7L-JLL7|F7L--7L7|FJF-JLJLJ7F7|FJL7|L7F7F7|F--JL--JF-J||FJFJF-JFJ|L-J|L-J||FFJL-7LJ||L7|LJJF7F77LJLJL--JFJLJL-JL--JLJJ.|7-F-|J.L|.L +LFF|-|LLL-F-----JLJL7F7L7|||FJF7F---7||LJJ-LJLLJLJLJL-7F-7F7L7FJLJFJ7|F7L7|F--J.F-JL7|F-7L-7||FJ|F--JLJL--7F7F--7L-----------7L-.LJF-|F7FJ-. +7.LLFJ.|.|L--------7LJ|FJLJ|L-JLJF--J|L--7F--7LF7F7F--JL7||L-JL--7L-7|||FJ||F---JF-7|||FJF-JLJL7|L--7F7F-7LJ|L-7L--7F7F7F7F7FJLLJLFL.LJ|7L7F +FJ.F|.7|-LJF7F7F7F7L-7LJF-7L----7L-7FJF--J|F7L-JLJ|L7F-7|||F-----JF-JLJ||FJ|L-7F7L7||||L7L--7F7LJF7|||LJ7L7.L-7L--7||LJLJLJLJF7J|.LJFJFJJ-|7 +LJ|-|-F7.LFJLJLJLJL--JF-JLL----7L--J|FJF77||L7F---JJLJL||LJ|F--7F7|JF7JLJL7|F-J||FJ||||FJF7FJ|L--JL-J|F---JF7FL--7||L----7F--J|.-7J77.7FF7LJ +J||.7-7JL.L7F--7F----7L7F--7F-7L----JL-JL-JL7LJF-7JF7LFJL7FJ|F-J||L-JL---7||L-7|||FJ|||L7||L7L7F7F-7FJL7F--JL7F7FLJ|F7F--J|F--J7L|JFL..FJ||. +F7-.JFJJL7JLJF7LJF7|FJFJL-7|L7L7F---7F--7F--JF7L7L7|L7L7FJ|FJ|F7|L7F7F-7FJLJF-J|LJL-J|L7LJ|FJ7LJLJ|LJF7LJF--7LJ|F-7LJ|L---JL------7L|-7-F7-| +|LFFJL7F7F---JL--JL-JFJ.F-J|.|FJL--7|L7FJL---JL7|FJL7|LLJFJL7LJLJFJ||L7LJ-F-JF7L7F7F-JFJF-JL--7F-----JL--JF7L-7LJFJF7L-------7F---J|F-|-J.|| +F7|J.LLJ-L---7F7F-7F-JF7L-7L-JL----JL-JL-7F7F-7LJL--JL7F-JF7L-7F-JFJL7|F-7L-7|L7LJ||F7L7L7F7F7||F------7F-JL-7L-7L-JL--7F---7|L7F7FF77L-J.|| +LF|-L-J7FF7LFJ|LJFJ|F-J|F-JF---7F7F----7FJ|||FJF7F--7FJL7FJ|F7||F7|F-JLJFJF-JL7|F-J|||FJ.LJ||||LJF----7LJF---JF7L----7FJ|F--JL-J|L-J|77J--FJ +-7J-J.LF7|L-JFJF7L-JL-7LJF7L-7FJ|||F---JL7|LJL7||L7JLJF-J|FJ|LJLJLJL--7FJFJF7FJ|L-7|||L7F--J|LJ7FJF-7FJF7L----J|F---7|L-JL7JF--7|F--JFJ-FJFJ +FJF-.FL|LJF-7|FJL-----JF-JL7FJL-JLJL---7-|L-7FJ|L7L--7L7FJL7L7F-------JL7|FJ|L7|F-JLJL7|L-7FJLF7L-JFJL-JL-----7LJF-7||F7F7|FJF7LJL-7L|L7J.J7 +J-L7L77L7FJ.||L7F------J.F7||F--------7L7|F-JL7L7L7F7L7LJF-JFJL--7F7F7F7|LJFJFJ||F7.F7LJFFJ||FJL7F7L---------7L7FJ.|||||||LJFJL-7F7|-|7LF7-F +|.F7J--FJ|F-J|7LJ-F-7F7F-JLJ|L-------7|FJ||F7JL-JJLJ|FJF7L7FJ|F--J||||||L-7L7|FJLJL7|L---JFJFJF7LJL----7FF---J7|L-7||LJLJ|F-JF-7LJLJ..FF-J7J +|FFJ|L-L-JL7FJ-JF7L7|||L--7FJF7F-----JLJ7LJ|L77F7F-7|L-JL7|L-7L7F7|LJ|||F-J7||L7F--JL----7|7L-JL----7F7L7L--7F7L-7||L---7||F7|FJ|||..FF.FFFF +-FF7F--JJLFJL7FFJL-JLJL---JL7||L----------7L7L7|LJFJL-7F7||F-J|LJ|L-7|||L--7|L7|L-7.FF--7LJF7F7F----J|L7|F7FJ|L-7|||F7F-JLJ|LJL-7JF--JF-LJJJ +LLJ-L-77|-L--JFJF---7F-7F---J||F----------J.|FJ|F-JF7FJ|LJ|L7JF7FJF7||||F7FJL7|L7FJ7-L-7|F7||||L-7F-7|||LJ|L-JF-JLJLJLJF-7FJF---JJJ7.||F|J-7 +F|.F7|LFL7LL|7L-JF7FJ|JLJF---J|L------7F7F7FJ||||F7||L7L7FJFJFJLJFJ|||||||L7FJ|-LJJL|F-JLJLJLJL-7LJFJ|FJF7L---JF7-F---7L7||FJFF777FF7JJ-F7F| +7LL.|LL|F7.77F---JLJFJF--JF7F7L-------J|LJ|L7L-JLJLJL7L-JL7L7L7F7L7||||LJ|FJL7|7LF7F7L------7F-7L-7L-JL-JL-7F--JL7L7F-JFJLJL--J|F--J|7|F|L.| +L-|J.FFFFF-77L------J|L---JLJL---------JF7L7|F7F7F7F7L7F7JL7|FJ|L7|LJLJJL||JJLJF-JLJ|F7F7F7FJL7L-7L7F-----7LJF7F-JFJL7|L7F-7F--J|F--J-J7-J-F +LJFJ|-7.||FJF7.F--7F7F--------------7F--JL-J||LJ|||||FJ||F7|||FJFJL-7JJL|LJJJFFL---7LJLJLJ|L--JF7|FJL----7L--JLJF-JF7L--JL7|L---JL-7L|LJ||LJ +|FJF|J|F-JL7|L7L-7|||L-------------7LJ|F----JL77|||||L7|LJ|||LJL|F-7L77L-7L|.F---7LL--7F-7|F7FFJLJL-7F---JF7F---JF-JL7F-7FJL-7F7F7FJ.-7LJ7.| +-F--L.FL--7|L7|F-JLJL7F7F---7F-7F--JF--JF7F7F7L7LJLJL7LJF-JLJJJF||JL7L7-.JFF-L--7|F--7||L|LJL7|F---7|L----JLJF7F7L--7LJF|L--7LJLJ||.|7L77F.7 +F77|L|JF--JL7||L----7|||L--7|L7|L---JF7FJ|||||FJF-7F7L-7L7J.LLFFJ|.F|FJ7FFFJFF77||L7FJLJFJF-7|LJ7F-J|F7F-----JLJ|F--JF-7L--7|F7F7LJF-7|.||F7 +LL-77F-L---7||||F-7FJLJL---JL7||F-7F-JLJFJ|||LJFJFJ||F7L-J7FF-7L-J.FJ||FJF|FFJL-JL-J|F7|L-JFJL--7L-7|||L-------7|L--7|FJF-7LJ|LJ|F-JFJ77L7-J +|-LF7.FF---JLJL7L7||F7F-7F7F7LJLJFJL-7F7L-J|L-7|FJF|LJL-----JFJ7L|F|FJ7J-FL7L7F----7LJL----JF7F7L--JLJL--------JL-7FJ|L-JFJF7|F-JL-7|F-77.|. +L7.JJFFJF---7F7L-JLJ|||7LJ|||F--7L--7LJ|F7.L--J||F7|F--7F-7F-J|L-7-LJFJ|J|LJLLJLF--JF7F7F7F-J||L7F7F7F7F---------7LJFJF-7L-JLJL7F7J|LJFJ7F|7 +F77-FJL-JLF7LJL----7|LJF--J|||F-JF-7|F7LJL-7F7FJLJLJL-7LJ7LJ7L|L|JJ.FJFFF77|JLJ7L7F7|||LJLJFFJL7||LJLJ|L7F-7F7F7FJF7|FJF|F7F7F7LJ|FJF7L--777 +|JFF|.F7-FJL-------JL-7L7F7|||L7FJFJ|||F---J|||F-----7L-7F--7.L|||-FFJLJF-J.FL7|-||||||F----JF7|LJF---JFJ|JLJLJLJ-|LJL-7LJLJLJ|F7LJFJL---J.| +LFJFL7|L-JF-7F7F7F-7F-J-LJ|||L7||.L7LJLJF7-FJLJL--7F7L-7LJF-J..|LF-L.F..|.|7LLJJ7LJ||||L7F7F7|LJF7L----JFL--7F-7F-JF---JF7F77FJ|L7FJJF----77 +||-LJ-L7F7L7LJLJLJFJL7F7F7LJL-JLJF7L7F7FJL7|F-----J||F7L--JJ7|7LF-7|7|.F-||L-7|FLF-J|LJFJ|LJ|L7FJL7F7F-7F---JL7LJF-JF7F7|LJL7L7|FJL--JF7F-J7 +L77.|-LLJL-JF7F--7L--J||||F---7F-JL-J||L7FJ|||F7FF-JLJL7.F7-JLJLL-7FJ-7|.LLJ-FL|-L--JF-JFJF7L-JL-7LJ||FJL-7F-7|F-JF-JLJLJF-7|FJ|L7F--7||L7J| +FLF.||LLL||FJLJF7|F-7.||||L--7|L-----JL-JL-JL-JL-JF7F-7L7|||L..||.L7JLFFJ7FFF-7|L|L.|L-7|FJ|F7F-7L-7LJL77FJL7LJL-7L-7F---JF||L-JFJL-7|||FJ7| +||J||7.L||FJF7FJ||L7L-JLJ|F7.||F7F7F7F-7F--------7||L7L7LJ|7-LF77FF|LFF7.J-J|JLJ.7FFFF7LJL7LJ||FJF-JF-7L-JF7|F---JF7|L----7LJF--JF7FJLJ|L7L- +--.LFF--FFL-JLJFJL7|F---7||L-J||LJLJLJ||L-------7|||FJFJF7L77.JJLJ7J|FJ|-J||.FJ.FF7FFJL7F-JF7LJL7L--J|L---JLJL----JLJF-7F-JF7L--7|LJJ-L|FJ.| +|F--J|.LF------JF7LJ|F7FJ|L--7|L7F-7F-7|F-------JLJLJ-L-J|FJ7.F|LLL77L7|.||J7|F7FJL7|F7LJF-JL7F7L-----------7F-----7FJ.LJF7|L7F7LJF7.FFLJ|-- +7F|F||.FL------7|L--J||L7L-7FJL7LJ-LJFJ|L-----7F7F7F77F7||L7J7|77FLF-FJL7-L---|||F7LJ|L7FJF--J||F----------7|L----7LJF7F7||L7LJL--JL7||FL-.| +LLLLF7J7F----7FJ|F---J|FL7FJL7FJF----J|L7F----J|LJ|||FJL7L-J7LJFJ|LF-JF-J.|7JF||LJL7FJJLJFJF-7|||F---7F7F--JL--7F7|F7|||LJL7L7F-7F-7L-7JLL-| +|JFF-JFFL---7|L-JL---7|F-J|F7||JL-----7FJL-----JF7LJLJF-J-J-|77F7J7L7FJJLLJ7.FJ|F77LJF--7L7L7LJLJL--7|||L----7FLJ|LJLJLJF--JFJL7|L7|F-JFF7F7 +7-L|..FF----JL-7F----JLJF7LJLJ|F7F7F7FJL7F----7FJL7F--JF7|||F7FJ|JFFJL-77|JLFL7|||F7|L-7L7L-J7F---7FJLJL--7F7L--7L7F-7F7|F77|F-J|FJ|L-7F77.| +|FLF.F-L----7F-JL7F-7F7FJL7F-7LJLJLJLJF7LJF7F7LJFFJL---JL7F7||L7L7FJF--J-F7JF7|LJLJ|F--JFJ-F--JF-7|L7F---7LJL---J7LJ-LJLJ||FJL-7LJL|F7LJL7.L +|J-|-7J.F---J||F7LJFJ|LJF7LJFJF7F--7F7||F7|LJL7F7L7F7F---J|||L7L7|L7|F77.|L-J|L-7F-JL--7|F7L---JFJL-J||F7L------7F--7LF7FJLJF--JF-7|||F7FJ7| +--7-.LFLL7F-7L-J|F7L-JF-JL--JFJ||F-J|||||||F-7LJL7||||F-7FJLJFJFJL-JLJ|F7|F--J7FJL-7F7FJ||L7F7F7L----JFJL-------JL7FJFJLJF--J|F-JFJ||||||-J- +LFF7F-7LLLJ.L7F7LJL---JF7F7F7L7LJL--JLJLJLJL7L-7FJLJ||L7LJF--J.L----7FJ||||F7F7L7F-J|||FJL7||||L------JF------7F7FJL-JF--JF7F7L7FJ|LJLJLJF|. +F7LLF7F7.LF--J||F------JLJ||L7|F-------7F7F7L-7||F7JLJ7L-7L-7F----7FJL7|||||||L-JL--JLJ|LFJLJ|L-7F--7F7|F-----J|LJF7F7L--7|||L-JL-7J.||.L7.7 +LL-LFJL--JL---JLJF7F------JL7LJL-----7JLJ||L7FJLJ|L7F7F--JF-JL---7|L-7LJLJLJ|L-7F-----7L7|F7FJF7LJF7LJLJL7F7F--JF-JLJL---J||L7F---JJF|-L.LFJ +FL7..J-|7|-LF----JLJF-------JF7.F7JF-JF-7|L7LJLF-JFJ||L7F-JF7-F--JL--JF-----JF7LJF-7F7|FJ|||L7|L7FJL--7F7LJLJF--JLF7F7F--7||.|L-7F777LJ.|JLJ +|FJJ7|7|-7-FJF-7F7F7|F7F7F7|FJ|FJL-JF-JFJL7L--7|F-JFJL-JL-7|L7L------7L---7F-J|F7L7||LJL7LJ|FJL7LJF7F7LJL----JF7F7|||||F-J|L-JF7LJL77LF77.F| +LJ|JFJJJF7|L-J|LJLJLJ|||LJL7|FJL---7|F-JF7L---J|L7|L7F----JL7|F7F7F-7L7F--JL-7LJL7||L--7L--JL7JL--JLJL--------JLJLJLJLJL-7|F--JL---JF7-J.FF| +7L--L.LL7LF--7F7FF---J||F-7LJL----7LJL--JL---7.|FJF-JL--7LF7||||||L7L-JL7F-7FL-7FJ||7F-JF--7FJF7|F7F---7F---7F7F-7F----7FJ|||F7LF7F7||77-FJ| +..L|-F7.|FL-7|||FJF7F7LJL7L7F7F--7L-7F7F7F---JFJL7|F7F--JFJ||LJ||L7L-7F-J|FJF7FJL7|L7L-7|F7LJJ|L-J|L--7|L--7||||FJL---7LJFJL-JL-JLJLJL-77LF| +FFJ|FLJ-L-F7||||L-JLJL7F7|FLJLJF7L-7|||||L---7L7FJLJ||F7|L7|L7FJ|FJF-JL7F||FJ|L-7LJFJF7|||||F7L-7FJF7FJL7F-JLJLJL-7F--JF7L7F7F7F7F7F-7FJ7-|| +L|.L|JLF-L|LJLJL---7F-J|LJF----JL--JLJLJL-7F7|J||F7FJ||L7L|L-J|FJL7L--7L-J||FJF7|F-JFJ||LJL7|L7FJL7||L-7LJ|F-7F7F7LJF--JL-J|LJLJLJ|L7LJL-7J7 +FFJL77-J|LL7F-7F--7LJF-JF7|F-7F----7|F7LF7LJLJFJLJ|L7LJFJFJF-7LJF-JF7|L7F-J|L-J|||F7L7LJF7FJL7LJF7LJ|F7L--7|FJ||||F7L7F----JF----7L-J-L..L7| +JJ7.||.LL.L|L7LJF7L--JF-J|LJ|LJF7F7L-JL7||F-7FJF7FJFJF-J.L-JFJF7|F7||F-JL-7L--7||LJL7L-7|LJF7L--JL7FJ||F7FJ||FJLJLJL-JL-7F7FJF7F7L---7.F7.|. +FLL7-J7J7|FL-J7FJL7F7FJF7L7F77FJLJL-7F7LJ||FJL7|LJJL7|F77|F7L7|LJ|||||F7F-JF--JLJF--JF-JL-7|L7LF7-|L7|LJLJ-|LJF7F------7LJLJFJLJL-7F7|7JFJ77 +F7L|F-7LFF-7F7FJF7LJ|L-JL7|||FJF---7LJL--J|L--JL7.F7|||L7FJL-J|F7|LJ|||LJF7|F---7|F7JL---7LJFJFJL7L7LJF----JF-J|L-----7|F7F-JF--7LLJLJ.FJFL- +||F-7-.FJL7LJ|L-JL7FJF7F7||||L-JLF7L-----7L7F--7|FJ||LJFJL7F--J||L-7||L7FJ|LJF7FJLJL7F7F7|F-JFJF-JFJF7|F----JF7L-7F---JLJ|L--JF7L-7.JF-|FL77 +L7F...FJF-JF7L----JL-JLJLJLJ|F7F-JL------JFJL-7LJL7||F-JF7|L--7|L7FJLJFJ|FJF7||L7F--J|LJLJL7.L7|F7L7|LJL---7FJL7FJL-----7|F-7FJ|F-JJ7|JL|FJ- +L||F----L7FJL----7F---7F-7F7LJLJF--7F7F-7|L7F-JF-7|LJ|F-J||F--JL7|L7F-J-|L7|LJL7|L--7|F7F--JF7||||7||F----7LJF-JL7F7F7F7||L7||F|L7JF-L-7-LJ. +-J||-F|-FJ|F-7.F7||F--J||LJL7F7FJF7LJ||FJF-J|F7|FJL7FJL-7LJL-7FFJ|FJ|F-7|FJL--7|L7F-JLJ|||F7|LJ||L7LJL---7L--JF7|LJ||LJ||L-J|L7L-JFJ||LLJF77 +.-F7L-F7L7|L7L-JLJ|L7F7L7F--J|LJ.||F-J|L7L-7||LJ|F7|L7F-JF---JFJFJL7||FJ|L7F7FJ|FJL7F7FJ|FJ||F-JL7|F-7F--JF---JL--7|L-7LJF7LL-JF7FJFF-777|J7 +.|J.|.|||LJ-L7F7F-JJLJL7||F--J|F-JLJF7|FJF7|||F-J|||FJL-7|F7F7L7|F7||||FL7|||L7||F-J||L7||FJ||JF7|||FJL---JF7F7F--JL-7L--JL----JL7LF-7LJ7JLF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FJ|L--JF---J|FJ|L---7F77||7.JFF|7.FJFJJ +FL||JL------7F-7FJ|F7|FJFJL7|FJF7F7|F-JL-7|L-7|F-JFJLF-J|||-LJF-J|F-J||L7L-7|FJ||F7F--JL7|J||LF---JF7|JL7F-7L---7||FJF7F7LJL7-LL.|F-||.L7J77 +FJFJF-------JL7|L7LJLJL7|F7LJL7||||||F7F7LJF7L7|F7L-7L7FJ|L7F-JF7|L-7||FJF7||L-J|||L---7||FJL7L---7||L77||-|F-7FJLJL7|||L-7FJFJFF7J-|L7.LLJ7 +..LFL-----7F7FJL7L--7F-JLJ|F--J|||||||LJL-7|L7|LJ|F7L7||J|FJL-7|||F-J||L7||LJF--J||F-7FJLJ|F7|F7LFJ|L7L7|L7|L7|L---7|||L-7|L777FL|JFL-FF-7.F +J7.F------J||||FJF7FJ|F7F7|L-7FJLJ||||F---JL7LJ7FJ||FJ|L7||F7FJ|||L7FJL7LJ|F7L--7||L7|L--7|||||L-JFJFJFJL7||FJ|F-7FJ||L7FJ|FJ7--|JF-7J|77L-7 +|L-L-7F7F--J|L7|FJ||FJ|||LJF-J|F--J|||L7F-7FJ|F7L-J||FJFJ|||LJFJ|||LJ.|L--J||LF7||L7||F--J|||||F7FJLL7|-FJ|LJ-|L7|||LJJLJ-LJJJ7.|-F.|-7LJFLL +F7LF-J|||F--JFJ||FJ|L7|||FFJF7|L7F-J||FJ|FJL--JL--7||L-JFJ||F7|FJL-7F7F----JL-JLJL7LJ|L--7||LJ||LJF7FJL7L7L--7|FJ|L-7-7FJJLLJFLJ.|.F|.LFFJFJ +F--L-7||||F-7|||||FJFJ|LJFJFJ||.LJF7LJL7|L7F7F7F7FJLJFF-JFJLJLJ|F7FJ||L--7F----7F7L-7|F7FJ||F-JL--J||F7L7|F--J|L7|F7L-7JJF77JL7L-7-JFJ|L-LJ7 +7|-J-LJLJLJFJL7LJLJ|L7L-7L7|FJ|F--JL---JL7|||||||L-7LFJF7L-77F-J|||F||JF-J|F--7|||F-J||||F||L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|-L|.L|7F--JF7FJFJ|FJF-7LJL--7||7-F-J|LLLLFJ.|.7-F-7LF-|77.|J7F|J.F|-7.F +7J.L|.L|FLLF-7|.L|-J7J.|F-FFJJ-F--7-L|FJ|L-7JJLJ.J7L-LFJJ-7JJF7LL|J7F-7F7-LLL---J|L7L7|L7L7|F--7FJ-F77FJ-FF7JF-J.|-.7|L|-|LJJ-L|77J-7FLJLJ.7 +|7|.F7.FJFLLFJ7-||F-77F|J-F|J.FL|-FJ.LJ-L--J.FJ|.FJ77||.LF|F77F-LL-FF-JLL7..|F---JFJFJL7L7|||7L|L7J.|7..-|J|L|7.F|J-J7|||.LLLJ7|.-7LL-.L-J-J +77J|JJ7L.|JJ..J-7-F7|7F|-L||7F-7|.F-F|J.LJL|-77F-F-JF|7..F|7|7|FL|LLL7LJFJFF-|F7F7L7|F7L7|||L7.|FJJJLLL|.|7|.L7F-LJFF|F--7.||L||FJ||J|L-FJ7L +L--LJJ|L-L7.FLJ..|JL7-LL-L.FLJJ.--JLL7J.L|-JJJF-JL7JF|-..JJF|--7.|LL-JLL|J---LJLJL-JLJL-JLJL-J-LJJ.LJ.|J-L7--JL-|L7-LL|-J-|-JJL-J.L|LF|LLJL. \ No newline at end of file diff --git a/2023/solvelib/10/day10.cpp b/2023/solvelib/10/day10.cpp new file mode 100644 index 0000000..ae1a3fc --- /dev/null +++ b/2023/solvelib/10/day10.cpp @@ -0,0 +1,278 @@ +#include "day10.hpp" + +#include "xmaslib/log/log.hpp" +#include "xmaslib/matrix/text_matrix.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using Int = std::int64_t; + +struct coords { + Int r; + Int c; +}; + +enum heading { + N, + E, + S, + W, +}; + +std::optional update_heading( + xmas::views::text_matrix map, coords pos, heading curr_heading) { + switch (map.at(pos)) { + case 'F': + switch (curr_heading) { + case N: + return E; + case W: + return S; + default: + return {}; + } + case 'L': + switch (curr_heading) { + case S: + return E; + case W: + return N; + default: + return {}; + } + case '|': + switch (curr_heading) { + case N: + return N; + case S: + return S; + default: + return {}; + } + case '-': + switch (curr_heading) { + case E: + return E; + case W: + return W; + default: + return {}; + } + case '7': + switch (curr_heading) { + case E: + return S; + case N: + return W; + default: + return {}; + } + case 'J': + switch (curr_heading) { + case S: + return W; + case E: + return N; + default: + return {}; + } + case '.': + return {}; + case 'S': + return {}; + } + + throw std::runtime_error(std::format("unexpected cell with value {}", map.at(pos))); +} + +std::optional take_step(xmas::views::text_matrix map, coords pos, heading heading) { + // clang-format off + switch (heading) { + case N: + if(pos.r == 0) return {}; + return {{pos.r - 1, pos.c}}; + case S: + if(std::size_t(pos.r+1) == map.nrows()) return {}; + return {{pos.r + 1, pos.c}}; + case W: + if(pos.c == 0) return {}; + return {{pos.r, pos.c - 1}}; + case E: + if(std::size_t(pos.c+1) == map.ncols()) return {}; + return {{pos.r, pos.c + 1}}; + } + // clang-format on + + throw std::runtime_error("Invalid value for heading"); +} + +heading find_start_direction(xmas::views::text_matrix map, coords start) { + std::vector possible_headings; + + // North side + if (start.r != 0) { + if (update_heading(map, {start.r - 1, start.c}, N).has_value()) { + possible_headings.push_back(N); + } + } + + // South side + if (std::size_t(start.r + 1) != map.nrows()) { + if (update_heading(map, {start.r + 1, start.c}, S).has_value()) { + possible_headings.push_back(S); + } + } + + // West side + if (start.c != 0) { + if (update_heading(map, {start.r, start.c - 1}, W).has_value()) { + possible_headings.push_back(W); + } + } + + // East side + if (std::size_t(start.c + 1) != map.ncols()) { + if (update_heading(map, {start.r, start.c + 1}, E).has_value()) { + possible_headings.push_back(E); + } + } + + switch (possible_headings.size()) { + case 0: + throw std::runtime_error("No possible heading from start point"); + case 1: + throw std::runtime_error("No closed loop: only one direction available from start"); + case 2: + break; + default: + xlog::warning("Multiple possible loops"); + } + + return possible_headings.front(); +} + +Int compute_perimeter(std::vector const& vertices) { + return std::transform_reduce(std::execution::par_unseq, vertices.cbegin(), vertices.cend() - 1, + vertices.cbegin() + 1, 0, std::plus{}, + [](coords const& u, coords const& v) { return std::abs(u.r - v.r) + std::abs(u.c - v.c); }); +} + +// Shoelace formula to find the (maybe non-integer) area, so 2*area is returned. +// https://en.wikipedia.org/wiki/Shoelace_formula#Shoelace_formula +Int compute_double_area(std::vector const& vertices) { + Int twice_area = std::transform_reduce(std::execution::par_unseq, vertices.begin(), + vertices.end() - 1, vertices.begin() + 1, Int{0}, std::plus{}, + [](coords const& u1, coords const& u2) { return u1.r * u2.c - u1.c * u2.r; }); + + // Shoelace formula returns the signed area: the sign depends on wether the polygon is traversed + // clockwise or counterclockise. This is not something we care about here, so we remove the sign. + return std::abs(twice_area); +} + +std::vector loop_path(xmas::views::text_matrix map, coords start, heading heading) { + assert(map.at(start) == 'S'); + std::vector vertices{start}; + + auto pos = take_step(map, start, heading); + if (!pos.has_value()) { + xlog::warning("Cancelling path after 0 steps"); + return {}; + } + + while (true) { + auto new_heading = update_heading(map, *pos, heading); + if (!new_heading.has_value()) { + break; + } + + if (*new_heading != heading) { + heading = *new_heading; + vertices.push_back(*pos); + } + + pos = take_step(map, *pos, heading); + if (!pos.has_value()) { + return {}; // Bad path + } + } + + if (map.at(*pos) != 'S') { + // Bad path + return {}; + } + + vertices.push_back(start); + + return vertices; +} + +Int loop_perimeter(xmas::views::text_matrix map, coords start, heading heading) { + auto vertices = loop_path(map, start, heading); + if (vertices.empty()) { + return 0; // Bad path + } + + return compute_perimeter(vertices); +} + +Int loop_enclosed_area(xmas::views::text_matrix map, coords start, heading heading) { + auto vertices = loop_path(map, start, heading); + if (vertices.empty()) { + return 0; // Bad path + } + vertices.push_back(vertices.front()); + + Int twice_area = compute_double_area(vertices); + Int perimeter = compute_perimeter(vertices); + + // Pick's theorem + // A = i + b/2 - 1 + // + // where A is the area, i is the count of internal points, and + // b is the count of boundary points. We can re-arrange it to: + // + // i = (2A - b)/2 + 1 + return (twice_area - perimeter) / 2 + 1; +} + +} + +std::uint64_t Day10::part1() { + xmas::views::text_matrix lines(this->input); + + auto p = std::ranges::find(this->input, 'S') - this->input.begin(); + coords S{ + .r = Int(p) / Int(lines.ncols() + 1), // +1 for the \n + .c = Int(p) % Int(lines.ncols() + 1), + }; + + auto heading = find_start_direction(lines, S); + + Int perimeter = loop_perimeter(lines, S, heading); + return static_cast(perimeter / 2); +} + +std::uint64_t Day10::part2() { + xmas::views::text_matrix lines(this->input); + + auto p = std::ranges::find(this->input, 'S') - this->input.begin(); + coords S{ + .r = Int(p) / Int(lines.ncols() + 1), // +1 for the \n + .c = Int(p) % Int(lines.ncols() + 1), + }; + + auto heading = find_start_direction(lines, S); + Int enclosed_area = loop_enclosed_area(lines, S, heading); + return static_cast(enclosed_area); +} diff --git a/2023/solvelib/10/day10.hpp b/2023/solvelib/10/day10.hpp new file mode 100644 index 0000000..689c115 --- /dev/null +++ b/2023/solvelib/10/day10.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "xmaslib/solution/solution.hpp" + +class Day10 : public xmas::solution { +public: + int day() override { + return 10; + } + +public: + std::uint64_t part1() override; + std::uint64_t part2() override; +}; diff --git a/2023/solvelib/10/day10_test.hpp b/2023/solvelib/10/day10_test.hpp new file mode 100644 index 0000000..43c7f46 --- /dev/null +++ b/2023/solvelib/10/day10_test.hpp @@ -0,0 +1,55 @@ +#include "day10.hpp" +#include + +TEST_CASE("Day 10") { + + SUBCASE("Part 1, example 1") { + Day10 solution{}; + solution.set_input("./data/10/example1.txt"); + solution.load(); + REQUIRE_EQ(solution.part1(), 4); + } + + SUBCASE("Part 1, example 2") { + Day10 solution{}; + solution.set_input("./data/10/example2.txt"); + solution.load(); + REQUIRE_EQ(solution.part1(), 8); + } + + SUBCASE("Part 2, example 1") { + Day10 solution{}; + solution.set_input("./data/10/example1.txt"); + solution.load(); + REQUIRE_EQ(solution.part2(), 1); + } + + SUBCASE("Part 2, example 2") { + Day10 solution{}; + solution.set_input("./data/10/example2.txt"); + solution.load(); + REQUIRE_EQ(solution.part2(), 1); + } + + SUBCASE("Part 2, example 3") { + Day10 solution{}; + solution.set_input("./data/10/example3.txt"); + solution.load(); + REQUIRE_EQ(solution.part2(), 4); + } + + SUBCASE("Part 2, example 4") { + Day10 solution{}; + solution.set_input("./data/10/example4.txt"); + solution.load(); + REQUIRE_EQ(solution.part2(), 10); + } + + SUBCASE("Real data") { + Day10 solution{}; + solution.set_input("./data/10/input.txt"); + solution.load(); + REQUIRE_EQ(solution.part1(), 6956); + REQUIRE_EQ(solution.part2(), 455); + } +} \ No newline at end of file diff --git a/2023/solvelib/16/day16.cpp b/2023/solvelib/16/day16.cpp index bee39fb..a6afff8 100644 --- a/2023/solvelib/16/day16.cpp +++ b/2023/solvelib/16/day16.cpp @@ -16,7 +16,7 @@ namespace { -enum class direction : std::uint8_t { +enum class heading : std::uint8_t { none = 0, left = 1, right = 2, @@ -43,18 +43,18 @@ static enum orientation splitter_orientation(char ch) { } struct cell { - direction visited = direction::none; + heading visited = heading::none; - bool history_contains(direction x) { + bool history_contains(heading x) { return (int(visited) & int(x)) != 0; } - void set_history(direction x) { - visited = direction(int(visited) | int(x)); + void set_history(heading x) { + visited = heading(int(visited) | int(x)); } }; struct beam { - direction towards; + heading towards; std::ptrdiff_t row; std::ptrdiff_t col; @@ -73,31 +73,31 @@ struct beam { void step() { switch (towards) { - case direction::left: + case heading::left: --col; break; - case direction::right: + case heading::right: ++col; break; - case direction::up: + case heading::up: --row; break; - case direction::down: + case heading::down: ++row; break; - case direction::none: + case heading::none: throw std::runtime_error("Beam heading nowhere"); } } orientation beam_orientation() { switch (towards) { - case direction::left: - case direction::right: + case heading::left: + case heading::right: return orientation::horizontal; - case direction::up: - case direction::down: + case heading::up: + case heading::down: return orientation::vertical; default: @@ -109,13 +109,13 @@ struct beam { switch (beam_orientation()) { case orientation::vertical: return { - {.towards = direction::left, .row = row, .col = col}, - {.towards = direction::right, .row = row, .col = col} + {.towards = heading::left, .row = row, .col = col}, + {.towards = heading::right, .row = row, .col = col} }; case orientation::horizontal: return { - {.towards = direction::up, .row = row, .col = col}, - {.towards = direction::down, .row = row, .col = col} + {.towards = heading::up, .row = row, .col = col}, + {.towards = heading::down, .row = row, .col = col} }; default: assert(false); @@ -147,31 +147,31 @@ struct beam { return false; // Splitter is parallel } - direction reflection(char pos) { + heading reflection(char pos) { switch (pos) { case '/': switch (towards) { - case direction::up: - return direction::right; - case direction::right: - return direction::up; - case direction::down: - return direction::left; - case direction::left: - return direction::down; + case heading::up: + return heading::right; + case heading::right: + return heading::up; + case heading::down: + return heading::left; + case heading::left: + return heading::down; default: assert(false); } case '\\': switch (towards) { - case direction::up: - return direction::left; - case direction::left: - return direction::up; - case direction::down: - return direction::right; - case direction::right: - return direction::down; + case heading::up: + return heading::left; + case heading::left: + return heading::up; + case heading::down: + return heading::right; + case heading::right: + return heading::down; default: assert(false); } @@ -179,7 +179,7 @@ struct beam { break; } - return direction::none; + return heading::none; } std::string print_state(xmas::views::text_matrix const& map, @@ -221,7 +221,7 @@ struct beam { char pos = map.at(srow(), scol()); - if (auto d = reflection(pos); d != direction::none) { + if (auto d = reflection(pos); d != heading::none) { towards = d; continue; } @@ -269,13 +269,13 @@ std::uint64_t solve(xmas::views::text_matrix const& map, beam init_beam) { xlog::debug("Running {} from ({:>3},{:>3}) results in {:>3} visited cells", xmas::lazy_string([&]() -> std::string { switch (init_beam.towards) { - case direction::right: + case heading::right: return "LEFT "; - case direction::left: + case heading::left: return "RIGHT"; - case direction::up: + case heading::up: return "UP "; - case direction::down: + case heading::down: return "DOWN "; default: return std::format("{:>4}?", int(init_beam.towards)); @@ -290,7 +290,7 @@ std::uint64_t solve(xmas::views::text_matrix const& map, beam init_beam) { std::uint64_t Day16::part1() { xmas::views::text_matrix map(this->input); - return solve(map, {.towards = direction::right, .row = 0, .col = -1}); + return solve(map, {.towards = heading::right, .row = 0, .col = -1}); } std::uint64_t Day16::part2() { @@ -301,10 +301,9 @@ std::uint64_t Day16::part2() { auto h = std::transform_reduce(std::execution::par_unseq, rows.begin(), rows.end(), std::uint64_t{0}, xmas::max{}, [&map](std::size_t row) { auto from_left = - solve(map, {.towards = direction::right, .row = std::ptrdiff_t(row), .col = -1}); - auto from_right = solve(map, {.towards = direction::left, - .row = std::ptrdiff_t(row), - .col = std::ptrdiff_t(map.ncols())}); + solve(map, {.towards = heading::right, .row = std::ptrdiff_t(row), .col = -1}); + auto from_right = solve(map, + {.towards = heading::left, .row = std::ptrdiff_t(row), .col = std::ptrdiff_t(map.ncols())}); return std::max(from_left, from_right); }); @@ -313,9 +312,9 @@ std::uint64_t Day16::part2() { auto v = std::transform_reduce(std::execution::par_unseq, cols.begin(), cols.end(), std::uint64_t{0}, xmas::max{}, [&map](std::size_t col) { auto from_above = - solve(map, {.towards = direction::down, .row = -1, .col = std::ptrdiff_t(col)}); + solve(map, {.towards = heading::down, .row = -1, .col = std::ptrdiff_t(col)}); auto from_below = solve(map, - {.towards = direction::up, .row = std::ptrdiff_t(map.nrows()), .col = std::ptrdiff_t(col)}); + {.towards = heading::up, .row = std::ptrdiff_t(map.nrows()), .col = std::ptrdiff_t(col)}); return std::max(from_above, from_below); }); return std::max(h, v); diff --git a/2023/solvelib/18/day18.cpp b/2023/solvelib/18/day18.cpp index 747c9a8..a20fed7 100644 --- a/2023/solvelib/18/day18.cpp +++ b/2023/solvelib/18/day18.cpp @@ -31,7 +31,7 @@ struct cell { bool filled; }; -enum direction { +enum heading { right = '0', down = '1', left = '2', @@ -205,7 +205,7 @@ blocks coords_to_blocks(coords p, canvas const& map) { } struct instruction { - direction dir; + heading dir; length_t len; static instruction parse_part1(std::string_view ln) { @@ -251,7 +251,7 @@ struct instruction { } const auto n = xmas::parse_hex(std::string_view{it + 1, it + 6}); - const auto d = static_cast(*(it + 6)); + const auto d = static_cast(*(it + 6)); return {d, n}; } diff --git a/2023/solvelib/23/day23.cpp b/2023/solvelib/23/day23.cpp index fac5911..77c39cf 100644 --- a/2023/solvelib/23/day23.cpp +++ b/2023/solvelib/23/day23.cpp @@ -97,7 +97,7 @@ struct graph { std::vector nodes; }; -enum direction { +enum heading { N = '^', E = '>', W = '<', @@ -110,7 +110,7 @@ struct temp_edge { length_t len = 0; bool bidirectional = true; - void step(direction towards) { + void step(heading towards) { switch (towards) { case N: --pos.row; @@ -140,7 +140,7 @@ enum BOOL : std::uint8_t { std::array::iterator explore(xmas::views::text_matrix map, xmas::basic_matrix& visited, std::array::iterator it, temp_edge tip, - direction towards) { + heading towards) { tip.step(towards); while (map.at(tip.pos) == char(towards)) { tip.bidirectional = false; diff --git a/2023/solvelib/CMakeLists.txt b/2023/solvelib/CMakeLists.txt index 91cb644..b2edd74 100644 --- a/2023/solvelib/CMakeLists.txt +++ b/2023/solvelib/CMakeLists.txt @@ -8,7 +8,7 @@ add_library(solvelib 07/day07.cpp 08/day08.cpp 09/day09.cpp - + 10/day10.cpp 11/day11.cpp 12/day12.cpp 13/day13.cpp diff --git a/2023/solvelib/alldays.hpp b/2023/solvelib/alldays.hpp index 4aac020..f4d4611 100644 --- a/2023/solvelib/alldays.hpp +++ b/2023/solvelib/alldays.hpp @@ -11,7 +11,7 @@ #include "07/day07.hpp" #include "08/day08.hpp" #include "09/day09.hpp" - +#include "10/day10.hpp" #include "11/day11.hpp" #include "12/day12.hpp" #include "13/day13.hpp" @@ -36,7 +36,7 @@ inline void populate_registry() { xmas::register_solution(); xmas::register_solution(); xmas::register_solution(); - + xmas::register_solution(); xmas::register_solution(); xmas::register_solution(); xmas::register_solution(); diff --git a/2023/test/test.cpp b/2023/test/test.cpp index 7167e85..52d9d0c 100644 --- a/2023/test/test.cpp +++ b/2023/test/test.cpp @@ -10,7 +10,7 @@ #include "solvelib/07/day07_test.hpp" #include "solvelib/08/day08_test.hpp" #include "solvelib/09/day09_test.hpp" - +#include "solvelib/10/day10_test.hpp" #include "solvelib/11/day11_test.hpp" #include "solvelib/12/day12_test.hpp" #include "solvelib/13/day13_test.hpp"