From e1d2a785f526e84a82dbbf225a681910ee5c1edc Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sat, 23 Dec 2023 03:53:04 +0100 Subject: [PATCH] 2023 - Day 22 --- 2023/input/22/input | 1446 +++++++++++++++++++++++++++++++++++++++++ 2023/input/22/test-1 | 7 + 2023/src/bin/day22.rs | 229 +++++++ 3 files changed, 1682 insertions(+) create mode 100644 2023/input/22/input create mode 100644 2023/input/22/test-1 create mode 100644 2023/src/bin/day22.rs diff --git a/2023/input/22/input b/2023/input/22/input new file mode 100644 index 0000000..746d1de --- /dev/null +++ b/2023/input/22/input @@ -0,0 +1,1446 @@ +5,0,238~8,0,238 +2,4,126~5,4,126 +0,4,224~0,6,224 +0,0,294~0,1,294 +4,7,326~4,8,326 +7,3,37~9,3,37 +7,9,154~7,9,156 +6,0,210~6,2,210 +5,7,36~8,7,36 +5,2,76~5,5,76 +6,0,12~8,0,12 +3,1,159~5,1,159 +9,7,168~9,9,168 +7,6,213~9,6,213 +3,3,63~3,5,63 +3,4,69~5,4,69 +5,6,67~6,6,67 +9,0,184~9,2,184 +0,2,122~0,5,122 +7,9,101~8,9,101 +1,5,191~3,5,191 +1,8,287~3,8,287 +3,5,263~3,7,263 +9,5,287~9,7,287 +1,1,217~4,1,217 +2,0,286~2,2,286 +1,0,72~1,1,72 +3,2,115~3,5,115 +5,6,5~5,8,5 +1,7,230~1,9,230 +1,0,216~3,0,216 +5,8,151~5,9,151 +2,9,15~6,9,15 +3,1,305~6,1,305 +5,0,3~8,0,3 +4,8,146~6,8,146 +4,9,19~4,9,21 +1,4,107~4,4,107 +4,4,326~4,6,326 +2,4,286~2,6,286 +8,7,146~9,7,146 +2,9,9~4,9,9 +4,6,243~4,8,243 +5,7,300~8,7,300 +8,7,7~8,8,7 +4,3,167~4,7,167 +4,7,242~6,7,242 +6,3,186~9,3,186 +2,1,252~2,3,252 +2,3,47~4,3,47 +6,7,100~6,9,100 +1,4,147~4,4,147 +5,9,240~7,9,240 +3,2,59~5,2,59 +1,8,148~1,8,151 +1,2,42~3,2,42 +3,1,52~5,1,52 +7,1,69~7,3,69 +3,1,31~3,3,31 +2,9,319~4,9,319 +5,2,24~8,2,24 +5,4,252~7,4,252 +1,4,313~4,4,313 +5,6,223~7,6,223 +7,3,82~7,6,82 +7,2,216~7,4,216 +5,7,34~7,7,34 +5,4,66~7,4,66 +4,1,256~4,3,256 +8,6,125~8,8,125 +3,6,301~6,6,301 +3,6,246~5,6,246 +2,6,210~2,9,210 +0,6,108~1,6,108 +2,5,137~2,8,137 +6,3,147~8,3,147 +8,3,201~8,5,201 +0,5,228~0,6,228 +1,5,214~1,7,214 +9,8,171~9,8,174 +4,5,324~4,8,324 +2,5,195~5,5,195 +3,1,131~4,1,131 +1,2,150~1,4,150 +5,4,37~5,7,37 +8,6,3~8,7,3 +3,6,47~3,7,47 +0,5,62~0,6,62 +4,0,87~4,2,87 +6,4,59~6,7,59 +2,2,93~2,2,95 +6,5,85~6,7,85 +3,3,16~5,3,16 +3,2,269~5,2,269 +0,9,159~0,9,160 +1,7,17~1,8,17 +2,0,19~2,2,19 +1,4,159~2,4,159 +4,4,294~4,6,294 +8,9,16~9,9,16 +7,5,151~7,6,151 +5,8,269~8,8,269 +0,2,212~3,2,212 +2,7,229~2,9,229 +7,6,199~7,8,199 +0,1,254~3,1,254 +6,3,196~6,5,196 +7,0,49~7,2,49 +7,4,107~7,4,108 +6,9,180~8,9,180 +9,3,2~9,5,2 +9,3,226~9,3,228 +1,8,20~3,8,20 +3,2,219~3,5,219 +2,1,42~4,1,42 +2,4,74~6,4,74 +8,3,61~8,5,61 +6,4,318~9,4,318 +0,0,214~3,0,214 +2,2,22~2,3,22 +2,7,75~2,8,75 +4,7,22~6,7,22 +0,2,120~3,2,120 +0,4,237~2,4,237 +8,6,217~8,8,217 +9,3,110~9,3,112 +4,5,119~4,7,119 +5,9,310~8,9,310 +0,1,263~0,3,263 +4,1,266~4,2,266 +9,5,73~9,6,73 +3,8,167~5,8,167 +4,3,50~6,3,50 +7,5,212~9,5,212 +5,7,38~6,7,38 +7,5,48~7,7,48 +8,6,234~8,6,235 +0,6,152~2,6,152 +9,1,186~9,1,188 +7,3,121~7,3,123 +8,4,231~8,6,231 +2,0,310~2,3,310 +4,4,304~5,4,304 +4,0,241~6,0,241 +3,0,271~3,3,271 +1,1,98~1,4,98 +4,2,223~5,2,223 +1,2,289~2,2,289 +0,6,244~0,8,244 +0,7,79~0,9,79 +8,2,274~8,4,274 +5,8,299~7,8,299 +3,4,173~4,4,173 +7,8,228~8,8,228 +3,3,254~3,5,254 +6,2,234~6,4,234 +2,7,98~3,7,98 +6,1,221~8,1,221 +4,2,140~7,2,140 +4,0,115~4,0,118 +9,7,148~9,8,148 +7,7,178~7,9,178 +5,7,266~5,8,266 +7,7,81~7,9,81 +2,7,175~3,7,175 +0,9,200~3,9,200 +0,4,281~0,6,281 +1,4,153~2,4,153 +1,5,265~1,5,267 +8,2,252~9,2,252 +5,1,302~5,1,303 +2,0,111~2,0,112 +9,5,267~9,8,267 +1,1,119~4,1,119 +7,0,70~9,0,70 +4,5,285~6,5,285 +7,1,182~9,1,182 +6,2,228~6,2,230 +6,5,208~6,8,208 +1,2,102~1,4,102 +1,9,90~2,9,90 +1,0,98~1,0,99 +0,3,223~0,5,223 +9,5,280~9,7,280 +1,0,197~3,0,197 +8,0,263~8,3,263 +4,8,195~4,8,197 +5,2,312~7,2,312 +0,4,64~0,6,64 +5,4,131~6,4,131 +1,4,80~1,4,83 +3,2,315~5,2,315 +2,1,189~2,1,190 +1,3,251~4,3,251 +5,6,240~7,6,240 +0,5,147~3,5,147 +8,2,139~8,4,139 +5,6,3~5,7,3 +3,1,35~5,1,35 +8,4,104~8,6,104 +7,5,206~7,7,206 +5,5,78~7,5,78 +5,0,218~6,0,218 +4,7,135~4,7,138 +8,8,186~9,8,186 +0,0,285~0,1,285 +8,1,277~8,3,277 +1,7,166~1,8,166 +9,5,216~9,7,216 +3,5,251~5,5,251 +1,2,103~3,2,103 +2,2,156~2,4,156 +4,5,54~4,7,54 +0,3,257~0,4,257 +0,6,231~0,8,231 +5,4,249~5,6,249 +3,7,241~3,9,241 +5,7,276~5,8,276 +4,4,168~7,4,168 +8,2,64~8,2,66 +6,7,88~6,9,88 +1,1,111~1,4,111 +1,1,118~2,1,118 +0,0,222~2,0,222 +5,4,132~5,4,134 +0,5,200~0,6,200 +3,8,67~3,8,69 +4,6,185~6,6,185 +2,0,149~5,0,149 +8,7,38~8,8,38 +5,5,64~5,7,64 +5,1,76~6,1,76 +3,9,280~6,9,280 +8,8,185~9,8,185 +0,6,1~0,6,2 +0,6,22~0,8,22 +1,5,305~1,8,305 +9,0,295~9,3,295 +1,4,201~1,6,201 +9,7,275~9,9,275 +7,4,103~8,4,103 +7,8,39~7,9,39 +3,6,16~5,6,16 +0,7,232~0,9,232 +3,2,307~5,2,307 +4,5,91~4,8,91 +3,7,236~6,7,236 +4,4,117~7,4,117 +2,8,139~2,9,139 +7,3,292~9,3,292 +7,0,106~7,3,106 +7,0,96~7,2,96 +9,8,303~9,8,304 +7,3,56~7,5,56 +2,5,202~2,5,202 +2,0,211~2,4,211 +2,9,214~2,9,216 +0,5,95~3,5,95 +2,7,227~3,7,227 +6,6,58~6,8,58 +1,1,256~1,1,257 +0,7,148~0,8,148 +2,8,70~4,8,70 +4,1,154~4,2,154 +2,8,23~3,8,23 +6,6,132~6,7,132 +8,4,198~8,6,198 +7,3,172~7,5,172 +9,8,14~9,8,16 +0,0,231~2,0,231 +3,6,221~5,6,221 +6,7,254~6,8,254 +6,3,183~6,5,183 +3,6,43~3,9,43 +5,4,128~5,7,128 +7,7,280~8,7,280 +2,5,24~5,5,24 +0,3,261~0,6,261 +1,1,1~3,1,1 +3,0,282~4,0,282 +5,8,152~6,8,152 +8,0,224~8,3,224 +5,1,90~6,1,90 +2,4,244~5,4,244 +4,1,273~6,1,273 +6,2,172~6,4,172 +6,0,294~8,0,294 +5,1,74~8,1,74 +1,6,165~1,8,165 +1,0,316~3,0,316 +5,5,141~6,5,141 +5,3,73~6,3,73 +9,0,298~9,3,298 +3,1,129~3,3,129 +0,7,134~3,7,134 +2,1,124~2,4,124 +6,7,320~7,7,320 +2,0,92~2,4,92 +1,4,219~2,4,219 +3,7,199~3,9,199 +3,3,193~3,5,193 +3,7,114~5,7,114 +6,0,214~8,0,214 +5,0,297~6,0,297 +2,4,24~5,4,24 +2,6,149~2,7,149 +2,3,30~4,3,30 +6,2,104~6,3,104 +2,1,203~6,1,203 +9,7,6~9,9,6 +1,1,114~2,1,114 +9,0,225~9,0,225 +3,5,154~3,6,154 +5,4,38~5,6,38 +1,5,122~1,7,122 +0,3,14~0,5,14 +1,2,279~1,5,279 +0,4,71~0,6,71 +5,2,300~5,3,300 +9,0,6~9,4,6 +7,7,87~7,9,87 +4,8,257~4,9,257 +2,8,191~2,9,191 +0,8,281~3,8,281 +1,7,233~1,9,233 +6,6,249~6,9,249 +5,5,108~5,5,110 +4,0,235~6,0,235 +6,4,304~6,6,304 +3,5,276~6,5,276 +6,0,318~8,0,318 +8,2,14~8,4,14 +2,7,258~2,8,258 +0,0,288~0,2,288 +6,0,13~8,0,13 +3,2,93~3,5,93 +7,5,269~9,5,269 +0,8,147~2,8,147 +5,0,187~5,2,187 +0,1,124~0,3,124 +2,4,233~5,4,233 +4,5,144~5,5,144 +6,5,253~6,7,253 +9,2,254~9,3,254 +4,3,285~5,3,285 +2,5,236~4,5,236 +1,8,227~4,8,227 +8,4,262~8,6,262 +2,4,193~2,6,193 +4,5,134~4,8,134 +4,9,252~6,9,252 +2,9,88~2,9,89 +2,1,183~2,4,183 +7,4,52~8,4,52 +7,1,7~9,1,7 +6,3,175~6,5,175 +4,2,82~6,2,82 +7,3,221~7,5,221 +6,8,34~6,9,34 +1,8,283~3,8,283 +6,0,224~6,2,224 +4,2,178~7,2,178 +4,0,210~4,2,210 +5,6,272~5,9,272 +9,1,53~9,3,53 +2,4,227~4,4,227 +0,0,218~0,0,219 +5,6,264~6,6,264 +5,3,36~8,3,36 +3,1,257~3,1,257 +9,3,69~9,6,69 +1,0,6~3,0,6 +2,6,190~3,6,190 +0,4,227~0,5,227 +6,0,320~6,2,320 +0,8,247~2,8,247 +5,3,212~5,6,212 +5,1,299~7,1,299 +6,7,279~7,7,279 +9,1,15~9,1,16 +5,0,85~5,2,85 +0,2,46~0,4,46 +1,1,34~3,1,34 +6,1,281~6,4,281 +9,3,246~9,3,249 +5,0,216~6,0,216 +5,0,233~5,3,233 +6,1,211~7,1,211 +6,6,317~6,7,317 +9,3,251~9,5,251 +2,1,112~2,4,112 +6,5,190~6,8,190 +2,1,70~4,1,70 +5,9,152~5,9,154 +5,3,9~5,5,9 +4,4,199~7,4,199 +4,1,317~7,1,317 +1,6,222~1,8,222 +7,4,89~9,4,89 +2,1,149~5,1,149 +7,8,225~7,9,225 +0,9,125~0,9,129 +5,5,79~5,7,79 +2,1,126~4,1,126 +1,2,45~4,2,45 +1,7,268~4,7,268 +5,3,246~5,5,246 +2,2,302~2,5,302 +3,4,184~5,4,184 +9,3,243~9,6,243 +1,3,2~1,5,2 +1,9,48~2,9,48 +8,4,123~8,7,123 +8,1,62~8,3,62 +7,5,235~9,5,235 +3,4,110~5,4,110 +0,5,100~0,6,100 +6,6,230~9,6,230 +8,0,234~8,1,234 +8,6,56~8,9,56 +8,4,303~8,7,303 +2,3,75~4,3,75 +9,5,4~9,6,4 +0,1,115~2,1,115 +6,1,214~9,1,214 +5,1,227~5,1,229 +9,5,3~9,7,3 +3,9,242~5,9,242 +0,5,141~0,7,141 +6,2,315~6,3,315 +5,1,40~5,1,43 +7,0,293~7,2,293 +4,2,250~5,2,250 +7,4,250~7,5,250 +0,6,158~0,9,158 +0,4,105~2,4,105 +6,5,274~6,7,274 +2,7,114~2,9,114 +2,2,145~4,2,145 +3,0,200~3,2,200 +6,8,76~8,8,76 +2,3,162~2,5,162 +3,7,93~3,9,93 +5,3,306~8,3,306 +1,6,81~1,6,85 +1,0,69~1,3,69 +8,2,28~8,4,28 +6,2,182~6,2,184 +5,5,69~5,7,69 +7,9,312~8,9,312 +2,3,48~2,5,48 +7,6,152~7,9,152 +7,3,175~7,3,175 +4,3,135~4,5,135 +7,0,291~7,1,291 +1,9,243~3,9,243 +8,9,105~8,9,107 +2,5,209~2,8,209 +6,9,84~8,9,84 +1,2,263~1,5,263 +6,1,64~9,1,64 +0,7,287~0,8,287 +7,5,265~9,5,265 +6,4,94~6,5,94 +4,0,124~6,0,124 +0,1,283~0,4,283 +0,0,19~0,0,19 +1,7,271~3,7,271 +0,6,253~3,6,253 +7,6,54~9,6,54 +6,3,86~6,3,89 +1,5,5~3,5,5 +3,4,238~3,8,238 +0,9,28~2,9,28 +7,4,178~7,4,181 +5,5,258~5,8,258 +6,1,215~6,2,215 +0,6,67~2,6,67 +7,8,271~7,9,271 +5,8,22~5,9,22 +3,6,125~3,8,125 +0,7,28~1,7,28 +5,8,177~7,8,177 +5,5,112~7,5,112 +7,8,34~9,8,34 +8,5,232~9,5,232 +4,0,271~4,2,271 +7,6,50~9,6,50 +9,5,285~9,7,285 +5,2,309~5,3,309 +5,5,261~6,5,261 +3,3,42~3,6,42 +0,8,72~2,8,72 +0,4,228~1,4,228 +6,7,219~7,7,219 +4,5,207~4,7,207 +1,0,282~1,3,282 +1,6,228~1,9,228 +2,1,71~2,1,73 +4,2,48~7,2,48 +0,5,242~0,6,242 +5,2,22~5,4,22 +6,0,97~9,0,97 +1,7,264~3,7,264 +1,7,111~4,7,111 +6,4,248~8,4,248 +5,7,55~5,8,55 +0,5,152~3,5,152 +4,3,240~7,3,240 +0,5,50~0,6,50 +8,3,136~8,6,136 +3,9,23~3,9,26 +7,1,30~9,1,30 +1,7,191~2,7,191 +2,8,86~2,9,86 +3,1,225~6,1,225 +7,5,284~8,5,284 +1,0,102~1,0,102 +9,4,56~9,6,56 +5,3,12~5,5,12 +6,5,178~6,5,181 +6,5,237~8,5,237 +1,0,309~1,1,309 +4,2,54~4,4,54 +4,9,220~5,9,220 +9,7,7~9,7,10 +2,1,57~3,1,57 +8,8,271~8,9,271 +6,5,120~6,7,120 +1,9,223~4,9,223 +8,4,271~8,4,273 +9,0,33~9,1,33 +2,6,138~4,6,138 +3,5,61~3,7,61 +1,3,303~3,3,303 +3,0,188~3,4,188 +3,7,169~5,7,169 +4,6,206~6,6,206 +5,2,303~5,6,303 +3,1,128~3,3,128 +7,3,52~9,3,52 +5,0,311~8,0,311 +6,3,56~6,4,56 +6,1,228~7,1,228 +5,6,321~5,8,321 +1,5,250~1,8,250 +3,6,169~5,6,169 +7,6,84~7,8,84 +5,9,184~5,9,185 +0,6,153~0,8,153 +4,4,265~4,6,265 +5,8,58~5,8,61 +6,4,193~6,6,193 +5,3,156~5,5,156 +5,0,226~6,0,226 +2,6,267~2,8,267 +2,6,231~5,6,231 +3,0,185~3,3,185 +2,3,85~4,3,85 +5,9,223~7,9,223 +1,3,105~1,3,105 +7,6,46~8,6,46 +0,4,17~3,4,17 +1,2,261~4,2,261 +1,4,53~1,7,53 +6,7,3~7,7,3 +8,0,312~8,1,312 +3,5,230~3,7,230 +6,1,85~6,3,85 +3,4,196~3,5,196 +0,0,102~0,3,102 +0,5,103~0,5,105 +0,1,253~2,1,253 +3,6,6~3,8,6 +4,3,262~4,5,262 +4,4,127~4,6,127 +5,0,122~5,2,122 +7,4,30~9,4,30 +5,9,279~8,9,279 +6,6,5~8,6,5 +8,7,82~8,8,82 +1,8,22~1,8,24 +8,2,283~8,4,283 +2,4,229~4,4,229 +9,6,281~9,8,281 +7,0,300~7,3,300 +4,9,16~6,9,16 +4,7,27~4,9,27 +5,2,107~5,5,107 +4,3,292~4,5,292 +5,5,277~7,5,277 +6,8,94~9,8,94 +3,5,260~6,5,260 +3,9,87~6,9,87 +6,3,242~9,3,242 +0,5,143~0,8,143 +6,4,156~6,5,156 +2,7,78~2,7,81 +2,5,57~2,5,59 +5,0,87~5,2,87 +0,2,82~3,2,82 +2,0,22~2,0,23 +1,8,258~1,8,260 +6,2,142~6,3,142 +3,5,232~5,5,232 +5,0,116~5,3,116 +5,3,174~5,6,174 +7,6,8~7,8,8 +3,3,257~3,4,257 +2,4,185~2,5,185 +3,0,39~3,2,39 +3,4,1~5,4,1 +1,4,90~4,4,90 +3,2,259~5,2,259 +9,1,223~9,3,223 +7,4,149~7,7,149 +3,0,219~4,0,219 +4,2,279~4,4,279 +5,7,41~5,9,41 +7,0,180~8,0,180 +8,2,204~8,4,204 +4,0,110~4,3,110 +2,6,25~4,6,25 +7,7,304~8,7,304 +2,2,221~5,2,221 +0,0,93~2,0,93 +3,5,99~3,6,99 +5,2,279~5,2,282 +7,1,73~7,3,73 +5,0,46~5,1,46 +1,1,284~2,1,284 +6,5,74~8,5,74 +6,5,187~6,6,187 +4,5,66~4,8,66 +5,3,63~5,4,63 +2,5,96~3,5,96 +8,7,270~8,9,270 +1,7,27~1,9,27 +8,5,216~8,8,216 +3,6,68~5,6,68 +8,1,259~8,2,259 +4,4,143~4,5,143 +3,0,113~5,0,113 +0,1,9~0,3,9 +6,0,57~8,0,57 +3,7,152~3,8,152 +4,7,79~4,9,79 +8,4,59~8,6,59 +0,0,215~0,1,215 +4,0,76~6,0,76 +8,3,215~8,5,215 +4,5,77~5,5,77 +4,4,67~6,4,67 +7,6,157~7,7,157 +5,2,109~5,4,109 +0,6,76~0,8,76 +3,3,6~5,3,6 +2,4,207~2,6,207 +6,0,102~6,1,102 +7,8,10~9,8,10 +1,7,219~3,7,219 +4,6,218~4,9,218 +0,0,150~3,0,150 +5,7,122~8,7,122 +1,9,175~4,9,175 +9,3,217~9,4,217 +0,8,155~0,9,155 +5,1,232~5,3,232 +3,7,87~3,8,87 +3,6,155~6,6,155 +8,6,80~8,8,80 +6,1,206~7,1,206 +0,4,5~0,4,7 +2,4,53~2,7,53 +3,2,62~3,3,62 +0,2,314~0,4,314 +5,4,241~5,7,241 +2,9,231~2,9,231 +1,4,110~1,5,110 +3,0,21~5,0,21 +7,5,252~7,5,254 +8,4,233~8,6,233 +5,7,10~6,7,10 +0,5,60~3,5,60 +6,9,251~8,9,251 +1,4,304~2,4,304 +1,0,16~4,0,16 +7,2,68~8,2,68 +5,4,208~5,6,208 +7,7,160~7,9,160 +1,2,217~3,2,217 +0,3,170~1,3,170 +1,5,83~3,5,83 +9,4,111~9,7,111 +2,4,114~5,4,114 +1,7,82~1,9,82 +0,1,53~3,1,53 +4,0,126~4,0,129 +3,7,131~6,7,131 +7,9,163~9,9,163 +6,6,312~6,9,312 +7,6,195~7,8,195 +5,8,246~7,8,246 +4,3,222~6,3,222 +5,2,251~8,2,251 +2,7,22~2,9,22 +4,3,261~4,5,261 +5,3,60~5,6,60 +1,4,70~1,7,70 +3,2,241~3,5,241 +9,7,41~9,9,41 +3,0,192~3,2,192 +7,1,289~7,3,289 +0,1,224~0,3,224 +8,2,101~8,3,101 +8,4,239~8,6,239 +4,2,196~7,2,196 +1,7,20~2,7,20 +5,4,285~5,4,287 +0,5,198~2,5,198 +1,6,290~1,9,290 +1,2,204~1,4,204 +0,4,284~0,5,284 +3,3,33~3,5,33 +3,0,105~3,3,105 +5,5,153~7,5,153 +5,2,231~8,2,231 +1,6,3~3,6,3 +0,6,11~3,6,11 +8,0,55~8,3,55 +9,7,177~9,7,179 +7,9,9~9,9,9 +4,5,51~6,5,51 +4,1,83~4,3,83 +7,1,14~7,1,14 +5,1,247~5,3,247 +3,3,279~3,7,279 +5,6,289~5,8,289 +0,0,227~0,2,227 +0,5,74~0,7,74 +3,1,248~3,3,248 +2,1,24~2,3,24 +4,0,131~7,0,131 +5,6,227~7,6,227 +0,0,230~1,0,230 +4,6,110~6,6,110 +5,5,288~5,7,288 +3,3,39~3,6,39 +5,0,208~7,0,208 +1,7,46~4,7,46 +2,1,194~2,4,194 +0,9,234~1,9,234 +2,3,209~2,4,209 +0,2,113~3,2,113 +1,6,68~1,7,68 +4,6,74~4,8,74 +0,6,194~0,9,194 +2,8,14~3,8,14 +3,7,174~3,8,174 +8,0,142~8,0,145 +2,7,290~2,8,290 +2,6,115~2,6,118 +3,5,217~3,7,217 +8,3,64~8,3,67 +8,2,94~8,3,94 +6,6,298~6,8,298 +5,1,127~5,1,129 +2,1,5~2,3,5 +0,6,289~0,8,289 +8,1,183~8,3,183 +5,2,177~5,5,177 +9,2,215~9,6,215 +2,7,146~2,9,146 +1,6,80~3,6,80 +5,1,57~5,4,57 +4,2,84~4,2,85 +7,1,288~7,4,288 +3,3,220~5,3,220 +8,8,75~8,9,75 +5,0,276~5,2,276 +3,3,53~3,5,53 +9,7,161~9,7,161 +6,2,26~8,2,26 +9,2,7~9,4,7 +3,0,14~5,0,14 +5,8,121~7,8,121 +0,8,1~1,8,1 +9,6,158~9,8,158 +7,5,197~7,7,197 +5,1,296~7,1,296 +4,7,39~4,9,39 +0,5,243~0,5,246 +2,3,41~5,3,41 +1,3,115~1,4,115 +3,2,14~5,2,14 +5,1,181~5,4,181 +1,2,167~1,5,167 +4,6,40~7,6,40 +9,1,19~9,4,19 +6,0,33~7,0,33 +2,3,12~2,6,12 +8,1,111~8,1,113 +7,9,252~8,9,252 +0,6,17~3,6,17 +5,6,228~7,6,228 +7,4,1~7,6,1 +7,8,302~9,8,302 +0,4,146~3,4,146 +1,5,251~2,5,251 +2,5,273~2,7,273 +1,4,283~2,4,283 +3,7,178~4,7,178 +6,6,237~6,9,237 +1,9,225~1,9,227 +5,9,46~7,9,46 +5,0,206~5,1,206 +4,5,21~6,5,21 +9,6,78~9,7,78 +1,4,243~1,6,243 +4,7,271~4,7,272 +8,2,8~8,5,8 +3,3,247~3,5,247 +1,6,233~4,6,233 +5,8,291~5,8,292 +4,7,108~4,9,108 +9,6,77~9,8,77 +5,1,125~5,3,125 +1,8,123~3,8,123 +7,3,100~7,6,100 +4,9,3~7,9,3 +0,4,280~2,4,280 +7,6,124~9,6,124 +9,0,34~9,2,34 +0,6,121~0,8,121 +6,3,71~8,3,71 +8,0,140~8,2,140 +3,7,104~5,7,104 +5,3,81~7,3,81 +4,7,105~5,7,105 +5,5,19~5,6,19 +4,1,190~6,1,190 +7,5,282~9,5,282 +7,8,213~7,8,215 +7,0,315~7,2,315 +4,3,55~6,3,55 +7,4,139~7,4,139 +0,8,257~2,8,257 +5,0,244~7,0,244 +4,0,300~4,3,300 +0,3,252~0,3,253 +4,8,253~7,8,253 +4,8,194~6,8,194 +1,7,161~4,7,161 +8,9,60~9,9,60 +4,9,245~5,9,245 +8,6,305~9,6,305 +8,2,74~8,4,74 +2,3,3~2,5,3 +4,0,150~5,0,150 +1,1,187~3,1,187 +5,1,11~5,4,11 +6,1,136~6,4,136 +5,2,104~5,3,104 +2,1,213~4,1,213 +3,7,233~5,7,233 +3,5,314~3,8,314 +5,2,119~5,5,119 +5,6,43~8,6,43 +3,3,52~5,3,52 +1,4,79~1,7,79 +3,2,18~6,2,18 +1,4,308~4,4,308 +5,2,235~7,2,235 +4,8,177~4,8,180 +8,0,71~8,0,73 +9,2,32~9,5,32 +2,7,286~2,9,286 +4,6,248~4,6,250 +3,9,2~5,9,2 +2,3,213~2,5,213 +3,1,152~7,1,152 +2,1,122~3,1,122 +4,0,179~4,3,179 +9,1,226~9,1,229 +3,0,243~5,0,243 +2,8,255~5,8,255 +6,9,36~6,9,37 +9,6,169~9,7,169 +6,1,251~7,1,251 +2,5,70~2,7,70 +4,7,109~4,9,109 +9,6,248~9,8,248 +0,1,289~0,1,292 +6,5,240~6,5,242 +6,1,253~9,1,253 +2,3,182~4,3,182 +2,1,111~3,1,111 +9,4,71~9,6,71 +6,0,16~8,0,16 +0,0,104~0,1,104 +6,3,216~6,5,216 +8,2,149~8,5,149 +9,4,108~9,4,108 +6,1,138~7,1,138 +7,8,251~8,8,251 +6,8,74~9,8,74 +5,1,49~5,1,51 +2,6,63~5,6,63 +5,8,150~7,8,150 +5,1,260~5,2,260 +3,7,165~5,7,165 +6,1,108~8,1,108 +2,0,279~5,0,279 +6,6,57~8,6,57 +9,7,278~9,8,278 +1,4,93~1,4,96 +5,7,24~7,7,24 +1,1,62~1,5,62 +9,7,276~9,9,276 +4,7,196~7,7,196 +6,6,134~7,6,134 +3,3,74~4,3,74 +7,5,9~7,7,9 +2,5,72~2,5,73 +0,4,67~0,4,69 +5,7,144~7,7,144 +6,0,109~8,0,109 +6,6,276~8,6,276 +0,1,117~0,3,117 +0,7,193~1,7,193 +5,8,192~7,8,192 +3,7,22~3,9,22 +3,6,75~3,9,75 +7,6,85~7,6,87 +6,6,315~6,7,315 +3,4,87~3,6,87 +1,9,29~2,9,29 +2,4,116~6,4,116 +5,2,272~5,4,272 +5,4,289~5,4,289 +2,7,118~2,9,118 +1,7,76~3,7,76 +5,7,239~5,9,239 +7,1,283~7,1,286 +1,5,121~1,8,121 +0,2,3~0,4,3 +9,9,18~9,9,18 +9,3,91~9,4,91 +6,6,321~7,6,321 +2,0,108~4,0,108 +2,1,106~5,1,106 +6,0,127~6,0,130 +6,7,276~8,7,276 +9,9,20~9,9,22 +7,3,119~7,5,119 +2,6,194~5,6,194 +7,2,205~9,2,205 +1,0,308~1,1,308 +0,7,260~0,9,260 +4,4,171~7,4,171 +4,5,286~4,7,286 +0,9,213~2,9,213 +3,8,148~5,8,148 +9,1,66~9,3,66 +3,6,174~3,6,175 +7,4,274~7,6,274 +0,8,15~3,8,15 +7,0,35~8,0,35 +3,7,197~6,7,197 +3,5,84~3,7,84 +3,4,311~3,6,311 +5,7,225~5,9,225 +4,3,130~4,5,130 +2,0,104~2,3,104 +0,6,311~2,6,311 +5,4,183~5,7,183 +2,8,143~4,8,143 +0,5,285~0,7,285 +3,3,13~3,6,13 +3,8,103~5,8,103 +8,7,252~8,8,252 +8,0,217~9,0,217 +9,1,258~9,3,258 +9,5,286~9,6,286 +4,8,94~4,8,96 +5,9,12~7,9,12 +3,6,219~4,6,219 +1,4,26~3,4,26 +4,7,26~6,7,26 +3,8,90~5,8,90 +1,5,115~2,5,115 +1,4,50~2,4,50 +6,6,224~7,6,224 +1,1,307~3,1,307 +3,3,103~6,3,103 +3,0,195~5,0,195 +8,4,266~8,4,268 +4,9,260~5,9,260 +0,7,139~2,7,139 +0,6,105~0,9,105 +0,2,258~0,4,258 +1,0,65~1,3,65 +8,7,78~8,9,78 +9,0,3~9,2,3 +0,0,17~2,0,17 +8,6,79~8,8,79 +3,3,8~3,6,8 +4,3,237~4,5,237 +1,2,306~1,4,306 +5,8,83~6,8,83 +6,2,35~6,2,36 +1,6,274~1,8,274 +3,5,293~5,5,293 +3,9,78~7,9,78 +5,0,32~8,0,32 +6,6,213~6,7,213 +1,3,260~4,3,260 +7,6,194~7,8,194 +6,6,211~6,8,211 +7,7,124~7,7,124 +6,2,263~6,5,263 +7,7,249~7,9,249 +4,8,302~5,8,302 +5,6,263~7,6,263 +4,3,57~4,4,57 +3,2,37~3,4,37 +2,4,234~4,4,234 +9,1,72~9,3,72 +9,0,36~9,0,38 +7,3,218~8,3,218 +4,5,133~4,7,133 +8,0,260~8,3,260 +4,8,30~7,8,30 +5,5,209~5,5,211 +6,2,213~6,4,213 +3,4,274~6,4,274 +6,7,91~6,8,91 +6,8,276~6,9,276 +6,0,182~6,0,182 +2,5,200~4,5,200 +0,5,102~0,6,102 +0,0,6~0,3,6 +6,5,10~8,5,10 +2,5,140~5,5,140 +6,0,99~8,0,99 +8,3,11~8,3,11 +8,5,75~8,5,78 +5,9,286~7,9,286 +0,3,47~0,6,47 +4,6,121~6,6,121 +8,0,36~8,2,36 +6,8,93~6,9,93 +6,0,66~6,2,66 +3,8,73~6,8,73 +0,2,40~3,2,40 +3,2,123~3,2,126 +7,6,212~7,9,212 +8,3,320~8,6,320 +1,1,145~1,4,145 +2,4,246~2,4,248 +7,8,96~9,8,96 +4,0,274~6,0,274 +3,0,11~6,0,11 +4,0,3~4,3,3 +2,7,16~2,8,16 +8,2,255~8,5,255 +6,9,8~6,9,11 +2,0,70~4,0,70 +1,7,117~4,7,117 +7,1,137~7,4,137 +2,5,76~4,5,76 +1,4,112~1,6,112 +0,8,141~3,8,141 +0,2,311~2,2,311 +3,3,45~3,4,45 +2,5,188~2,8,188 +6,6,294~8,6,294 +1,3,126~2,3,126 +2,1,206~2,4,206 +2,5,143~2,5,145 +7,4,88~7,6,88 +6,5,231~6,8,231 +9,1,255~9,3,255 +0,0,26~2,0,26 +9,2,109~9,5,109 +4,0,278~5,0,278 +0,3,284~1,3,284 +2,7,224~2,9,224 +5,7,72~6,7,72 +5,7,21~5,8,21 +4,2,262~4,2,263 +2,5,85~2,8,85 +3,3,253~5,3,253 +0,0,3~3,0,3 +5,1,215~5,3,215 +1,3,125~4,3,125 +1,9,45~5,9,45 +8,9,57~8,9,57 +6,4,7~6,7,7 +8,7,183~8,9,183 +9,4,270~9,6,270 +6,0,211~6,0,212 +6,0,181~7,0,181 +4,6,317~4,9,317 +7,7,215~7,7,217 +4,7,209~4,7,212 +6,8,220~7,8,220 +7,1,231~8,1,231 +6,7,252~7,7,252 +2,2,313~3,2,313 +7,4,263~8,4,263 +6,4,134~8,4,134 +5,1,38~5,3,38 +3,8,25~3,8,26 +6,8,155~9,8,155 +2,5,112~4,5,112 +5,8,217~7,8,217 +4,3,289~4,5,289 +8,4,251~8,6,251 +0,2,51~2,2,51 +8,0,39~8,2,39 +1,5,288~1,8,288 +1,8,303~4,8,303 +3,1,109~5,1,109 +5,3,319~7,3,319 +0,6,115~0,9,115 +2,9,121~5,9,121 +8,7,218~8,8,218 +1,9,293~2,9,293 +1,7,306~2,7,306 +5,8,315~7,8,315 +0,6,110~0,8,110 +7,4,155~7,6,155 +1,5,8~1,7,8 +7,2,90~7,5,90 +5,3,145~6,3,145 +5,2,79~5,4,79 +3,0,68~3,2,68 +0,5,96~0,5,98 +9,5,246~9,8,246 +3,5,79~3,7,79 +0,4,9~0,4,11 +9,2,73~9,2,73 +0,1,207~4,1,207 +3,0,9~3,2,9 +2,2,299~5,2,299 +4,3,264~4,5,264 +9,7,2~9,9,2 +7,4,175~9,4,175 +7,5,275~8,5,275 +0,4,21~0,7,21 +4,6,101~4,8,101 +3,3,121~5,3,121 +7,3,53~7,3,55 +0,6,123~0,9,123 +1,5,224~1,7,224 +2,2,101~2,3,101 +7,3,17~9,3,17 +9,2,231~9,3,231 +1,8,8~3,8,8 +8,0,29~8,2,29 +7,7,146~7,7,148 +2,2,280~4,2,280 +1,6,54~1,6,56 +3,5,67~5,5,67 +5,9,181~5,9,183 +6,2,179~6,4,179 +9,2,106~9,6,106 +5,8,119~7,8,119 +2,1,58~2,2,58 +0,8,189~3,8,189 +5,1,248~7,1,248 +3,8,176~5,8,176 +7,2,258~8,2,258 +3,0,85~3,2,85 +6,5,92~6,7,92 +1,4,198~3,4,198 +5,2,199~5,2,201 +9,1,12~9,2,12 +0,4,254~0,6,254 +3,7,267~5,7,267 +1,5,276~2,5,276 +0,6,167~1,6,167 +3,3,156~3,6,156 +3,5,68~3,5,71 +3,6,195~3,6,197 +6,1,34~6,3,34 +5,6,306~5,8,306 +1,0,190~3,0,190 +0,1,25~2,1,25 +4,4,284~6,4,284 +1,6,270~1,8,270 +6,0,146~8,0,146 +9,5,144~9,8,144 +4,1,77~4,3,77 +1,6,271~3,6,271 +5,8,44~5,8,46 +8,6,52~9,6,52 +2,3,232~2,5,232 +4,6,22~6,6,22 +5,1,279~7,1,279 +0,0,95~3,0,95 +1,4,117~1,4,119 +8,4,90~8,4,92 +0,6,263~0,8,263 +2,4,224~3,4,224 +3,1,117~5,1,117 +4,7,120~4,9,120 +1,2,307~1,3,307 +9,5,272~9,8,272 +4,4,44~4,5,44 +8,0,308~8,3,308 +4,2,298~4,3,298 +0,1,230~2,1,230 +3,7,155~5,7,155 +1,0,70~1,2,70 +4,4,41~4,6,41 +7,5,215~7,5,218 +2,5,55~2,7,55 +6,3,24~6,6,24 +4,8,31~6,8,31 +4,0,197~4,0,199 +4,2,306~6,2,306 +7,5,89~7,7,89 +2,1,29~2,4,29 +1,6,87~1,7,87 +5,4,319~7,4,319 +1,2,173~1,4,173 +0,7,71~2,7,71 +1,6,255~1,6,257 +8,3,219~8,5,219 +6,0,110~6,2,110 +6,0,58~7,0,58 +0,3,106~0,5,106 +5,7,116~5,9,116 +6,5,143~9,5,143 +0,3,100~3,3,100 +0,8,113~1,8,113 +5,6,243~5,8,243 +5,5,143~5,7,143 +7,9,282~7,9,285 +0,0,66~3,0,66 +6,9,99~8,9,99 +4,7,314~7,7,314 +4,1,214~4,4,214 +9,3,99~9,5,99 +4,0,151~7,0,151 +1,7,95~4,7,95 +6,2,93~9,2,93 +1,4,124~1,6,124 +3,2,122~4,2,122 +0,6,124~0,6,124 +7,0,187~9,0,187 +7,6,214~7,7,214 +7,2,79~7,4,79 +1,3,74~1,3,75 +7,0,179~7,2,179 +5,1,15~5,4,15 +6,5,272~8,5,272 +7,5,32~7,8,32 +7,5,203~9,5,203 +9,7,176~9,8,176 +9,3,188~9,4,188 +3,9,178~5,9,178 +7,7,58~8,7,58 +0,1,49~0,4,49 +4,6,319~4,6,322 +8,3,77~8,3,77 +2,2,33~2,3,33 +2,6,112~4,6,112 +3,6,172~3,9,172 +2,1,8~2,4,8 +7,9,103~9,9,103 +6,5,298~8,5,298 +1,0,313~1,2,313 +1,2,27~1,4,27 +4,7,99~4,9,99 +7,0,281~7,2,281 +7,4,211~7,7,211 +9,1,114~9,3,114 +0,4,222~2,4,222 +3,8,64~6,8,64 +6,4,247~6,8,247 +7,6,60~7,6,62 +0,5,149~3,5,149 +1,8,30~2,8,30 +5,5,296~5,7,296 +7,0,254~7,2,254 +0,8,120~2,8,120 +0,2,41~0,2,44 +6,3,223~7,3,223 +1,6,276~1,6,277 +3,5,114~4,5,114 +7,1,218~9,1,218 +5,5,65~7,5,65 +4,6,225~7,6,225 +0,8,11~2,8,11 +6,4,195~9,4,195 +8,5,13~8,6,13 +2,8,268~4,8,268 +1,5,164~1,7,164 +6,2,197~8,2,197 +5,6,318~7,6,318 +3,8,278~5,8,278 +7,6,98~7,9,98 +3,3,136~4,3,136 +7,1,259~7,2,259 +3,9,6~6,9,6 +5,4,201~5,5,201 +6,6,307~6,9,307 +6,4,219~6,6,219 +0,6,51~0,9,51 +0,4,15~3,4,15 +5,7,119~7,7,119 +7,3,213~7,4,213 +3,6,226~6,6,226 +9,1,221~9,1,222 +0,1,127~0,3,127 +7,7,153~7,8,153 +6,2,227~9,2,227 +7,3,20~7,4,20 +1,3,72~4,3,72 +4,0,146~4,3,146 +8,0,223~8,1,223 +4,8,40~4,8,42 +7,5,156~7,7,156 +9,0,224~9,3,224 +8,5,6~8,7,6 +4,6,164~4,8,164 +0,5,108~0,5,108 +4,2,296~7,2,296 +6,2,32~8,2,32 +1,3,207~1,3,210 +5,6,261~8,6,261 +0,1,249~0,4,249 +4,3,280~4,4,280 +3,1,240~3,4,240 +9,0,68~9,2,68 +1,3,34~3,3,34 +2,9,227~3,9,227 +2,2,163~2,4,163 +3,0,73~4,0,73 +2,4,306~2,6,306 +6,4,105~9,4,105 +5,7,70~7,7,70 +1,2,243~2,2,243 +0,0,313~0,2,313 +3,1,148~5,1,148 +0,7,18~0,9,18 +9,5,98~9,8,98 +2,3,187~2,4,187 +7,2,317~7,5,317 +4,5,49~4,8,49 +5,5,178~5,7,178 +2,8,304~2,8,307 +4,6,292~6,6,292 +4,5,287~4,6,287 +5,8,80~5,9,80 +5,3,203~5,6,203 +3,8,304~3,8,305 +4,3,283~4,5,283 +0,6,241~4,6,241 +7,5,85~9,5,85 +9,8,80~9,8,83 +8,3,280~8,3,280 +9,6,282~9,9,282 +4,7,217~4,9,217 +2,2,216~2,5,216 +2,3,25~5,3,25 +5,9,42~7,9,42 +7,1,10~7,1,12 +3,2,245~3,3,245 +6,1,8~9,1,8 +2,5,186~2,6,186 +3,3,281~3,3,282 +0,0,107~0,0,110 +0,9,261~0,9,264 +2,7,24~3,7,24 +1,2,142~4,2,142 +6,2,39~6,5,39 +5,0,20~5,3,20 +1,7,86~3,7,86 +3,6,192~5,6,192 +5,8,260~5,8,263 +3,0,237~5,0,237 +2,1,156~4,1,156 +1,7,158~3,7,158 +7,3,49~7,5,49 +1,9,296~4,9,296 +8,1,217~8,3,217 +3,7,149~3,9,149 +8,0,185~8,2,185 +9,2,9~9,4,9 +8,3,73~8,6,73 +6,2,76~7,2,76 +4,1,62~4,4,62 +9,7,13~9,9,13 +3,8,52~5,8,52 +8,3,3~8,3,4 +4,4,109~4,6,109 +4,8,34~4,8,36 +3,7,235~5,7,235 +8,2,98~9,2,98 +6,3,84~6,5,84 +1,2,195~4,2,195 +2,2,242~4,2,242 +3,2,81~3,5,81 +2,5,309~2,7,309 +6,6,61~6,6,63 +2,2,235~2,5,235 +8,1,97~8,2,97 +7,7,209~7,9,209 +7,3,4~7,5,4 +4,2,236~7,2,236 +2,4,71~2,6,71 +0,0,54~0,2,54 +9,7,39~9,9,39 +2,3,210~4,3,210 +3,3,265~3,6,265 +6,5,295~6,6,295 +1,9,128~2,9,128 +5,9,308~6,9,308 +2,3,166~3,3,166 +3,7,215~5,7,215 +3,8,76~3,8,77 +2,7,19~5,7,19 +4,1,277~4,4,277 +1,5,50~3,5,50 +9,7,166~9,9,166 +8,0,315~8,2,315 +6,8,120~7,8,120 +5,0,184~5,1,184 +9,5,5~9,5,8 +7,0,111~8,0,111 +2,1,56~2,3,56 +7,8,36~9,8,36 +0,0,232~0,0,232 +0,3,247~0,5,247 +5,4,18~6,4,18 +6,0,301~6,2,301 +3,4,277~3,6,277 +4,8,273~6,8,273 +2,6,126~2,9,126 +5,5,72~7,5,72 +7,1,104~7,4,104 +1,5,211~3,5,211 diff --git a/2023/input/22/test-1 b/2023/input/22/test-1 new file mode 100644 index 0000000..43a7fc5 --- /dev/null +++ b/2023/input/22/test-1 @@ -0,0 +1,7 @@ +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 diff --git a/2023/src/bin/day22.rs b/2023/src/bin/day22.rs new file mode 100644 index 0000000..477b2e1 --- /dev/null +++ b/2023/src/bin/day22.rs @@ -0,0 +1,229 @@ +#![feature(test)] +use std::collections::{HashMap, HashSet}; + +use anyhow::Result; +use aoc::Solver; + +// -- Runners -- +fn main() -> Result<()> { + Day::solve() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_test1() -> Result<()> { + Day::test(Day::part1, "test-1", 5) + } + + #[test] + fn part1_solution() -> Result<()> { + Day::test(Day::part1, "input", 488) + } + + #[test] + fn part2_test1() -> Result<()> { + Day::test(Day::part2, "test-1", 7) + } + + #[test] + fn part2_solution() -> Result<()> { + Day::test(Day::part2, "input", 79465) + } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(Day::part1, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(Day::part2, b) + } +} + +#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] +struct Brick { + start: (isize, isize, isize), + end: (isize, isize, isize), +} + +impl Brick { + fn fall( + &self, + index: usize, + grid: &mut HashMap<(isize, isize, isize), usize>, + ) -> HashSet { + let mut down = 0; + // Keep track of all the unique bricks supporting the current brick + let mut supports = HashSet::new(); + for o in 1..self.start.2 { + for x in self.start.0..=self.end.0 { + for y in self.start.1..=self.end.1 { + for z in self.start.2..=self.end.2 { + if let Some(&support) = grid.get(&(x, y, z - o)) { + supports.insert(support); + } + } + } + } + + // We have landed on a support + if !supports.is_empty() { + break; + } + + down += 1; + } + + // Occupy the space in the grid + for x in self.start.0..=self.end.0 { + for y in self.start.1..=self.end.1 { + for z in self.start.2..=self.end.2 { + grid.insert((x, y, z - down), index); + } + } + } + + // Return all the unique supports + supports + } +} + +// -- Solution -- +pub struct Day; +impl aoc::Solver for Day { + type Output1 = usize; + type Output2 = usize; + + fn day() -> u8 { + 22 + } + + fn part1(input: &str) -> Self::Output1 { + let mut bricks: Vec<_> = input + .lines() + .map(|line| { + let (start, end) = line.split_once('~').unwrap(); + + let start: Vec<_> = start + .splitn(3, ',') + .map(|num| num.parse::().unwrap()) + .collect(); + let start = (start[0], start[1], start[2]); + + let end: Vec<_> = end + .splitn(3, ',') + .map(|num| num.parse::().unwrap()) + .collect(); + let end = (end[0], end[1], end[2]); + + Brick { start, end } + }) + .collect(); + + // Sort the bricks from top to bottom + bricks.sort_by(|a, b| a.start.2.cmp(&b.start.2)); + + // Figure out which bricks are essential and can not be disintegrated + let mut grid = HashMap::new(); + let essential_bricks: HashSet<_> = bricks + .iter() + .enumerate() + // Drop down each brick and get all the supporting bricks + .map(|(index, brick)| brick.fall(index, &mut grid)) + // Only keep bricks that are supported by one other brick + // If that other brick is disintegrated this brick will fall + .filter(|supports| supports.len() == 1) + // Flatten out and collect to take out duplicate entries + .flatten() + .collect(); + + bricks.len() - essential_bricks.len() + } + + fn part2(input: &str) -> Self::Output2 { + let mut bricks: Vec<_> = input + .lines() + .map(|line| { + let (start, end) = line.split_once('~').unwrap(); + + let start: Vec<_> = start + .splitn(3, ',') + .map(|num| num.parse::().unwrap()) + .collect(); + let start = (start[0], start[1], start[2]); + + let end: Vec<_> = end + .splitn(3, ',') + .map(|num| num.parse::().unwrap()) + .collect(); + let end = (end[0], end[1], end[2]); + + Brick { start, end } + }) + .collect(); + + // Sort the bricks from top to bottom + bricks.sort_by(|a, b| a.start.2.cmp(&b.start.2)); + + // Figure out which bricks are essential and can not be disintegrated + let mut grid = HashMap::new(); + let supports: Vec<_> = bricks + .iter() + .enumerate() + // Drop down each brick and get all the supporting bricks + .map(|(index, brick)| brick.fall(index, &mut grid)) + .collect(); + + let essential_bricks: HashSet<_> = supports + .iter() + .filter(|supports| supports.len() == 1) + // Flatten out and collect to take out duplicate entries + .flatten() + .collect(); + + let mut sum = 0; + for eliminate in essential_bricks { + let mut falling = HashSet::new(); + falling.insert(*eliminate); + let mut previous = 0; + + while previous != falling.len() { + previous = falling.len(); + + let new: HashSet<_> = supports + .iter() + .enumerate() + .filter(|(_, supports)| { + if supports.is_empty() { + return false; + } + + for support in *supports { + if !falling.contains(support) { + return false; + } + } + + true + }) + .map(|(index, _)| index) + .collect(); + + falling.extend(new); + } + + if !falling.is_empty() { + sum += falling.len() - 1; + } + } + + sum + } +}