diff --git a/2023/day10/Cargo.lock b/2023/day10/Cargo.lock new file mode 100644 index 0000000..5f8ea61 --- /dev/null +++ b/2023/day10/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day10" +version = "0.1.0" diff --git a/2023/day10/Cargo.toml b/2023/day10/Cargo.toml new file mode 100644 index 0000000..40d2066 --- /dev/null +++ b/2023/day10/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023/day10/src/input b/2023/day10/src/input new file mode 100644 index 0000000..30784a7 --- /dev/null +++ b/2023/day10/src/input @@ -0,0 +1,140 @@ +FF--7-|7F--7F--|7F77-.FJF7F-7-F|7.77.FF7-F7---7-FJ7.|J--FJJ-FF-.JJ7LF7.J7F-7-F77L7.7-7.7FF77FF7..F--L7JF--.FF.7F|JFL7.7FL77.-7|7.F7-7FJ.FJ.- +FL-JF-L-JJFFJJ.7-JLL.7JF-J---7JLF7|LLF--.LJ.F7.LJL7--JJLJJJ.||JF|.F7LJ7.JJL7-FJ7L|-JL..LJJF-FJJ-|7|||F.|7.F-J7|FJF-JF--7L|7L---7-FL7FJ-FJ7.| +F7.||.7--7-LJ.FJ.|7..J-J7J|F.LFFJ7F7J|LJFL7.FJ7.|FJ..J|.|.F-LL-7..|F|J|7.FF77|F77J.F-JJ-J-|-|7JLL7-FJLFLF-7J.LJ7--7-7-L|JL-J7J7L..L-J|L|-FF7 +||F||FL7F7-|7|LF7|F|7|7|F7|..FJ|FJFJ.L--.FL.F.LF7J|FF.L7FLJ|.|7FJ-LJ|.|FL||.LF7JF.F||7JF--L-|7F7.-..L.7||..L7FL7|JL7L7-L77L|F-7J.7F|FL77.LL7 +-7FJFJFLJJ.F-JJ|LJL--7-F--F-|L|FL-LJF7J|F77.L--7L.|F77LJ7FJJFF-J7LL-J--JJ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|FJF7F7L7|L7|||||||LJ|||||||FJ||L7F7|L7FJ|||||||||FJ||L7FJ|FJ||L7FJ||LJ|FJL7|LJFJJLJFJF7L----J||LJL-----7||7||F7 +F77.|F-L-7|L7LJL-JL7|FJF---J||FJLJL7LJLLJ|||||L-7|||||||L7|L7||||FJ|FJ|LJLJLJ||JLJ||L7|L7||FJL7|||FJL7.LJF-JF7F7L-J|F7F--7LJF--7F7F-J||J7LFJ +LJLF-JL|LLJJL-7F--7LJL7L---7|LJF7F7L----7LJ|||F7|||||||L7||FJ||LJ|FJL7|JF7LF-JL7F--JFJL-J|||F7|||FJF7|F--JF7|LJL--7|||L-7L-7L-7LJLJ.LLJJLFJ| +LL.7.||LF-----J|F-JF-7L7F7FJ|F7||||F---7|F7||||LJ||||||FJ||L7|L7FJ|F-JL-JL7L7F7|L7F7L---7LJLJ|||||FJLJL---JLJF7F7FJLJ|F7L7L|F7L---7-JL|F||JL +FF-L7-J-L---7F7||F7L7L7LJ|L7||||||LJF-7LJ|LJ||L-7LJLJLJL7||FJ|FJL7|L-7F---JFJ|||FJ|L7F--JJF7-LJLJ||F7F7F----7|||LJF-7LJL7L7|||F---J7FJL7J.7J +7J--F7||F|7FJ|||LJL-JF|F7L-JLJLJ|L--JFJF7L7FJ|F-J.F7F-7FJ|||FJL-7||F-JL---7L7|LJL7|FJL----J|F---7|||LJLJF--7|||L-7|FJF-7|FJLJ|L--7J-L7J||-|. +LLF7JFLLLJFJFJLJF-----J|L7F7F7F7L---7L-J|FJ|-|L---JLJFJL-JLJL7F-J||L-7F7F7|FJ|F--J|L7F7F--7|L7F7LJ||F7F-JF-JLJ|F7LJL-JFJ||JF7|F7FJJFJL----|- +7F77LJ7||LL7|.LFJF7F7F7L7LJ||LJL7F-7L-7FJL7|FJF----7FJF7F---7|L-7LJF-J|LJ||L7|L7F7|FJ||L-7LJ|LJ|F7|LJLJF7L---7LJL-----JJLJFJ|LJLJFFJJ||LJFF. +|7||FL-|J-LLJ7.L-JLJLJL-JF7LJLF7LJLL-7LJF7LJL7L---7LJFJ|L--7LJF7|F-JF7L-7|L7|L7||||L7||F7L---7FJ|LJFF--JL----JF---7F7FF7F7L7|F7F77J|L|-77FFJ +--J7JF|J|JJLLF7F7JF7FF---JL---J|F---7L-7|L--7L7F-7L-7|FJF--JF7|LJ|F7||F7||FJL-J||LJ7||LJL7F7FJL7L7F7L-7F---7F7|F--J||FJLJL-JLJLJ|F--7LLLL-J7 +FJ.L.FJ.LF7F7FFJL-JL7L----7F--7|L--7L--J|F-7|FJ|FJF7LJ|FJF7FJ||F-J|LJ||LJ|L7-F7LJF7FJL-7.|||L7.L7||L-7||F--J|||L---JLJF---------J|F-J7|LJL7J +FL|7|.LL7LFJL-JF7F-7L77F-7LJF7||F7.L----JL7LJL7|L7||F7|L7||L7|||F7|F-JL-7L7L7|L--JLJF7FJFJ|L7L7||||F7LJ|L7F7|||F---7F7L7F-7F7F---JL-7-|7-LJ| +|JJLJ.FL|JL---7||L7L7L7L7|F-JLJ|||F---7F-7L-7FJL7||||LJFJ|L7|||||LJL7F7FJFJFJL---7F7|LJFJFJ.L7|FJ|LJL--J-LJLJLJL--7||L7LJFJ|LJF7F7F-JLF77J.J +F.FJ|LF.J.F---J||FJ7L7|F||L7F-7LJ|L--7|L7|F-JL7FJ||||F-JFJFJ||||L-7FJ|LJ7L-JF----J||L77L7|F-7LJL-JF7F---------7F7FJLJ-L-7L-JF-JLJ||||LL-7FJ7 +L-JLFJJ|.FL-7F7|LJ.F-JL-JL-J|F|F7L---JL7|LJF-7LJJLJ||L7FJLL7|||L-7|L7L---7F7L-7F7FJL7L-7|LJFJF7F--JLJF-----7F7LJ|L--7F7|L7F-JF-7FJL7-|LF7-L7 +L|.--.F|-7JFJ|||F-7L-7F---7FJFJ|L7F7F-7LJF7L7|F77F7||FJL7-FJ||L-7LJFJF-7FJ|L--J||L-7|F7|L7FJ-||L--7F7|F----J||F7L---J||F-JL--JFJL--J-|.LJ.LJ +F|7|JL-JF-JL-JLJ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F|FFJ|7L-7JF.FJ|F7L7|F---7F-7LJ|L-----JL-JF7F--7F7F--JLL7FJ-LJL-7L7-F7|FJ|.|FJ|F7||FJLJ|FJL7|L7FJLJ-L-------77LJFJFJJ|||L-JFJL7LJ|F--7|J-7JJ +LJ-..||.J|FFF7FJ|L7|LJF-7LJFJF7L7F----7F--J|L-7|||L----7|L7|J|F-JFJFJL-JFJFJL7LJ||LJF-7LJF7LJ|LJJF----------JF-7L-JF7LJ|F--JF7|F7||F-J7|-||7 +J.L7L-F7LLFFJ|L-J7||F7L7L-7L-JL7|L---7|L--7|F7||||F----JL7|F7FJF7L7L7F-7|||F7|F-JL7F|FJF-J|F----7L--7F7F-7F7F|FJF7FJ|.FJL7F-J|LJLJ||L|JL7F-7 +.F7J|LJ|7.FJFJJ7LFJ|||FL-7|F7F-JL----JL---J||LJLJ|L-----7|LJ|L7|L7|LLJFJL7|||||F--JFJL7L-7||F--7|JF7LJLJFLJL7||7|||FJFJF7LJF7L7F7FJL7F77|LJ| +FL7FL7.FJ7L7L-7FFJFJ||F--JLJLJF-----------7|L-7F7L-7F---JL--JFLJ-LJ-L-L7FJLJ||LJF7F|F7L--J|LJF-JL7||F7JF7F7FJ||FJLJ|FJFJL7FJL7LJLJF7LJ|-FF-. +7L77F7JJ.F-JF7L7L7|FJLJF--7F-7L7F---------JL-7||L-7|L-----77FF77||JJ-LLLJ.|-|L7FJL-J||F7F7L-7L--7LJLJL-J||LJ7|LJF--J|FJF7|L-7|F---JL--JFF|J. +J-|J-F7L-L--JL7L-JLJF7FJF-J|-L-JL--------7F7JLJ|F-JL7F----JF7||-|..|-FJLJ-L-L7|L-7F7|LJLJL-7L-7FJF-7F7F7LJ7F-JF-JF7FJL-J||F-J|L---------7-7| +.L|JFL-7L||F-7L-7F7FJ||7L--JF7.F--7F7F7F7LJL7F-JL--7|L7F7F-JLJL7F7--.LJ-|.FLLLJF7LJ||F-----JF7LJFJ.||LJL---JF7L--JLJF-7FJ|L7LL7F-7F-7F--JF7| +7.JFJLF|LF7L7L7FJ||L7||F----JL-JF7LJLJLJL---J|F7F7FJL-J|LJF7F7FJ||LL7L7FF-----FJL7|LJL--7F7FJL7FJF-J|F7F7F7FJL7F7F--JJLJ-|FJF7|L7LJFJL-7.|JF +F.FFJFLJL|L7L7|L7||FJLJL--------JL-----7-F7F7||LJ||F7F7|F-JLJLJ-|||.L|F|7-|-|.|F7L7F7F-7LJLJ-FJ|FJF7||LJLJLJF7|||L------7LJF||L-JF7L--7|777| +LFJF-L-|FL7L-JL7LJLJF------------------JFJ||||L-7LJ|||LJL-7F7JF-J|77.|-|LF--|FLJL7|||L7L----7L-JL-JLJL------J|||L7F--7F-JJF-JL---JL-7|||7.7J +F|LJ7-FJFLL--7FJF7F7L----7F-7F7F7F--7|F7L7LJLJF7L-7|LJF---J||7L7FJJL|J-|F|..|F7F-J||L7L7F--7|F-7F-----7F7F7F-JLJFJL-7|L7-FJF7F-7F---JF|L7-7| +-JF.|-J.F7F--JL7||||F7LF-JL7LJLJLJFFJFJL-JF7F7||F-J|F7L----JL--JL77-LLFJ-.--FJ|L-7||FJFJ|F-JLJFJL----7LJLJLJF7F7L---JL-JFJFJLJ|LJ-J-L-L-J7.L +LF-7||LF||L---7LJLJLJL7L7F-J|F-----J7|F---JLJ|||L--J||F7F7F7F7F7FJ7J7|FJL77FL7|F7|||||L7|L7F-7|7F--7LL------JLJ|F7F7F7F7|FJF7|F-7F|77.L.F|7. +F||FF|-FJL7|F7L------7L-J|F-7L-------JL7F---7LJ|F-7FJLJ||LJLJLJ||JJ-L-J||F||FJLJLJ||L77LJLLJFJL7L-7|F-----7F7F-J|LJLJLJ||L7|L7|FJF77F777FF-- +.J.L-J7L-7L-JL----7-FJF7FJL7L-7F-------J|F--JF7LJ7|L-77|L---7|FJ|.|F|7|FFFF7L----7||FJF7JF7FJF7|F7||L----7||||F7|F-----JL7||FJ||L|L7F7FL7L|J +FLJF--JJFL-------7|FJFJLJF7L-7LJF-------JL---JL--7|F7|FJF-7FJFL-JFFF7LF-F-JL7F---J||L7||FJLJFJ|LJLJ|F7F7FJLJLJ|||L--7F-7FJLJL-JL-JFJ|L7JJFJ. +F.L||FFFLF------7||L-JFF-J|F7L--JF---7F-7F7F7F-7FJ||LJL7L7|L7F-7-L-LJLJLL--7LJF--7|L7|||L-7FJFJF7F7LJLJ|L7-F7J|||F--J|FJ|F--------JJ|FJ.F--| +FJ-L7-F7JL-7F7F7LJL7F-7L-7LJL--7.L--7|L7||LJ|L7|L7|L-7FJFJL7|L7|.L7||-7LLF7L--JFFJL7|LJL--J|FL7|LJL7F-7L-JFJL-JLJ|-F-JL-JL---7F7F7F7||F7J-|| +F-77JJF----J|||L7F7|L7|F7L----7|F---J|FJLJF7L-J|FJL7FJL7|J-LJL|L7J7FJ|-LL||F-7F7L-7LJF--7F-JF7LJJF7LJ7L---JF-7F-7L-JF7F---7F7LJLJLJLJLJ|F-J7 +-7F7|.L-7F7FJLJFLJ|L-J|||F----J|L---7|L--7|L--7|L-7|L-7LJF-77L|FJ-F-7F7..||L7LJ|F-JF7|F7||LFJL7F7|L--7F---7L7|L7L--7||||F7LJL--7F------J7||| +FLF-77LLLJLJF7F7F-JF-7|||L--7F7L7F--JL7F-J|F--JL7FJL--J||L7|F7|L-7L7|||F7|L7|F7||F7|LJ|LJ|FJF7LJ|L--7LJF-7|FJL7L--7LJ|L-JL7F--7LJLF7F7||.-F- +F--JL|F7|JJFJLJLJF-JFJLJ|F7LLJL7|L---7LJ-FJL---7|L-7F---7FJ||LJF-JFJLJ||LJFJLJ||LJLJF-JF7|L-JL-7|F7JL-7|FJLJF7L---JF7|F--7LJF7L--7|LJL777L|J +|.L|7F-JJ|-L7F7F7|F-JF-7LJ|F---JL----JF-7|F----JL-7|L-7FJ|FJL7FJ.FJF--JL-7|F7FJL7FF7L--JLJFF-7FJLJL---J|L---JL--7F7||LJF7|F-JL--7LJF--JL7FL7 +.JLFFF7.LFF-J|||LJL--JFL-7|L---7F--7F7|FJ|L-7F--7-LJF-J|FJL7FJL-7L7L-7LLFJ|||L7FJFJ|F--7LF7L7|L7F-7F-7FJF------7LJLJL-7||LJF7F7FL--JF-7.F--J +..F-FJL7||L--JLJFF-------JL-7F-J|F-J|LJL-JF-J|F-JF-7L-7||F-JL7F-JJ|F7L7FJFJ||FJL7|FJL7FJFJL-JL-J|FJ|FJL7L--7F7.L--7F-7LJL7FJLJL-7F7FJFJ-LJ.L +--LJL7FJF----7JF7L-7F-7F-7F7|L--JL--JF7F-7L--JL--JFJF-J||L7LFJL7F7LJ|FJL7L-J|L7FJ||F7|L-JF7F---7|L-JL--J|F7LJL---7|L7|F-7LJF---7LJLJFJF7JFJ7 +|.|||||-L---7|FJL7FJ|FJL7||||F7F-----JLJFJF--7F---JLL-7|L7L-JF7|||F7||F7L--7L7|L-JLJLJF-7||||F-J|F--7F---JL-7F---JL-J|L7|F7L--7L7F7FJL||.|-7 +|7.-FJL7..F7||L-7|L7||F-JLJ||||L------7.L7L-7|L---7.F7||FJF--JLJ|||LJ|||F7FJFJL---7F7FJFJ||L7|F-JL-7|L-----7|L------7|FJ|||F7FJLLJLJ||||-7|| +L-7-L-7L--JLJL7FJL7LJ||F-7FJLJ|F7F---7L-7|F7||F---JFJ|||L7L7F7F-J|L-7LJ|||L7|F7F-7|||L7L7|L7|LJF---JL-7F---JL-------JLJ-LJLJLJF7F-7F7FJ|-JF7 +.LLFJJL7F-7F-7LJF7L-7LJL7LJF-7LJ|L--7|F7||||||L---7L7|||FJFJ|||F-JF-JF-J||FJ||LJFJLJL-JFJ|FJ|F7L-----7|L----7F-7F7F-----------J|L7LJLJFJ7F-L +L.L.L7LLJFLJLL7FJ|F-JF7FJF-JLL-7L---JLJLJLJ|||F7F-JFJ|||L7L7|LJL-7L-7L-7||L7||F-JF7JF-7L-JL7|||LF----JL-----JL7||||F--7F-------JFJF7F-JJJLJJ +|7.FLF7F---7F7LJFJL--JLJFJJF-7FJF7F-------7|||||L-7L7LJ|7L7LJF7F7|F7L7FJ||FJLJ|F7|L7L7|F7F7LJ|L7L--7F7F7F7F---JLJLJL-7|L--------JFJ|L--7|FL7 +LL-F7|LL7F7LJL-7L---7F-7L-7L7LJFJLJF------JLJLJL--JFJF7|F7|F-JLJLJ|L-JL-J||F--J|LJFJ-||||||F7L7|.F7LJLJLJLJF7F7F--7F-JL-7F7F7F--7|-L7F7|JJ-| +77--JJ-LLJ|F7F7L----J|FJF7|7L7FJJF-JF7F7F--7F7F7F7LL-J|LJLJL7|F7F7L-----7||L7F7|F-JF-J||LJLJL7||FJL--------J|||L7FJL---7LJ|||L-7|L7LLJLJ7LF- +|-7JL|.F|JLJLJL7F7F7FJL7|LJF7LJF-JF-JLJLJF-J|||LJL-7F-JF7F7FJFJLJ|F-7F--JLJFJ||||F7L7FJL--7F-J||L----------7LJ|FJL--7F7L-7|||F-JL7|.LLL-|.|J +JJF7JFLJ||F----J|LJ||F7||F-JL-7L7FJF----7L-7|||F---JL--JLJ|L-JF--JL7|L--7F-JFJ||||L-J|F-7FJ|F7||F7F7F-----7L-7||F--7LJL-7||||L7LLLJ--7|F-7L. +||L7.|7|FFL-----JF7LJ|||||F--7L7LJFL---7|F7LJLJ|F-7.F7F7F7|F-7|F7F7||F7FJL7-|FJ||L--7||FJL7|||||||||L----7L--JLJ|F-JJF-7|||||FJ77-JF|77|LJ7J +7-FJFF-LLF-------JL--J|LJLJF7L7|F7F----JLJL----JL7L7|||||||L7|LJ||LJ|||L-7|FJL7||FF7|LJ|F7||||||||||F---7L--7F7FJL---JFJLJ||||F-77.JJL|7LJ7. +L7|F-F--LL7F-7F-7F-7F7|F7JFJL-J|||L----7F7F7F-7F7L7LJ||||LJFJL--JL-7|||F7|LJF-J||FJ||F-J|||LJ||LJLJ|L--7L---J|||F---7FJ7LFJ||LJFJ7FL7L||7JL- +|FL.L|.F-|LJLLJFJ|FJ||LJL-JF--7LJL-----J|||||FJ|L7|F-J|||F-JF7F7F7FJ||LJ||F-JJFJ|L7||L--J||F7|L---7L7F7L---7-|LJL--7LJF7LL-JL7FJJ.LFFF-JL... +FJJ77LF--7F|LLLL-JL-JL-7F--JF-JF--------J||LJ|FJFJ||F7|||L-7||||||L7|L-7LJL7F7|FJ7|||F---JLJ||.F77|FJ|L---7L-JF7F-7L--JL-7-LL||L|7F-|LLL7..F +LJJ||JJLJ|F7.|F-------7LJF7FJF7|F--------JL-7|L7L7|||||||F-J|||LJ|FJL-7L-7FJ||||F7|||L7F7F7FJ|FJL7|L7|F7F7L---JLJJ|F7F---J7.F||F|LF-|7JF---7 +.|LFJJFLF|-|7LL--7F7F7L--J|L-JLJL--7F7F-7F7FJL-JFJ||||||||F7||L-7|L--7|F7||FJ||LJ||||FJ||||L7LJF-J|FJLJLJL-------7LJLJ7FF---7|L7|F|-FJL|JF|| +F-L|J7F--|F77-|F7LJ||L7F-7L7JF7F7F7|||L7|||L---7|FJ|||||||||||F7||F--J||||||FJL-7||LJL7|||L7L-7|F-J|F7F7F-7F-----JF7F7F7|F--JL-JF---L7|.||F7 +|F7-7F|L|L-L7-FJL7-LJLLJ.L7L-JLJLJLJ||-||||F7F7||L7|||||||||||||||L-7FJ||||||F7J||L--7||||FJF7||L-7|||||L7|L------JLJLJLJ|LL|FL--.F-L-|-LJ|J +.||F-||-FJ7L--|F7L--7F--7FL7F7F-7F-7||FJ||LJ|||||FJ||LJ|||||||||||F7||FJLJ||||L7|L7F-J|||||FJ|||F7|LJ|||FJL7F------7F7F-7L7.L|F-FJ-|LLLFJ-.J +F|F--F7F|-J.|7LJL7F7|L-7|F7||||FJ|7||||-LJF7||||||FJ|F-J|||||||||||||||F7FJ|LJF||FJL-7||LJ||FJ|LJ|L7FJ||L-7LJF---7LLJLJ-L-J7|||.L7.7|..L|..| +7-J|F|F7|L|.LLF-7LJ|L-7|||LJ|||L-JFJ||L---J|||||||L7|L-7|LJ||||||LJ|||LJ|L7L7FFJ|L-7FJ|||FJ|L7L-7|FJ|FJ|F-JLFJF7FJF7F7JF7F7F--7JJ7F|-J7F|.FJ +F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F--LJ7LF7|L|||JJ|-7J|7L.LJ-7.-7FL-LJ-7JJ.LFJ.LL-|FF|LJF---JF-7.7JJ..7-.J-FL|J-J.FJ--7JJ-7||-JJ---L|.LJ|.F|L77J||LJ.FL--J.FJLF7.|F7FF +L|L|.LLL-JL|.|-777-L77J.L-LL|7L|.L7J-L.FL-J.LJL7J-7J-L.LL-77.7-JFJL--|7L|J.--J.LJL-JJ.F7LJ.LJ.L----JLJJ.FJ|...7J-L-L7L|-7L7-LFJJ-|J--|7JLL-L diff --git a/2023/day10/src/lib.rs b/2023/day10/src/lib.rs new file mode 100644 index 0000000..fae5aea --- /dev/null +++ b/2023/day10/src/lib.rs @@ -0,0 +1,604 @@ +use std::collections::HashMap; + +#[derive(PartialEq, Clone, Copy, Eq, Hash)] +pub struct Coordinate { + pub x: isize, + pub y: isize, + pub c: Option, +} + +impl Coordinate { + fn get_coords_in_bounds( + &self, + g: &Vec>, + ) -> ( + Option, + Option, + Option, + Option, + ) { + let mut top: Option = None; + let mut right: Option = None; + let mut down: Option = None; + let mut left: Option = None; + + // wtf are we doing here... + // . Ahh okay, first we just check if the coordinates we are going to check + // . are actually in bounds of the grid. Okay cool, let's keep it. + if (self.y - 1) >= 0 { + top = Some(g[self.y as usize - 1][self.x as usize]); + } + + if (self.x + 1) < g.first().unwrap().len() as isize { + right = Some(g[self.y as usize][self.x as usize + 1]); + } + + if (self.y + 1) < g.len() as isize { + down = Some(g[self.y as usize + 1][self.x as usize]); + } + + if (self.x - 1) >= 0 as isize { + left = Some(g[self.y as usize][self.x as usize - 1]); + } + + return (top, right, down, left); + } + + pub fn get_connected_adjacent(&self, g: &Vec>) -> HashMap { + // First let's get the possibility of adjacent Coordiantes by checking + // . in bounds. + let (top, right, down, left) = self.get_coords_in_bounds(g); + + // let mut possible_paths: HashMap> = HashMap::new(); + let mut possible_paths: HashMap = HashMap::new(); + let mut direction_check: HashMap = HashMap::new(); + + // Now we construct a hash map to indicate the possible directions we _can go_ + // . given the type of Pipe in question. Also reasonable I guess. + match self.c { + Some(Pipe::J) => { + direction_check = HashMap::from([ + (Direction::UP, true), + (Direction::LEFT, true), + (Direction::RIGHT, false), + (Direction::DOWN, false), + ]) + } + Some(Pipe::F) => { + direction_check = HashMap::from([ + (Direction::UP, false), + (Direction::LEFT, false), + (Direction::RIGHT, true), + (Direction::DOWN, true), + ]) + } + Some(Pipe::HYPHEN) => { + direction_check = HashMap::from([ + (Direction::UP, false), + (Direction::LEFT, true), + (Direction::RIGHT, true), + (Direction::DOWN, false), + ]) + } + Some(Pipe::PIPE) => { + direction_check = HashMap::from([ + (Direction::UP, true), + (Direction::LEFT, false), + (Direction::RIGHT, false), + (Direction::DOWN, true), + ]) + } + Some(Pipe::SEVEN) => { + direction_check = HashMap::from([ + (Direction::UP, false), + (Direction::LEFT, true), + (Direction::RIGHT, false), + (Direction::DOWN, true), + ]) + } + Some(Pipe::L) => { + direction_check = HashMap::from([ + (Direction::UP, true), + (Direction::LEFT, false), + (Direction::RIGHT, true), + (Direction::DOWN, false), + ]) + } + Some(Pipe::START) => { + direction_check = HashMap::from([ + (Direction::UP, true), + (Direction::LEFT, true), + (Direction::RIGHT, true), + (Direction::DOWN, true), + ]) + } + None | Some(Pipe::GROUND) => {} + } + + // Now we get the intersection of the directions we can go to given our pipe, + // . and the available coordinates that are in bounds. + // + // For each of those conditions we need to check if the adjacent pipe can connect to + // . our current pipe. + + // Check top + if direction_check[&Direction::UP] { + match top { + Some(top) => { + if [Pipe::PIPE, Pipe::F, Pipe::SEVEN, Pipe::START].contains(&top.c.unwrap()) { + possible_paths.insert(Direction::UP, top); + } + } + None => {} + } + } + + // Check right + if direction_check[&Direction::RIGHT] { + match right { + Some(right) => { + if [Pipe::HYPHEN, Pipe::J, Pipe::SEVEN, Pipe::START].contains(&right.c.unwrap()) + { + possible_paths.insert(Direction::RIGHT, right); + } + } + None => {} + } + } + + // Check down + if direction_check[&Direction::DOWN] { + match down { + Some(down) => { + if [Pipe::PIPE, Pipe::J, Pipe::L, Pipe::START].contains(&down.c.unwrap()) { + possible_paths.insert(Direction::DOWN, down); + } + } + None => {} + } + } + + // Check left + if direction_check[&Direction::LEFT] { + match left { + Some(left) => { + if [Pipe::HYPHEN, Pipe::F, Pipe::L, Pipe::START].contains(&left.c.unwrap()) { + possible_paths.insert(Direction::LEFT, left); + } + } + None => {} + } + } + + return possible_paths; + } + fn get_all_adjacent(&self, g: &Vec>) -> HashMap> { + let mut all_adjacent: HashMap> = HashMap::new(); + for (k, v) in self.get_connected_adjacent(g).iter() { + all_adjacent.insert(*k, Some(*v)); + } + + // We then fill in the missing coordinates + for d in [ + Direction::UP, + Direction::RIGHT, + Direction::DOWN, + Direction::LEFT, + ] { + if !all_adjacent.contains_key(&d) { + all_adjacent.insert(d, None); + } + } + + all_adjacent + } +} + +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub enum Direction { + UP, + RIGHT, + DOWN, + LEFT, +} + +#[derive(PartialEq, Clone, Copy, Eq, Hash, Debug, Ord, PartialOrd)] +pub enum Pipe { + F, + GROUND, + HYPHEN, + J, + L, + PIPE, + SEVEN, + START, +} + +impl Pipe { + fn from_char(c: char) -> Option { + match c { + '|' => Some(Pipe::PIPE), + '-' => Some(Pipe::HYPHEN), + 'J' => Some(Pipe::J), + '7' => Some(Pipe::SEVEN), + 'L' => Some(Pipe::L), + 'F' => Some(Pipe::F), + '.' => Some(Pipe::GROUND), + 'S' => Some(Pipe::START), + _ => None, + } + } + + fn to_char(p: Pipe) -> char { + match p { + Pipe::PIPE => '|', + Pipe::HYPHEN => '-', + Pipe::J => 'J', + Pipe::SEVEN => '7', + Pipe::L => 'L', + Pipe::F => 'F', + Pipe::GROUND => '.', + Pipe::START => 'S', + } + } +} + +pub fn read_input(input: &str) -> (Coordinate, HashMap, Vec>) { + let mut start: Option = None; + let mut grid: Vec> = Vec::new(); + for (y, line) in input.lines().enumerate() { + grid.push(Vec::new()); + for (x, c) in line.chars().enumerate() { + let pipe: Pipe = Pipe::from_char(c).unwrap(); + let coord: Coordinate = Coordinate { + x: x as isize, + y: y as isize, + c: Some(pipe), + }; + grid[y].push(coord); + + if pipe == Pipe::START { + start = Some(coord); + } + } + } + + let mut next_possible = start.unwrap().get_connected_adjacent(&grid); + let (mut next_dir, mut current) = next_possible.drain().next().unwrap(); + let mut prev_dir = next_dir; + let mut pipe: HashMap = HashMap::new(); + pipe.insert(current, true); + + while !pipe.contains_key(&start.unwrap()) { + next_dir = next_direction(prev_dir, current.c.unwrap()); + current = next_coordinate(current, next_dir, &grid); + prev_dir = next_dir; + + // Then this coordinate is inserted into the pipe hashmap + if (current.x == 12 && current.y == 5) || (current.x == 2 && current.y == 2) { + println!("wtf"); + } + pipe.insert(current, true); + } + + match start { + None => panic!("start not found"), + Some(start) => (start, pipe, grid), + } +} + +fn next_coordinate( + current: Coordinate, + direction: Direction, + grid: &Vec>, +) -> Coordinate { + let (x, y): (isize, isize) = match direction { + Direction::UP => (current.x, current.y.checked_sub(1).unwrap()), + Direction::RIGHT => (current.x.checked_add(1).unwrap(), current.y), + Direction::DOWN => (current.x, current.y.checked_add(1).unwrap()), + Direction::LEFT => (current.x.checked_sub(1).unwrap(), current.y), + }; + + grid[y as usize][x as usize] +} + +fn next_direction(prev: Direction, pipe: Pipe) -> Direction { + match pipe { + Pipe::PIPE => { + match prev { + Direction::UP => Direction::UP, + // RIGHT => Direction::UP, + Direction::DOWN => Direction::DOWN, + // LEFT => Direction::UP, + _ => panic!("Impossible previous dir"), + } + }, + Pipe::HYPHEN => { + match prev { + // UP => Direction::UP, + Direction::RIGHT => Direction::RIGHT, + // DOWN => Direction::DOWN, + Direction::LEFT => Direction::LEFT, + _ => panic!("Impossible previous dir"), + } + }, + Pipe::F => { + match prev { + Direction::UP => Direction::RIGHT, + // RIGHT => Direction::RIGHT, + // DOWN => Direction::DOWN, + Direction::LEFT => Direction::DOWN, + _ => panic!("Impossible previous dir"), + } + }, + Pipe::J => { + match prev { + // UP => Direction::UP, + Direction::RIGHT => Direction::UP, + Direction::DOWN => Direction::LEFT, + // LEFT => Direction::LEFT, + _ => panic!("Impossible previous dir"), + } + }, + Pipe::L => { + match prev { + // UP => Direction::UP, + // RIGHT => Direction::RIGHT, + Direction::DOWN => Direction::RIGHT, + Direction::LEFT => Direction::UP, + _ => panic!("Impossible previous dir"), + } + }, + Pipe::SEVEN => { + match prev { + Direction::UP => Direction::LEFT, + Direction::RIGHT => Direction::DOWN, + // DOWN => Direction::DOWN, + // LEFT => Direction::LEFT, + _ => panic!("Impossible previous dir"), + } + }, + _ => panic!("Impossible"), + } +} + +pub fn part_one(start: Coordinate, grid: Vec>) -> isize { + let possible_adjacent = start.get_connected_adjacent(&grid); + let mut dist_map: HashMap = HashMap::new(); + let mut distance: usize = 1; + + let mut possible_directions: Vec<_> = possible_adjacent.keys().collect(); + let mut possible_coords: Vec = possible_directions + .iter() + .map(|d| possible_adjacent[&d]) + .collect(); + + let mut prev_dir: Direction = *possible_directions.pop().unwrap(); + let mut current = possible_coords.pop().unwrap(); + + // First we go around starting with the 'right path' + while current != start { + dist_map.insert(current, distance); + let next_dir = next_direction(prev_dir, current.c.expect("Always true")); + prev_dir = next_dir; + current = next_coordinate(current, next_dir, &grid); + distance += 1; + } + + // Then we reset variables and go around from the 'left path' + distance = 1; + + prev_dir = *possible_directions.pop().unwrap(); + current = possible_coords.pop().unwrap(); + while current != start { + // Get the current value for the current Coordinate, update + // . if the current value is greater than what our current + // . distance is, we expect this to be true until the halfway mark + match dist_map.get(¤t) { + Some(current_val) => { + if *current_val > distance { + dist_map.insert(current, distance); + } + } + None => continue, + } + + // Determine the next direction, save it for next iteration + // then update the current Coordinate + let next_dir = next_direction(prev_dir, current.c.expect("Always true")); + prev_dir = next_dir; + current = next_coordinate(current, next_dir, &grid); + + distance += 1; + } + + *dist_map.values().max().unwrap() as isize +} + +pub fn part_two(pipe: HashMap, grid: Vec>) -> isize { + // First let's clean up the original grid of all the useless pipes and create a + // a clean copy + let mut pipe = pipe.clone(); + let mut clean_grid: Vec> = Vec::new(); + + for (y, row) in grid.iter().enumerate() { + clean_grid.push(Vec::new()); + for c in row.iter() { + if c.c.unwrap() == Pipe::START { + let adj_map = c.get_all_adjacent(&grid); + + // UP, RIGHT, DOWN, LEFT + let adj_coordinates: (Option, Option, Option, Option) = ( + adj_map[&Direction::UP].and_then(|v| v.c), + adj_map[&Direction::RIGHT].and_then(|v| v.c), + adj_map[&Direction::DOWN].and_then(|v| v.c), + adj_map[&Direction::LEFT].and_then(|v| v.c), + ); + + let whoami: Option = match adj_coordinates { + // Hyphen + (_, Some(Pipe::J), _, Some(Pipe::L)) // L - J + | (_, Some(Pipe::SEVEN), _, Some(Pipe::F)) // F - 7 + | (_, Some(Pipe::HYPHEN), _, Some(Pipe::HYPHEN)) // - - - + | (_, Some(Pipe::J), _, Some(Pipe::HYPHEN)) // - - J + | (_, Some(Pipe::SEVEN), _, Some(Pipe::HYPHEN)) // - - 7 + | (_,Some(Pipe::HYPHEN), _, Some(Pipe::F)) // F - - + | (_, Some(Pipe::HYPHEN), _, Some(Pipe::L)) // L - - + => Some(Pipe::HYPHEN), + + // Pipe + (Some(Pipe::F), _, Some(Pipe::L), _) + | (Some(Pipe::F), _, Some(Pipe::J), _) + | (Some(Pipe::SEVEN), _, Some(Pipe::L), _) + | (Some(Pipe::SEVEN), _, Some(Pipe::J), _) + | (Some(Pipe::PIPE), _, Some(Pipe::PIPE), _) + | (Some(Pipe::PIPE), _, Some(Pipe::J), _) + | (Some(Pipe::PIPE), _, Some(Pipe::L), _) + | (Some(Pipe::F), _, Some(Pipe::PIPE), _) + | (Some(Pipe::SEVEN), _, Some(Pipe::PIPE), _) + => Some(Pipe::PIPE), + + // F + (_, Some(Pipe::HYPHEN), Some(Pipe::L), _) + | (_, Some(Pipe::HYPHEN), Some(Pipe::J), _) + | (_, Some(Pipe::SEVEN), Some(Pipe::L), _) + | (_, Some(Pipe::SEVEN), Some(Pipe::J), _) + | (_, Some(Pipe::HYPHEN), Some(Pipe::PIPE), _) + | (_, Some(Pipe::SEVEN), Some(Pipe::PIPE), _) + | (_, Some(Pipe::J), Some(Pipe::L), _) + | (_, Some(Pipe::J), Some(Pipe::J), _) + | (_, Some(Pipe::J), Some(Pipe::PIPE), _) + => Some(Pipe::F), + + // J + (Some(Pipe::PIPE),_, _, Some(Pipe::F)) + | (Some(Pipe::PIPE),_, _, Some(Pipe::SEVEN)) + | (Some(Pipe::PIPE),_, _, Some(Pipe::HYPHEN)) + | (Some(Pipe::F),_, _, Some(Pipe::F)) + | (Some(Pipe::F),_, _, Some(Pipe::SEVEN)) + | (Some(Pipe::F),_, _, Some(Pipe::HYPHEN)) + | (Some(Pipe::SEVEN),_, _, Some(Pipe::F)) + | (Some(Pipe::SEVEN),_, _, Some(Pipe::SEVEN)) + | (Some(Pipe::SEVEN),_, _, Some(Pipe::HYPHEN)) + => Some(Pipe::J), + + // SEVEN + (_, _, Some(Pipe::PIPE), Some(Pipe::HYPHEN)) + | (_, _, Some(Pipe::PIPE), Some(Pipe::F)) + | (_, _, Some(Pipe::PIPE), Some(Pipe::L)) + | (_, _, Some(Pipe::J), Some(Pipe::HYPHEN)) + | (_, _, Some(Pipe::J), Some(Pipe::F)) + | (_, _, Some(Pipe::J), Some(Pipe::L)) + | (_, _, Some(Pipe::L), Some(Pipe::HYPHEN)) + | (_, _, Some(Pipe::L), Some(Pipe::F)) + | (_, _, Some(Pipe::L), Some(Pipe::L)) + => Some(Pipe::J), + + // L + (Some(Pipe::PIPE), Some(Pipe::HYPHEN), _, _) + | (Some(Pipe::PIPE), Some(Pipe::J), _, _) + | (Some(Pipe::PIPE), Some(Pipe::SEVEN), _, _) + | (Some(Pipe::SEVEN), Some(Pipe::HYPHEN), _, _) + | (Some(Pipe::SEVEN), Some(Pipe::J), _, _) + | (Some(Pipe::SEVEN), Some(Pipe::SEVEN), _, _) + | (Some(Pipe::F), Some(Pipe::HYPHEN), _, _) + | (Some(Pipe::F), Some(Pipe::J), _, _) + | (Some(Pipe::F), Some(Pipe::SEVEN), _, _) + => Some(Pipe::L), + _ => panic!("Help!") + }; + + match whoami { + Some(_whoami) => { + // Push identified Pipe for 'S' into the grid. + let s = Coordinate { + x: c.x, + y: c.y, + c: Some(_whoami), + }; + clean_grid[y].push(s); + + // Now we insert into the pipe to ensure that + // . we don't include this as an insider later on. + pipe.insert(s, true); + } + None => panic!("Help!"), + } + } else if pipe.contains_key(c) || c.c.unwrap() == Pipe::GROUND { + clean_grid[y].push(*c); + } else { + clean_grid[y].push(Coordinate { + x: c.x, + y: c.y, + c: Some(Pipe::GROUND), + }) + } + } + } + + for row in clean_grid.iter() { + for c in row.iter() { + print!("{}", Pipe::to_char(c.c.unwrap())) + } + println!(); + } + + // .......... + // .S------7. + // .|F----7|. + // .||OOOO||. + // .||OOOO||. + // .|L-7F-J|. + // .|II||II|. + // .L--JL--J. + // .......... + + let mut count = 0; + + for (y, row) in clean_grid.iter().enumerate() { + // Reset for each row + let mut p: Option = None; + let mut inside: bool = false; + + for (x, c) in row.iter().enumerate() { + // Taken from: + // https://github.com/UncleScientist/aoclib-rs/blob/main/crates/aoc2023/src/aoc2023_10.rs#L127-L153 + + // After watching a few videos of people solving Part 2, it seems like, + // . one only has to check the row and not the column. (n=2) + // . Hoping that this is the case for me as well. + + if pipe.contains_key(c) { + match c.c { + Some(Pipe::F) => p = Some(Pipe::F), + Some(Pipe::GROUND) => {} + Some(Pipe::HYPHEN) => {} + Some(Pipe::J) => { + if p == Some(Pipe::F) { + inside = !inside + } + } + Some(Pipe::L) => p = Some(Pipe::L), + Some(Pipe::PIPE) => inside = !inside, + Some(Pipe::SEVEN) => { + if p == Some(Pipe::L) { + inside = !inside + } + } + Some(Pipe::START) => {} + None => panic!("Not possible"), + } + + // println!( + // "PIPE | x: {}, y: {}, c: {:?}, inside: {:?}, pipe: {:?}", + // x, y, c.c.unwrap(), inside, p + // ) + } else if inside { + // println!("INSIDE | x: {}, y: {}, c: {:?}", x, y, c.c.unwrap()); + count += 1 + } + } + } + + count +} diff --git a/2023/day10/tests/integration_test.rs b/2023/day10/tests/integration_test.rs new file mode 100644 index 0000000..967bcbf --- /dev/null +++ b/2023/day10/tests/integration_test.rs @@ -0,0 +1,114 @@ +use day10; +use std::fs::read_to_string; + +#[test] +fn test_map_parsing_and_pipe_generation() { + let test_input = r#"..F7. +.FJ|. +SJ.L7 +|F--J +LJ..."#; + + let (_, pipe, grid) = day10::read_input(test_input); + let c = day10::Coordinate{ + x: 2, + y: 1, + c: Some(day10::Pipe::J), + }; + + assert!(pipe.get(&c).unwrap()); + + // Count PIPE + let pipe_count = pipe.iter().filter(|(k,_)| k.c.unwrap() == day10::Pipe::PIPE).count(); + assert_eq!(pipe_count, 2); + + // Count J + let j_count = pipe.iter().filter(|(k,_)| k.c.unwrap() == day10::Pipe::J).count(); + assert_eq!(j_count, 4); + + // Count SEVEN + assert_eq!(pipe.iter().filter(|(k,_)| k.c.unwrap() == day10::Pipe::SEVEN).count(), 2); + + // Count HYPHEN + assert_eq!(pipe.iter().filter(|(k,_)| k.c.unwrap() == day10::Pipe::HYPHEN).count(), 2); + + // Count L + assert_eq!(pipe.iter().filter(|(k,_)| k.c.unwrap() == day10::Pipe::L).count(), 2); + + // Count F + assert_eq!(pipe.iter().filter(|(k,_)| k.c.unwrap() == day10::Pipe::F).count(), 3); + + + assert_eq!(pipe.len(), 16); + + let next_possible = c.get_connected_adjacent(&grid); + + assert_eq!(next_possible.len(), 2); +} + + +#[test] +fn test_test_part_one() { + let test_input = r#"..F7. +.FJ|. +SJ.L7 +|F--J +LJ..."#; + + let (start, _, grid) = day10::read_input(test_input); + let test_result = day10::part_one(start, grid); + assert_eq!(test_result, 8); +} + +#[test] +fn test_part_one() { + let test_input = read_to_string("src/input").unwrap(); + let (start, _, grid) = day10::read_input(test_input.as_str()); + let test_result = day10::part_one(start, grid); + assert_eq!(test_result, 6870); +} + +#[test] +fn test_test_part_two() { + let test_input = r#".F----7F7F7F7F-7.... +.|F--7||||||||FJ.... +.||.FJ||||||||L7.... +FJL7L7LJLJ||LJ.L-7.. +L--J.L7...LJS7F-7L7. +....F-J..F7FJ|L7L7L7 +....L7.F7||L7|.L7L7| +.....|FJLJ|FJ|F7|.LJ +....FJL-7.||.||||... +....L---J.LJ.LJLJ..."#; + +// 'I's here are corrected +// +// OF----7F7F7F7F-7OOOO +// O|F--7||||||||FJOOOO +// O||OFJ||||||||L7OOOO +// FJL7L7LJLJ||LJIL-7OO +// L--JOL7IIILJS7F-7L7O +// OOOOF-JIIF7FJ|L7L7L7 +// OOOOL7IF7||L7|IL7L7| +// OOOOO|FJLJ|FJ|F7|OLJ +// OOOOFJL-7O||O||||OOO +// OOOOL---JOLJOLJLJOOO + + + let (_, pipe, grid) = day10::read_input(test_input); + + assert_eq!(grid[4][13].c.unwrap(), day10::Pipe::SEVEN); + assert_eq!(grid[4][12].c.unwrap(), day10::Pipe::START); + assert_ne!(grid[5][12].c.unwrap(), day10::Pipe::GROUND); + + let test_result = day10::part_two(pipe, grid); + assert_eq!(test_result, 8); +} + +#[test] +fn test_test_part_twopart_two() { + let test_input = read_to_string("src/input").unwrap(); + let (_, pipe, grid) = day10::read_input(test_input.as_str()); + let test_result = day10::part_two(pipe, grid); + assert_eq!(test_result, 287); +} \ No newline at end of file