diff --git a/Cargo.lock b/Cargo.lock index 31e2cd0..2c99114 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,6 +214,14 @@ dependencies = [ "itertools", ] +[[package]] +name = "day-22" +version = "0.0.0" +dependencies = [ + "indoc", + "itertools", +] + [[package]] name = "deprecate-until" version = "0.1.1" diff --git a/day-22/Cargo.toml b/day-22/Cargo.toml new file mode 100644 index 0000000..2edf10e --- /dev/null +++ b/day-22/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "day-22" +version = "0.0.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools.workspace = true + +[dev-dependencies] +indoc.workspace = true diff --git a/day-22/src/bin/input.txt b/day-22/src/bin/input.txt new file mode 100644 index 0000000..98cd12d --- /dev/null +++ b/day-22/src/bin/input.txt @@ -0,0 +1,1203 @@ +7,3,85~9,3,85 +2,8,123~4,8,123 +8,2,69~8,2,72 +3,1,280~3,4,280 +4,1,6~4,2,6 +7,1,156~7,3,156 +1,5,105~4,5,105 +5,8,69~5,8,69 +5,0,255~5,3,255 +2,8,120~3,8,120 +3,3,202~5,3,202 +0,5,80~2,5,80 +5,9,123~6,9,123 +6,1,249~9,1,249 +9,4,100~9,6,100 +4,4,124~6,4,124 +3,4,222~3,4,223 +4,5,284~4,8,284 +3,5,44~5,5,44 +3,2,132~3,5,132 +5,2,127~7,2,127 +0,2,11~0,3,11 +4,7,151~5,7,151 +0,3,232~0,5,232 +0,9,34~3,9,34 +2,1,195~4,1,195 +0,0,50~0,3,50 +6,4,234~6,7,234 +3,5,234~3,6,234 +5,2,213~8,2,213 +4,2,223~4,5,223 +8,3,9~8,4,9 +2,2,292~2,4,292 +5,7,261~6,7,261 +4,5,263~4,7,263 +7,0,137~9,0,137 +6,0,4~6,0,4 +2,7,282~4,7,282 +9,3,291~9,4,291 +0,8,163~2,8,163 +4,9,111~7,9,111 +3,7,104~4,7,104 +9,0,34~9,3,34 +1,5,57~3,5,57 +8,5,214~9,5,214 +9,5,279~9,8,279 +2,4,206~4,4,206 +5,1,136~5,2,136 +1,2,219~1,3,219 +4,2,143~6,2,143 +4,8,195~6,8,195 +5,1,85~8,1,85 +4,4,10~4,4,11 +5,6,91~8,6,91 +1,6,51~1,6,51 +3,9,237~5,9,237 +3,5,264~3,6,264 +7,3,232~9,3,232 +4,1,192~6,1,192 +2,2,232~5,2,232 +7,4,287~7,7,287 +4,8,63~4,9,63 +3,5,295~6,5,295 +4,6,39~4,9,39 +8,7,217~8,9,217 +6,7,229~8,7,229 +4,4,184~4,5,184 +6,3,236~8,3,236 +8,6,13~9,6,13 +0,3,12~0,4,12 +5,7,9~5,7,12 +1,3,151~1,5,151 +3,9,71~4,9,71 +2,8,60~4,8,60 +3,2,102~3,5,102 +3,6,26~6,6,26 +4,7,113~4,9,113 +7,1,268~7,1,270 +3,4,69~5,4,69 +7,6,165~9,6,165 +7,7,131~7,9,131 +8,5,74~8,7,74 +1,8,222~4,8,222 +3,3,189~3,5,189 +5,6,215~5,8,215 +5,5,111~6,5,111 +2,8,57~5,8,57 +3,3,101~3,4,101 +7,1,252~8,1,252 +0,0,2~3,0,2 +0,5,86~0,7,86 +7,1,229~9,1,229 +0,6,124~2,6,124 +8,3,32~9,3,32 +8,5,229~8,6,229 +7,4,285~7,6,285 +6,3,187~6,6,187 +2,5,237~4,5,237 +2,1,294~4,1,294 +1,5,241~3,5,241 +3,2,4~3,5,4 +0,4,44~3,4,44 +5,4,74~5,6,74 +4,7,154~6,7,154 +5,0,80~6,0,80 +2,2,234~2,4,234 +5,3,57~5,6,57 +8,3,148~8,5,148 +1,7,60~2,7,60 +0,0,3~0,0,4 +4,6,84~4,8,84 +6,0,140~7,0,140 +1,6,252~1,7,252 +3,7,276~3,9,276 +7,8,1~8,8,1 +3,5,187~4,5,187 +3,6,29~3,9,29 +2,1,233~4,1,233 +3,3,133~5,3,133 +5,6,128~5,6,131 +2,2,235~4,2,235 +2,4,7~4,4,7 +1,1,260~1,3,260 +8,7,176~9,7,176 +7,7,64~8,7,64 +4,8,10~6,8,10 +0,1,234~3,1,234 +3,7,33~3,9,33 +7,7,71~7,9,71 +0,5,179~1,5,179 +3,2,32~5,2,32 +5,7,3~7,7,3 +6,1,258~9,1,258 +5,4,258~5,7,258 +2,6,217~2,8,217 +0,4,131~0,4,133 +8,0,242~8,0,244 +9,4,262~9,7,262 +2,5,6~5,5,6 +8,3,159~8,5,159 +4,2,78~7,2,78 +6,0,235~8,0,235 +5,4,64~5,4,66 +6,4,273~6,6,273 +7,2,138~7,3,138 +1,0,6~4,0,6 +7,3,154~7,4,154 +9,1,39~9,4,39 +5,5,168~5,7,168 +1,5,41~3,5,41 +1,2,50~1,5,50 +4,2,259~6,2,259 +0,0,218~0,3,218 +5,6,40~8,6,40 +7,9,236~9,9,236 +8,4,6~8,7,6 +2,4,30~2,6,30 +0,7,221~0,7,222 +7,1,202~7,1,202 +4,2,181~4,5,181 +5,0,283~7,0,283 +4,6,238~5,6,238 +4,2,67~6,2,67 +2,7,228~2,9,228 +0,6,164~0,9,164 +1,2,193~3,2,193 +3,9,129~6,9,129 +7,3,266~9,3,266 +3,5,75~4,5,75 +0,9,37~1,9,37 +2,1,50~2,4,50 +0,4,2~0,6,2 +4,2,207~4,2,207 +3,1,108~3,2,108 +6,9,201~8,9,201 +7,6,176~8,6,176 +8,6,73~8,8,73 +6,9,69~8,9,69 +2,2,181~2,5,181 +9,4,232~9,7,232 +3,4,114~4,4,114 +3,3,225~3,5,225 +0,7,179~0,9,179 +0,5,109~2,5,109 +9,6,92~9,9,92 +9,1,96~9,2,96 +7,6,99~8,6,99 +6,3,116~6,5,116 +1,2,46~1,4,46 +4,8,87~5,8,87 +1,4,59~3,4,59 +0,1,163~0,3,163 +9,8,176~9,9,176 +3,5,28~3,5,29 +6,4,90~6,6,90 +5,9,133~7,9,133 +3,8,85~5,8,85 +8,3,244~8,4,244 +6,3,204~9,3,204 +2,2,295~4,2,295 +1,3,182~3,3,182 +3,0,122~4,0,122 +3,2,225~6,2,225 +0,6,166~2,6,166 +1,4,53~1,5,53 +1,2,88~1,2,90 +6,8,236~8,8,236 +3,2,141~3,3,141 +7,6,102~7,7,102 +9,3,215~9,5,215 +6,2,110~6,4,110 +5,0,287~7,0,287 +1,8,194~4,8,194 +2,2,8~2,4,8 +2,5,216~5,5,216 +5,8,186~7,8,186 +0,1,240~0,3,240 +1,3,295~3,3,295 +9,4,274~9,5,274 +3,4,249~3,6,249 +2,4,244~2,6,244 +4,9,68~4,9,68 +1,2,237~4,2,237 +8,1,141~9,1,141 +4,0,142~6,0,142 +8,1,124~8,3,124 +4,2,285~4,4,285 +1,2,192~1,4,192 +1,8,225~3,8,225 +6,3,203~6,6,203 +4,2,116~6,2,116 +4,6,177~4,8,177 +1,0,108~1,1,108 +9,1,40~9,3,40 +9,4,293~9,6,293 +5,1,231~5,2,231 +5,2,13~5,2,16 +6,0,219~7,0,219 +2,0,119~3,0,119 +7,3,238~7,3,241 +7,5,257~7,6,257 +2,8,146~5,8,146 +3,1,224~5,1,224 +3,1,158~3,3,158 +2,1,78~2,3,78 +4,4,9~7,4,9 +3,3,277~3,5,277 +5,6,134~7,6,134 +6,5,43~6,7,43 +7,2,198~7,5,198 +4,4,62~7,4,62 +6,2,136~8,2,136 +7,1,203~9,1,203 +2,9,231~5,9,231 +3,0,5~5,0,5 +4,3,127~4,5,127 +6,7,200~6,9,200 +3,6,118~6,6,118 +6,0,271~6,2,271 +3,0,200~3,1,200 +7,5,259~9,5,259 +5,4,39~5,7,39 +3,8,117~5,8,117 +0,3,299~0,4,299 +7,0,130~7,3,130 +8,0,121~8,1,121 +2,5,161~2,8,161 +8,0,28~8,1,28 +0,1,257~1,1,257 +7,2,263~7,4,263 +8,5,285~8,7,285 +3,0,221~3,2,221 +9,3,256~9,6,256 +1,7,164~1,9,164 +0,9,247~1,9,247 +5,7,282~6,7,282 +2,3,46~2,5,46 +6,2,83~7,2,83 +1,7,196~1,9,196 +9,0,35~9,1,35 +0,2,263~3,2,263 +4,7,82~5,7,82 +0,2,265~0,2,266 +6,4,13~7,4,13 +6,1,266~9,1,266 +5,3,226~5,5,226 +8,3,52~8,5,52 +2,1,150~2,3,150 +0,2,152~3,2,152 +7,2,2~7,2,2 +9,0,140~9,1,140 +4,3,252~4,5,252 +7,8,133~9,8,133 +4,9,131~4,9,131 +0,3,248~0,6,248 +1,7,51~3,7,51 +0,5,175~0,8,175 +9,2,102~9,3,102 +2,4,127~2,4,129 +1,5,1~1,5,3 +4,6,226~7,6,226 +1,7,134~3,7,134 +6,4,61~6,7,61 +3,6,2~5,6,2 +2,6,61~4,6,61 +4,3,89~4,4,89 +6,6,230~7,6,230 +8,0,15~8,0,17 +3,3,220~3,6,220 +8,4,145~8,7,145 +3,7,279~3,7,280 +0,1,132~0,1,133 +8,5,95~8,7,95 +6,6,210~6,8,210 +8,1,44~9,1,44 +7,4,3~9,4,3 +2,1,101~2,4,101 +2,1,12~2,3,12 +0,3,225~0,6,225 +1,1,227~3,1,227 +3,9,170~5,9,170 +5,0,241~6,0,241 +3,5,26~5,5,26 +2,7,213~4,7,213 +2,9,183~4,9,183 +7,0,218~7,2,218 +3,6,267~3,6,269 +9,4,11~9,6,11 +8,0,234~8,2,234 +2,5,108~3,5,108 +4,9,234~4,9,236 +9,4,91~9,7,91 +1,0,226~4,0,226 +1,0,199~1,2,199 +0,5,178~0,6,178 +5,8,100~7,8,100 +4,5,212~6,5,212 +3,3,201~7,3,201 +3,6,285~3,9,285 +7,2,245~7,4,245 +0,1,201~0,3,201 +5,5,217~7,5,217 +3,6,31~3,6,33 +4,2,242~6,2,242 +1,3,242~1,5,242 +8,4,274~8,5,274 +0,0,124~0,0,127 +4,3,258~4,3,261 +4,7,198~4,9,198 +4,1,244~4,3,244 +0,0,159~1,0,159 +7,3,253~7,6,253 +6,9,70~8,9,70 +0,8,240~2,8,240 +8,6,260~8,7,260 +5,5,249~7,5,249 +6,8,269~6,9,269 +7,9,186~7,9,186 +4,4,190~4,6,190 +1,3,51~1,5,51 +3,5,195~4,5,195 +7,0,84~7,2,84 +8,3,221~8,6,221 +1,0,205~4,0,205 +4,2,111~4,4,111 +3,2,268~6,2,268 +5,5,147~7,5,147 +4,9,204~7,9,204 +3,7,167~3,9,167 +7,0,215~7,3,215 +8,5,257~8,8,257 +0,4,227~0,5,227 +5,6,151~5,6,151 +4,4,260~6,4,260 +3,2,52~3,4,52 +7,3,17~8,3,17 +5,7,46~5,9,46 +5,2,203~5,3,203 +0,5,81~0,5,83 +5,2,62~5,3,62 +6,2,280~9,2,280 +2,4,230~2,5,230 +3,3,154~3,4,154 +4,8,270~6,8,270 +6,9,160~8,9,160 +9,0,1~9,2,1 +9,5,173~9,8,173 +3,7,249~5,7,249 +2,0,268~2,3,268 +0,9,200~0,9,202 +3,5,63~3,6,63 +6,9,230~9,9,230 +3,2,228~6,2,228 +3,1,207~5,1,207 +8,5,208~9,5,208 +3,4,128~4,4,128 +2,7,30~4,7,30 +0,1,291~3,1,291 +7,0,141~9,0,141 +6,2,125~6,4,125 +0,4,119~3,4,119 +4,4,231~5,4,231 +0,9,186~2,9,186 +9,3,101~9,5,101 +3,5,109~4,5,109 +6,8,214~6,9,214 +0,2,38~2,2,38 +2,5,137~3,5,137 +1,7,143~1,7,143 +1,7,166~1,9,166 +5,5,264~5,8,264 +2,2,238~2,3,238 +0,3,122~0,5,122 +3,3,144~3,3,147 +9,0,36~9,2,36 +7,1,267~9,1,267 +4,3,33~6,3,33 +2,2,18~5,2,18 +4,1,177~6,1,177 +1,0,254~1,2,254 +4,6,64~6,6,64 +5,8,212~8,8,212 +0,4,249~0,5,249 +5,3,256~5,3,257 +2,2,266~2,4,266 +6,6,50~6,6,52 +1,3,11~1,4,11 +7,5,207~8,5,207 +3,6,65~3,6,67 +1,2,2~4,2,2 +9,0,281~9,2,281 +7,2,157~8,2,157 +3,1,203~5,1,203 +5,5,169~6,5,169 +1,0,203~3,0,203 +4,8,97~7,8,97 +3,6,55~6,6,55 +0,8,246~1,8,246 +3,1,82~5,1,82 +2,6,53~2,8,53 +4,1,271~4,3,271 +8,1,100~8,4,100 +1,1,93~1,4,93 +0,5,139~1,5,139 +5,6,149~5,8,149 +9,9,130~9,9,133 +7,6,70~9,6,70 +0,6,251~0,8,251 +3,2,34~5,2,34 +0,9,240~1,9,240 +6,8,238~8,8,238 +9,0,88~9,0,90 +5,8,228~7,8,228 +4,6,286~4,9,286 +0,1,51~3,1,51 +4,1,112~4,2,112 +4,5,108~6,5,108 +8,4,92~8,4,94 +4,6,210~4,6,211 +2,4,249~2,5,249 +6,6,123~6,8,123 +6,2,254~6,2,256 +5,5,50~6,5,50 +4,5,128~6,5,128 +0,3,47~0,5,47 +4,5,8~4,7,8 +1,4,208~4,4,208 +5,3,13~6,3,13 +1,4,43~1,6,43 +2,8,61~3,8,61 +3,8,234~5,8,234 +1,0,27~1,3,27 +2,2,140~5,2,140 +7,0,105~9,0,105 +2,5,256~2,5,258 +0,2,63~0,4,63 +7,4,120~9,4,120 +5,7,236~7,7,236 +2,8,231~3,8,231 +5,1,270~5,2,270 +3,1,248~3,3,248 +7,6,95~7,8,95 +2,2,155~2,3,155 +2,4,140~2,5,140 +2,5,75~2,6,75 +1,7,195~4,7,195 +5,4,281~5,7,281 +9,0,93~9,2,93 +5,5,93~6,5,93 +6,5,144~8,5,144 +1,9,1~2,9,1 +1,5,204~1,5,204 +5,9,283~8,9,283 +1,3,75~1,6,75 +5,1,257~5,1,259 +0,6,281~2,6,281 +2,4,282~4,4,282 +4,6,209~4,7,209 +0,0,296~0,3,296 +5,7,269~5,9,269 +7,0,285~7,0,286 +6,4,138~6,5,138 +6,2,204~9,2,204 +1,1,109~1,2,109 +6,7,96~8,7,96 +7,1,200~7,3,200 +9,0,44~9,0,47 +6,8,241~8,8,241 +2,2,9~4,2,9 +2,0,118~2,2,118 +7,7,184~7,7,187 +6,4,118~7,4,118 +3,4,268~6,4,268 +3,2,240~6,2,240 +2,1,107~2,4,107 +7,3,65~7,5,65 +6,8,136~7,8,136 +6,0,273~8,0,273 +8,4,43~8,6,43 +2,7,104~2,9,104 +8,2,190~8,4,190 +1,6,194~3,6,194 +8,2,66~8,5,66 +6,2,144~6,2,147 +7,4,94~7,7,94 +3,3,205~3,3,207 +4,1,102~4,4,102 +2,3,38~2,6,38 +8,4,118~8,7,118 +8,2,60~8,5,60 +6,6,141~6,9,141 +3,6,197~3,6,198 +1,9,235~1,9,238 +9,6,109~9,6,110 +4,4,265~4,7,265 +3,2,135~3,2,137 +1,1,115~1,1,117 +3,4,134~5,4,134 +3,2,206~5,2,206 +2,2,289~3,2,289 +5,1,21~5,5,21 +4,9,271~5,9,271 +0,8,292~2,8,292 +1,5,111~1,6,111 +3,5,176~6,5,176 +8,1,253~8,1,256 +4,0,7~5,0,7 +0,0,94~3,0,94 +5,4,96~5,5,96 +6,6,72~6,9,72 +5,8,202~6,8,202 +2,7,215~4,7,215 +8,2,231~9,2,231 +0,8,40~0,9,40 +7,1,283~7,2,283 +7,3,3~7,3,5 +3,0,292~3,0,294 +7,0,134~7,4,134 +6,4,271~9,4,271 +4,8,161~7,8,161 +2,2,33~2,4,33 +6,0,276~6,1,276 +2,6,85~2,6,86 +1,1,58~3,1,58 +1,8,199~1,9,199 +0,9,43~0,9,45 +2,3,236~5,3,236 +2,4,220~2,5,220 +5,4,266~7,4,266 +1,4,250~1,5,250 +2,0,117~4,0,117 +0,0,206~2,0,206 +7,4,98~9,4,98 +7,3,155~8,3,155 +3,4,194~4,4,194 +5,1,174~6,1,174 +1,3,212~3,3,212 +0,3,9~2,3,9 +4,4,203~4,6,203 +0,8,219~2,8,219 +7,3,296~7,5,296 +3,5,274~3,8,274 +7,4,246~8,4,246 +6,3,186~6,5,186 +9,8,174~9,9,174 +1,5,199~1,7,199 +7,0,158~7,1,158 +1,5,202~1,5,202 +6,3,118~8,3,118 +6,0,239~8,0,239 +4,1,105~4,3,105 +1,2,86~3,2,86 +0,7,219~1,7,219 +5,1,246~5,2,246 +9,2,27~9,5,27 +0,0,227~1,0,227 +3,4,204~5,4,204 +0,4,197~2,4,197 +2,7,171~2,9,171 +8,0,34~8,1,34 +8,7,159~8,9,159 +6,2,150~8,2,150 +4,2,191~4,4,191 +6,0,249~8,0,249 +0,7,176~0,9,176 +7,6,162~7,8,162 +7,0,159~9,0,159 +4,7,214~4,8,214 +6,2,118~8,2,118 +1,3,96~1,3,98 +3,6,121~4,6,121 +7,5,250~7,6,250 +9,8,189~9,8,191 +0,9,46~0,9,47 +9,1,224~9,4,224 +7,7,265~7,8,265 +1,9,234~2,9,234 +3,4,120~5,4,120 +3,2,245~5,2,245 +4,0,225~4,1,225 +7,4,205~7,6,205 +4,7,223~4,9,223 +3,1,104~3,2,104 +0,1,251~2,1,251 +5,4,262~7,4,262 +9,0,232~9,1,232 +3,5,280~5,5,280 +7,3,16~7,5,16 +1,1,192~3,1,192 +1,7,245~1,9,245 +0,1,106~3,1,106 +8,6,79~8,7,79 +6,4,188~8,4,188 +6,2,155~6,3,155 +0,3,14~1,3,14 +4,4,275~4,6,275 +2,5,131~2,6,131 +7,0,63~7,2,63 +4,3,139~4,5,139 +4,6,205~4,6,206 +4,3,226~4,3,229 +0,1,86~2,1,86 +1,5,77~2,5,77 +5,5,23~5,6,23 +8,0,162~8,0,166 +3,8,248~5,8,248 +5,1,7~7,1,7 +4,7,103~4,9,103 +6,7,223~6,9,223 +0,9,244~2,9,244 +6,0,216~6,3,216 +3,9,226~4,9,226 +2,3,152~4,3,152 +4,6,136~6,6,136 +8,7,56~9,7,56 +2,0,59~5,0,59 +9,4,8~9,6,8 +0,4,229~0,7,229 +3,1,265~3,2,265 +5,1,27~5,1,29 +5,6,282~8,6,282 +6,2,62~8,2,62 +2,8,195~3,8,195 +2,6,247~2,7,247 +8,5,50~8,7,50 +0,0,89~0,2,89 +3,4,284~5,4,284 +6,6,128~6,7,128 +9,6,15~9,6,17 +2,1,119~3,1,119 +8,1,120~8,3,120 +9,6,160~9,7,160 +5,5,43~5,7,43 +8,7,183~8,8,183 +2,7,211~4,7,211 +4,4,143~4,5,143 +3,0,290~3,3,290 +2,2,219~3,2,219 +7,4,254~9,4,254 +9,0,207~9,1,207 +6,8,260~8,8,260 +8,2,276~8,4,276 +4,4,267~6,4,267 +1,2,125~1,5,125 +4,4,77~7,4,77 +4,7,178~7,7,178 +2,2,194~3,2,194 +7,5,252~7,7,252 +5,7,263~7,7,263 +7,3,218~9,3,218 +5,2,262~6,2,262 +4,7,51~5,7,51 +2,9,256~2,9,259 +2,4,123~2,4,124 +6,5,265~6,7,265 +3,7,179~3,9,179 +4,7,167~4,8,167 +2,1,19~2,3,19 +5,6,107~9,6,107 +3,7,247~3,8,247 +9,0,30~9,2,30 +6,5,173~6,8,173 +4,8,66~7,8,66 +9,0,86~9,3,86 +9,8,3~9,8,4 +6,3,242~9,3,242 +6,1,136~8,1,136 +0,7,133~2,7,133 +4,1,32~6,1,32 +0,2,183~2,2,183 +7,3,255~9,3,255 +8,2,230~8,4,230 +2,9,3~5,9,3 +9,1,29~9,4,29 +0,9,255~2,9,255 +0,7,75~3,7,75 +8,2,3~8,2,6 +9,5,184~9,7,184 +4,1,204~5,1,204 +9,4,230~9,6,230 +1,7,141~3,7,141 +2,6,231~4,6,231 +4,0,35~7,0,35 +4,9,110~6,9,110 +5,6,62~7,6,62 +0,5,247~3,5,247 +3,2,129~5,2,129 +2,0,271~5,0,271 +0,9,242~1,9,242 +3,0,120~5,0,120 +6,4,113~8,4,113 +5,6,256~8,6,256 +2,0,166~2,4,166 +1,2,186~4,2,186 +8,5,270~8,7,270 +5,0,195~7,0,195 +4,0,273~4,0,275 +3,7,114~5,7,114 +4,2,217~4,5,217 +0,4,201~0,6,201 +0,9,204~2,9,204 +4,3,32~4,5,32 +6,6,274~6,8,274 +3,7,228~3,8,228 +8,0,161~9,0,161 +4,5,224~4,7,224 +1,0,55~1,2,55 +5,8,101~7,8,101 +9,6,19~9,6,20 +6,0,264~7,0,264 +7,7,233~7,9,233 +2,3,136~4,3,136 +6,4,156~8,4,156 +5,1,269~5,2,269 +5,8,99~5,8,99 +5,8,28~7,8,28 +7,6,190~8,6,190 +0,3,217~1,3,217 +0,1,130~0,5,130 +1,2,63~3,2,63 +2,1,232~3,1,232 +0,5,137~0,7,137 +2,1,235~2,1,237 +4,4,65~4,5,65 +5,2,24~9,2,24 +4,7,234~5,7,234 +8,6,121~8,9,121 +1,6,282~1,6,285 +0,6,131~0,6,131 +1,6,44~1,6,47 +3,7,6~6,7,6 +4,9,106~4,9,108 +0,8,147~2,8,147 +2,2,217~2,4,217 +2,3,218~4,3,218 +8,4,55~9,4,55 +7,6,180~7,7,180 +2,1,87~2,3,87 +6,5,293~8,5,293 +5,4,228~7,4,228 +3,2,249~6,2,249 +5,4,126~7,4,126 +3,8,12~5,8,12 +1,8,287~3,8,287 +7,6,227~7,9,227 +9,2,126~9,4,126 +1,3,139~1,4,139 +3,0,201~6,0,201 +3,7,13~5,7,13 +4,6,274~4,8,274 +2,6,278~2,8,278 +0,3,159~2,3,159 +6,2,81~6,3,81 +0,6,172~0,8,172 +9,0,3~9,2,3 +0,3,250~0,3,252 +5,6,80~5,8,80 +6,4,96~7,4,96 +2,1,229~2,2,229 +1,6,82~3,6,82 +2,8,105~3,8,105 +6,7,157~9,7,157 +5,7,220~8,7,220 +4,0,278~4,0,281 +2,3,157~5,3,157 +2,4,99~5,4,99 +9,2,129~9,4,129 +0,1,197~3,1,197 +1,5,56~2,5,56 +6,5,8~6,8,8 +1,5,9~1,7,9 +4,7,232~6,7,232 +5,4,25~5,6,25 +7,1,3~8,1,3 +2,2,250~2,4,250 +3,2,29~5,2,29 +3,7,284~3,8,284 +1,7,228~1,8,228 +8,5,265~8,7,265 +8,7,51~8,7,53 +4,4,12~7,4,12 +0,3,49~2,3,49 +1,4,81~1,6,81 +1,3,265~1,3,268 +9,7,186~9,9,186 +6,8,217~6,9,217 +2,1,20~2,3,20 +4,6,96~7,6,96 +5,9,135~5,9,135 +8,2,31~9,2,31 +7,2,202~7,5,202 +7,1,234~7,3,234 +3,1,143~3,3,143 +4,8,102~4,9,102 +6,3,63~6,5,63 +2,5,135~5,5,135 +3,1,268~5,1,268 +1,6,138~1,7,138 +2,7,288~3,7,288 +7,2,151~7,4,151 +9,5,168~9,7,168 +0,4,190~2,4,190 +7,2,27~7,2,29 +8,1,206~9,1,206 +6,0,135~6,3,135 +2,8,165~2,8,166 +5,0,265~6,0,265 +3,2,113~5,2,113 +7,5,212~7,7,212 +0,1,185~0,2,185 +0,1,56~0,3,56 +7,5,7~8,5,7 +5,3,59~8,3,59 +0,3,141~0,6,141 +4,5,177~7,5,177 +0,9,198~3,9,198 +7,5,227~9,5,227 +4,8,267~6,8,267 +8,7,59~8,7,61 +0,2,243~0,4,243 +1,7,80~2,7,80 +6,1,64~6,3,64 +1,6,286~1,7,286 +4,5,240~4,5,242 +6,1,277~6,3,277 +9,3,6~9,7,6 +6,9,238~8,9,238 +5,1,30~6,1,30 +6,4,272~6,6,272 +5,4,85~5,7,85 +1,4,14~1,6,14 +5,5,200~6,5,200 +6,5,183~6,9,183 +0,0,58~2,0,58 +4,1,85~4,1,87 +3,5,141~4,5,141 +5,3,277~5,3,279 +7,6,51~7,8,51 +3,9,130~5,9,130 +5,1,84~5,3,84 +9,4,277~9,6,277 +9,0,87~9,2,87 +1,3,149~3,3,149 +2,4,40~4,4,40 +8,5,213~8,8,213 +6,4,289~9,4,289 +0,3,193~2,3,193 +9,7,171~9,9,171 +7,9,134~9,9,134 +1,7,218~2,7,218 +2,6,36~6,6,36 +0,8,148~3,8,148 +3,8,176~6,8,176 +8,6,69~8,7,69 +6,5,6~6,6,6 +0,1,54~0,3,54 +2,1,60~2,3,60 +1,1,205~5,1,205 +2,6,58~2,8,58 +2,7,170~3,7,170 +6,7,98~6,7,100 +5,0,33~7,0,33 +2,5,179~4,5,179 +1,0,83~1,2,83 +4,2,211~6,2,211 +0,0,239~0,2,239 +3,7,115~3,8,115 +3,7,165~6,7,165 +7,0,32~9,0,32 +0,7,106~3,7,106 +0,6,6~0,8,6 +0,0,157~0,1,157 +6,8,138~8,8,138 +8,8,3~8,9,3 +8,2,63~8,3,63 +1,1,111~1,1,114 +4,2,12~6,2,12 +0,7,169~1,7,169 +6,0,2~7,0,2 +7,7,70~8,7,70 +1,0,111~2,0,111 +0,5,253~2,5,253 +2,3,122~2,6,122 +6,5,53~6,7,53 +6,4,261~6,5,261 +2,2,283~4,2,283 +2,5,228~2,6,228 +4,5,225~6,5,225 +1,6,136~1,8,136 +7,5,110~7,6,110 +8,0,281~8,0,283 +2,1,24~5,1,24 +0,1,200~0,4,200 +5,7,284~7,7,284 +3,3,49~3,3,50 +5,3,293~5,5,293 +5,5,180~8,5,180 +1,1,81~3,1,81 +2,2,285~2,2,287 +1,6,288~3,6,288 +9,1,47~9,1,50 +2,1,189~4,1,189 +1,3,140~3,3,140 +4,4,121~4,4,123 +2,7,31~2,7,32 +8,7,268~8,7,268 +2,5,215~5,5,215 +9,0,42~9,2,42 +6,4,206~9,4,206 +4,6,169~4,7,169 +3,9,239~7,9,239 +5,5,210~8,5,210 +5,7,180~5,7,182 +4,0,251~4,3,251 +8,1,205~9,1,205 +3,7,53~3,9,53 +1,8,1~4,8,1 +2,5,207~5,5,207 +5,6,207~7,6,207 +0,6,224~0,7,224 +0,3,62~0,4,62 +9,5,1~9,5,3 +4,9,126~7,9,126 +0,6,8~0,8,8 +7,4,257~7,4,260 +2,4,142~2,4,144 +2,1,270~2,3,270 +0,4,56~1,4,56 +4,4,224~6,4,224 +8,3,231~8,4,231 +4,2,214~4,3,214 +5,1,133~7,1,133 +5,6,126~7,6,126 +4,1,5~7,1,5 +1,2,202~1,2,204 +5,7,292~7,7,292 +7,0,47~7,1,47 +3,5,285~5,5,285 +6,6,121~6,9,121 +3,4,106~3,6,106 +0,4,121~1,4,121 +1,7,244~3,7,244 +1,1,249~4,1,249 +1,2,76~1,4,76 +5,5,171~7,5,171 +8,2,78~8,2,80 +8,3,12~8,4,12 +0,0,97~0,0,100 +1,0,114~2,0,114 +0,0,122~2,0,122 +2,5,129~4,5,129 +1,4,137~3,4,137 +9,2,123~9,4,123 +3,4,290~5,4,290 +9,5,72~9,6,72 +0,7,170~0,8,170 +7,1,226~7,3,226 +2,4,81~2,6,81 +0,6,72~3,6,72 +6,3,145~6,5,145 +5,6,77~5,8,77 +0,0,237~0,3,237 +3,4,271~4,4,271 +4,4,60~6,4,60 +8,4,116~8,7,116 +5,5,223~8,5,223 +6,1,251~6,4,251 +2,9,182~5,9,182 +2,0,163~2,1,163 +8,0,12~8,1,12 +1,6,289~1,8,289 +4,7,116~5,7,116 +3,9,73~3,9,74 +4,4,67~6,4,67 +1,0,3~3,0,3 +3,1,46~3,4,46 +0,5,8~3,5,8 +1,2,53~4,2,53 +1,9,65~3,9,65 +8,2,162~8,3,162 +7,7,77~9,7,77 +6,4,197~6,7,197 +6,0,145~8,0,145 +6,4,269~9,4,269 +0,4,127~0,7,127 +8,5,267~8,8,267 +6,0,236~8,0,236 +4,0,56~4,3,56 +0,6,202~0,7,202 +6,3,3~6,5,3 +2,7,63~2,9,63 +3,5,73~3,8,73 +3,3,210~4,3,210 +4,1,114~4,1,115 +6,0,79~6,2,79 +0,2,221~1,2,221 +7,9,277~7,9,278 +2,4,72~5,4,72 +7,9,129~9,9,129 +0,4,196~0,6,196 +0,3,194~0,5,194 +4,7,67~4,9,67 +3,6,261~5,6,261 +3,0,256~3,1,256 +3,0,228~5,0,228 +1,1,252~3,1,252 +2,6,242~2,7,242 +2,1,167~3,1,167 +5,3,11~5,5,11 +7,3,223~9,3,223 +7,6,235~7,8,235 +1,9,66~2,9,66 +2,6,248~5,6,248 +5,2,76~5,5,76 +9,6,21~9,6,23 +2,7,2~2,9,2 +4,4,198~4,6,198 +5,8,199~5,9,199 +0,1,184~0,3,184 +0,1,293~3,1,293 +1,3,74~1,6,74 +4,4,201~5,4,201 +0,1,160~2,1,160 +9,0,41~9,3,41 +4,2,284~4,3,284 +2,0,61~5,0,61 +0,6,276~3,6,276 +6,7,237~8,7,237 +2,6,71~5,6,71 +5,4,287~6,4,287 +3,5,68~3,6,68 +3,7,201~5,7,201 +3,8,48~5,8,48 +4,4,86~7,4,86 +3,5,235~3,7,235 +4,2,152~8,2,152 +6,4,135~8,4,135 +6,2,85~8,2,85 +7,0,248~7,3,248 +0,3,59~0,4,59 +7,9,280~9,9,280 +8,2,164~8,3,164 +2,7,280~2,7,281 +0,6,130~0,8,130 +4,1,175~6,1,175 +1,2,124~1,4,124 +0,8,10~0,9,10 +3,3,117~3,6,117 +0,5,142~0,5,143 +6,6,27~6,8,27 +0,6,250~2,6,250 +1,8,227~1,9,227 +1,8,4~1,8,6 +0,8,107~2,8,107 +6,3,265~8,3,265 +3,6,237~6,6,237 +3,5,192~3,8,192 +6,0,8~6,1,8 +6,0,263~6,3,263 +5,7,196~5,9,196 +6,6,159~6,8,159 +5,3,248~5,5,248 +1,8,99~4,8,99 +5,5,129~6,5,129 +8,4,224~8,4,227 +0,1,154~0,4,154 +7,5,224~9,5,224 +2,7,158~5,7,158 +5,3,205~7,3,205 +2,6,144~2,8,144 +6,4,47~6,7,47 +5,3,275~5,6,275 +2,5,27~6,5,27 +8,0,139~9,0,139 +6,5,139~8,5,139 +8,3,219~9,3,219 +6,2,29~6,4,29 +2,1,187~2,4,187 +5,6,93~8,6,93 +6,5,142~8,5,142 +2,7,49~5,7,49 +6,2,250~8,2,250 +4,0,83~6,0,83 +6,0,280~9,0,280 +4,7,44~6,7,44 +1,0,264~1,3,264 +0,3,255~0,4,255 +6,6,1~8,6,1 +2,5,239~2,8,239 +7,3,221~7,6,221 +8,0,102~8,2,102 +7,7,182~9,7,182 +1,3,123~3,3,123 +0,7,135~2,7,135 +1,7,78~3,7,78 +4,5,196~7,5,196 +1,2,296~4,2,296 +0,1,21~3,1,21 +8,7,67~8,9,67 +5,9,185~7,9,185 +2,5,227~2,8,227 +9,4,275~9,7,275 +4,3,231~4,3,233 +9,3,131~9,4,131 +4,2,108~6,2,108 +7,6,209~7,6,210 +4,5,204~6,5,204 +7,4,52~7,6,52 +6,0,193~6,2,193 +8,7,215~8,8,215 +4,4,28~6,4,28 +2,3,45~2,5,45 +1,4,188~2,4,188 +0,2,269~0,4,269 +9,2,130~9,4,130 +9,4,263~9,6,263 +8,5,263~8,8,263 +7,3,42~7,6,42 +5,1,173~5,5,173 +3,3,227~3,6,227 +2,2,208~4,2,208 +3,2,220~4,2,220 +1,0,229~2,0,229 +5,0,279~7,0,279 +6,6,104~7,6,104 +1,2,26~5,2,26 +8,1,259~8,4,259 +6,7,49~8,7,49 +1,5,48~1,7,48 +6,3,82~9,3,82 +6,6,175~8,6,175 +2,0,36~2,2,36 +4,9,288~4,9,289 +0,6,5~2,6,5 +3,7,160~3,7,162 +8,3,125~8,4,125 +8,2,75~8,3,75 +0,5,279~0,7,279 +6,5,120~6,6,120 +8,7,158~9,7,158 +5,6,189~8,6,189 +8,1,25~8,2,25 +0,0,91~2,0,91 +2,3,77~5,3,77 +1,3,214~3,3,214 +2,7,155~5,7,155 +5,1,252~5,4,252 +2,8,102~2,9,102 +7,1,44~7,3,44 +5,6,174~8,6,174 +7,4,291~7,7,291 +2,7,55~2,8,55 +3,1,253~3,1,253 +4,9,274~7,9,274 +3,6,272~5,6,272 +6,4,89~9,4,89 +6,5,75~8,5,75 +4,5,245~5,5,245 +4,1,255~4,4,255 +1,8,248~1,8,249 +0,7,253~0,9,253 +3,4,29~4,4,29 +8,0,9~8,2,9 \ No newline at end of file diff --git a/day-22/src/bin/part1.rs b/day-22/src/bin/part1.rs new file mode 100644 index 0000000..fa0c290 --- /dev/null +++ b/day-22/src/bin/part1.rs @@ -0,0 +1,125 @@ +use std::collections::{BTreeMap, BTreeSet}; + +use itertools::Itertools; + +fn main() { + println!("{}", part1(include_str!("./input.txt"))); +} + +fn part1(input: &str) -> usize { + let mut bricks = input + .lines() + .map(|line| { + let mut ends = line.split('~').map(|part| { + let mut coords = part.splitn(3, ','); + let x = coords.next().unwrap().parse::().unwrap(); + let y = coords.next().unwrap().parse::().unwrap(); + let z = coords.next().unwrap().parse::().unwrap(); + + (x, y, z) + }); + let start = ends.next().unwrap(); + let end = ends.next().unwrap(); + + Brick::new(start, end) + }) + .sorted_by_key(|brick| brick.z.0) + .collect::>(); + + // simulate bricks falling + for index in 0..bricks.len() { + let mut max_z = 1; + for check in &bricks[..index] { + if check.overlaps(bricks.get(index).unwrap()) { + max_z = max_z.max(check.z.1 + 1); + } + } + let brick = bricks.get_mut(index).unwrap(); + brick.z.1 -= brick.z.0 - max_z; + brick.z.0 = max_z; + } + bricks.sort_by_key(|brick| brick.z.0); + + let mut supports = (0..bricks.len()) + .map(|i| (i, BTreeSet::new())) + .collect::>(); + let mut supported_by = (0..bricks.len()) + .map(|i| (i, BTreeSet::new())) + .collect::>(); + + // find all bricks that support other bricks + for (j, upper) in bricks.iter().enumerate() { + for (i, lower) in bricks[..j].iter().enumerate() { + if upper.overlaps(lower) && upper.z.0 == lower.z.1 + 1 { + supports.get_mut(&i).unwrap().insert(j); + supported_by.get_mut(&j).unwrap().insert(i); + } + } + } + + // count all the bricks for which all the bricks that + // it supports have more than one supporter + supports + .values() + .filter(|supported| { + supported + .iter() + .all(|brick| supported_by.get(brick).unwrap().len() > 1) + }) + .count() +} + +#[derive(Debug, Clone, Copy)] +struct Brick { + x: (u16, u16), + y: (u16, u16), + z: (u16, u16), +} +impl Brick { + fn new(start: (u16, u16, u16), end: (u16, u16, u16)) -> Self { + Self { + x: (start.0, end.0), + y: (start.1, end.1), + z: (start.2, end.2), + } + } + + fn overlaps(&self, other: &Self) -> bool { + self.x.1 >= other.x.0 + && other.x.1 >= self.x.0 + && self.y.1 >= other.y.0 + && other.y.1 >= self.y.0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + #[test] + fn test_part1() { + assert_eq!( + part1(indoc!( + " + 1,0,1~1,2,1 + 0,0,2~2,0,2 + 0,2,3~2,2,3 + 0,0,4~0,2,4 + 2,0,5~2,2,5 + 0,1,6~2,1,6 + 1,1,8~1,1,9 + " + )), + 5 + ); + } + + #[test] + fn test_overlap() { + let brick_a = Brick::new((0, 0, 0), (2, 1, 1)); + let brick_b = Brick::new((1, 0, 0), (1, 1, 1)); + + assert!(brick_a.overlaps(&brick_b)); + } +} diff --git a/day-22/src/bin/part2.rs b/day-22/src/bin/part2.rs new file mode 100644 index 0000000..b72834a --- /dev/null +++ b/day-22/src/bin/part2.rs @@ -0,0 +1,150 @@ +use std::collections::{BTreeMap, BTreeSet}; + +use itertools::Itertools; + +fn main() { + println!("{}", part2(include_str!("./input.txt"))); +} + +fn part2(input: &str) -> usize { + let mut bricks = input + .lines() + .map(|line| { + let mut ends = line.split('~').map(|part| { + let mut coords = part.splitn(3, ','); + let x = coords.next().unwrap().parse::().unwrap(); + let y = coords.next().unwrap().parse::().unwrap(); + let z = coords.next().unwrap().parse::().unwrap(); + + (x, y, z) + }); + let start = ends.next().unwrap(); + let end = ends.next().unwrap(); + + Brick::new(start, end) + }) + .sorted_by_key(|brick| brick.z.0) + .collect::>(); + + // simulate bricks falling + for index in 0..bricks.len() { + let mut max_z = 1; + for check in &bricks[..index] { + if check.overlaps(bricks.get(index).unwrap()) { + max_z = max_z.max(check.z.1 + 1); + } + } + let brick = bricks.get_mut(index).unwrap(); + brick.z.1 -= brick.z.0 - max_z; + brick.z.0 = max_z; + } + bricks.sort_by_key(|brick| brick.z.0); + + let mut supports = (0..bricks.len()) + .map(|i| (i, BTreeSet::new())) + .collect::>(); + let mut supported_by = (0..bricks.len()) + .map(|i| (i, BTreeSet::new())) + .collect::>(); + + // find all bricks that support other bricks + for (j, upper) in bricks.iter().enumerate() { + for (i, lower) in bricks[..j].iter().enumerate() { + if upper.overlaps(lower) && upper.z.0 == lower.z.1 + 1 { + supports.get_mut(&i).unwrap().insert(j); + supported_by.get_mut(&j).unwrap().insert(i); + } + } + } + + supports + .keys() + .map(|index| { + let mut supported_by = supported_by.clone(); + disintigrate(*index, &supports, &mut supported_by) + }) + .sum::() +} + +fn disintigrate( + index: usize, + supports: &BTreeMap>, + supported_by: &mut BTreeMap>, +) -> usize { + supports + .get(&index) + .iter() + .map(|supported_bricks| { + supported_bricks + .iter() + .map(|supported_brick| { + supported_by + .get_mut(supported_brick) + .unwrap() + .remove(&index); + let supporting_bricks = supported_by.get(supported_brick).unwrap(); + if !supporting_bricks.is_empty() { + 0 + } else { + 1 + disintigrate(*supported_brick, supports, supported_by) + } + }) + .sum::() + }) + .sum() +} + +#[derive(Debug, Clone, Copy)] +struct Brick { + x: (u16, u16), + y: (u16, u16), + z: (u16, u16), +} +impl Brick { + fn new(start: (u16, u16, u16), end: (u16, u16, u16)) -> Self { + Self { + x: (start.0, end.0), + y: (start.1, end.1), + z: (start.2, end.2), + } + } + + fn overlaps(&self, other: &Self) -> bool { + self.x.1 >= other.x.0 + && other.x.1 >= self.x.0 + && self.y.1 >= other.y.0 + && other.y.1 >= self.y.0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + #[test] + fn test_part2() { + assert_eq!( + part2(indoc!( + " + 1,0,1~1,2,1 + 0,0,2~2,0,2 + 0,2,3~2,2,3 + 0,0,4~0,2,4 + 2,0,5~2,2,5 + 0,1,6~2,1,6 + 1,1,8~1,1,9 + " + )), + 7 + ); + } + + #[test] + fn test_overlap() { + let brick_a = Brick::new((0, 0, 0), (2, 1, 1)); + let brick_b = Brick::new((1, 0, 0), (1, 1, 1)); + + assert!(brick_a.overlaps(&brick_b)); + } +}