From fdb5d7cccfb93ee2c75de1382dde44b74238d6d0 Mon Sep 17 00:00:00 2001 From: livexia Date: Fri, 22 Dec 2023 15:46:25 +0800 Subject: [PATCH] day 22: part1 --- aoc22/Cargo.toml | 8 + aoc22/input/input.txt | 1223 +++++++++++++++++++++++++++++++++++++++++ aoc22/src/main.rs | 216 ++++++++ 3 files changed, 1447 insertions(+) create mode 100644 aoc22/Cargo.toml create mode 100644 aoc22/input/input.txt create mode 100644 aoc22/src/main.rs diff --git a/aoc22/Cargo.toml b/aoc22/Cargo.toml new file mode 100644 index 0000000..2eb06bf --- /dev/null +++ b/aoc22/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc22" +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/aoc22/input/input.txt b/aoc22/input/input.txt new file mode 100644 index 0000000..2901134 --- /dev/null +++ b/aoc22/input/input.txt @@ -0,0 +1,1223 @@ +5,0,87~8,0,87 +7,3,13~7,5,13 +6,7,185~8,7,185 +4,3,64~7,3,64 +0,0,74~2,0,74 +5,7,149~6,7,149 +5,3,142~5,5,142 +1,8,218~1,8,220 +3,6,180~3,6,182 +5,7,65~5,8,65 +3,4,275~4,4,275 +8,9,115~9,9,115 +1,1,165~4,1,165 +1,5,18~3,5,18 +0,1,90~2,1,90 +2,3,29~4,3,29 +6,4,277~8,4,277 +9,4,194~9,4,196 +2,0,188~2,2,188 +0,8,38~2,8,38 +8,5,57~8,7,57 +3,0,257~3,3,257 +1,0,144~1,3,144 +3,2,158~6,2,158 +4,0,5~4,0,7 +4,7,203~4,7,206 +3,6,145~5,6,145 +5,5,1~7,5,1 +8,5,139~8,7,139 +7,1,31~7,2,31 +3,5,229~4,5,229 +7,0,97~7,0,99 +5,6,195~5,7,195 +5,2,27~5,4,27 +6,4,234~7,4,234 +2,7,127~2,7,129 +2,5,200~5,5,200 +8,2,140~8,5,140 +0,0,175~0,0,175 +6,7,233~6,9,233 +5,0,15~8,0,15 +3,1,267~3,3,267 +6,6,65~6,6,67 +3,8,16~5,8,16 +6,7,238~8,7,238 +0,5,245~0,7,245 +6,6,247~6,9,247 +5,5,39~5,6,39 +3,0,177~5,0,177 +5,3,248~5,5,248 +2,0,138~4,0,138 +3,5,178~3,6,178 +1,5,154~1,5,155 +7,3,14~7,3,15 +4,0,190~4,3,190 +5,3,264~5,5,264 +7,5,107~9,5,107 +9,1,147~9,1,148 +5,9,175~6,9,175 +0,2,70~0,5,70 +6,0,60~6,4,60 +8,4,167~8,6,167 +3,2,244~3,5,244 +5,5,14~7,5,14 +8,9,64~8,9,65 +1,6,99~4,6,99 +5,7,25~6,7,25 +5,5,188~7,5,188 +5,6,100~5,8,100 +2,8,80~2,9,80 +1,8,268~3,8,268 +6,3,94~8,3,94 +0,6,138~2,6,138 +1,0,81~2,0,81 +3,1,113~4,1,113 +9,3,154~9,5,154 +4,6,38~6,6,38 +8,4,151~8,5,151 +1,0,229~1,1,229 +5,6,2~7,6,2 +9,6,159~9,8,159 +2,1,203~2,3,203 +4,6,109~6,6,109 +1,6,211~3,6,211 +5,0,224~5,3,224 +1,4,17~1,5,17 +9,5,138~9,8,138 +6,0,280~6,2,280 +2,4,41~4,4,41 +3,0,229~5,0,229 +5,7,68~5,7,70 +5,8,152~5,9,152 +9,6,92~9,6,95 +1,4,207~3,4,207 +4,6,211~4,8,211 +6,5,62~6,8,62 +6,0,266~6,2,266 +8,0,57~8,4,57 +6,5,182~6,8,182 +6,0,254~6,4,254 +8,6,4~8,9,4 +4,5,133~4,5,135 +5,0,167~5,2,167 +0,4,177~0,5,177 +1,1,181~1,2,181 +2,8,28~2,8,30 +4,0,48~4,2,48 +9,6,14~9,8,14 +5,1,199~8,1,199 +9,7,48~9,9,48 +0,7,282~2,7,282 +3,8,214~3,8,217 +3,1,86~3,3,86 +5,3,67~8,3,67 +1,5,166~4,5,166 +7,5,263~9,5,263 +6,9,228~7,9,228 +7,0,88~7,0,89 +6,5,12~8,5,12 +5,6,208~7,6,208 +2,9,19~4,9,19 +0,1,129~2,1,129 +3,2,146~3,6,146 +7,4,87~7,4,90 +1,4,279~4,4,279 +5,8,275~8,8,275 +3,1,273~3,1,275 +1,2,129~3,2,129 +8,9,6~8,9,7 +3,7,276~5,7,276 +4,0,160~4,2,160 +3,8,284~5,8,284 +4,9,224~7,9,224 +4,5,87~4,5,89 +6,1,270~9,1,270 +1,1,204~1,4,204 +1,5,46~1,7,46 +2,8,78~5,8,78 +2,7,168~2,7,171 +4,2,130~4,4,130 +9,6,64~9,8,64 +1,7,279~1,9,279 +0,9,57~2,9,57 +1,4,66~1,5,66 +3,0,270~3,2,270 +5,6,218~8,6,218 +6,0,241~6,2,241 +1,1,132~2,1,132 +1,4,260~1,7,260 +4,6,210~4,8,210 +8,0,236~8,3,236 +3,7,181~5,7,181 +2,3,244~2,5,244 +1,3,169~1,5,169 +8,1,235~8,3,235 +4,6,2~4,8,2 +1,5,72~3,5,72 +3,6,279~3,8,279 +5,8,3~5,8,5 +2,3,177~5,3,177 +5,3,70~5,4,70 +4,6,147~4,9,147 +1,7,64~3,7,64 +7,1,147~7,1,148 +8,3,115~8,5,115 +4,2,231~4,4,231 +5,0,137~8,0,137 +3,0,161~5,0,161 +2,8,222~2,9,222 +4,2,183~4,5,183 +0,8,213~1,8,213 +1,8,129~4,8,129 +3,2,128~5,2,128 +4,0,67~6,0,67 +2,5,128~2,6,128 +2,2,139~4,2,139 +3,5,43~3,6,43 +8,6,216~8,8,216 +0,2,104~1,2,104 +6,7,171~7,7,171 +0,3,1~0,4,1 +3,7,193~6,7,193 +8,5,27~8,8,27 +0,5,22~0,7,22 +2,7,156~4,7,156 +7,3,93~7,5,93 +7,4,118~7,7,118 +6,1,202~6,1,205 +3,2,19~3,2,21 +3,6,98~5,6,98 +6,0,157~6,2,157 +5,5,220~5,7,220 +8,5,99~8,5,99 +4,2,111~4,4,111 +3,5,224~5,5,224 +5,3,255~5,5,255 +9,8,256~9,8,257 +3,9,223~3,9,225 +6,1,208~6,3,208 +4,0,112~4,2,112 +5,6,111~5,6,114 +4,0,137~4,3,137 +4,0,11~7,0,11 +3,1,256~5,1,256 +2,2,200~2,4,200 +0,4,261~2,4,261 +6,5,55~6,7,55 +3,7,285~3,7,287 +0,8,54~0,9,54 +0,4,54~2,4,54 +4,7,5~4,9,5 +2,7,220~3,7,220 +4,7,166~4,8,166 +6,2,88~8,2,88 +5,2,117~5,4,117 +3,0,278~3,2,278 +1,8,57~3,8,57 +3,6,282~3,9,282 +6,6,187~8,6,187 +6,7,86~8,7,86 +8,0,138~9,0,138 +5,4,78~5,6,78 +4,5,196~6,5,196 +0,6,42~3,6,42 +6,5,71~7,5,71 +8,4,80~8,4,82 +0,9,275~2,9,275 +4,1,98~6,1,98 +8,2,149~8,4,149 +0,3,159~0,5,159 +0,7,92~0,8,92 +1,8,190~3,8,190 +4,2,188~7,2,188 +2,9,109~5,9,109 +8,3,84~8,5,84 +6,9,63~8,9,63 +6,2,275~6,4,275 +6,3,82~6,5,82 +2,7,35~2,8,35 +7,3,152~9,3,152 +6,7,187~7,7,187 +9,2,192~9,5,192 +7,1,256~7,3,256 +4,2,229~5,2,229 +6,5,217~8,5,217 +4,2,136~4,4,136 +9,8,102~9,8,104 +8,5,104~9,5,104 +6,6,169~8,6,169 +1,0,71~1,0,73 +6,5,199~8,5,199 +3,0,32~3,2,32 +4,7,171~4,9,171 +0,0,146~0,1,146 +0,5,48~0,9,48 +0,1,239~1,1,239 +2,1,140~4,1,140 +1,0,12~4,0,12 +6,2,264~6,4,264 +7,6,31~9,6,31 +3,1,99~3,4,99 +0,3,73~0,5,73 +9,3,270~9,6,270 +5,0,259~8,0,259 +0,7,137~0,9,137 +7,7,193~7,8,193 +8,0,81~8,1,81 +2,0,3~5,0,3 +3,5,49~4,5,49 +6,2,259~6,2,261 +1,3,151~1,5,151 +2,8,271~2,8,272 +6,6,286~8,6,286 +4,6,205~6,6,205 +2,3,262~2,3,265 +1,6,244~1,8,244 +5,2,239~5,5,239 +0,0,250~0,0,253 +1,5,257~1,7,257 +2,2,22~2,6,22 +1,3,146~1,5,146 +5,2,277~6,2,277 +9,7,252~9,8,252 +0,4,215~0,6,215 +2,2,45~4,2,45 +6,0,256~6,2,256 +7,1,28~9,1,28 +6,7,218~8,7,218 +0,6,57~2,6,57 +3,8,107~5,8,107 +5,2,243~5,5,243 +0,6,52~0,8,52 +1,5,181~4,5,181 +2,4,100~2,6,100 +6,7,227~8,7,227 +1,2,3~3,2,3 +7,6,136~9,6,136 +0,1,252~0,2,252 +6,4,203~6,7,203 +6,6,135~8,6,135 +1,4,185~3,4,185 +7,2,36~7,4,36 +2,6,172~4,6,172 +1,4,216~2,4,216 +9,1,67~9,4,67 +1,2,102~1,4,102 +1,5,54~1,7,54 +0,7,73~2,7,73 +2,3,3~4,3,3 +4,7,257~4,7,259 +0,8,51~2,8,51 +2,5,227~5,5,227 +7,5,260~7,5,261 +6,1,115~6,3,115 +4,3,174~6,3,174 +1,4,240~1,6,240 +9,6,11~9,8,11 +5,3,120~5,6,120 +6,2,163~6,4,163 +0,1,29~3,1,29 +9,7,99~9,9,99 +8,3,181~8,5,181 +7,1,249~7,1,251 +2,7,62~5,7,62 +3,7,108~4,7,108 +1,4,181~2,4,181 +3,1,272~3,4,272 +1,6,115~1,7,115 +7,7,208~8,7,208 +7,3,82~7,7,82 +0,2,92~4,2,92 +3,8,168~4,8,168 +3,1,264~3,4,264 +2,9,68~4,9,68 +1,1,7~3,1,7 +5,4,75~7,4,75 +6,2,173~6,5,173 +6,6,114~7,6,114 +2,2,230~3,2,230 +6,0,181~6,3,181 +4,1,138~7,1,138 +9,4,115~9,6,115 +3,6,3~3,6,3 +1,2,210~1,5,210 +2,4,249~4,4,249 +9,4,157~9,7,157 +4,3,83~6,3,83 +5,4,66~5,5,66 +6,0,62~7,0,62 +2,5,92~4,5,92 +9,7,67~9,9,67 +2,3,187~2,5,187 +9,7,139~9,8,139 +9,4,33~9,6,33 +7,3,133~8,3,133 +0,3,2~1,3,2 +0,6,157~2,6,157 +7,1,35~9,1,35 +3,0,52~3,2,52 +4,4,11~6,4,11 +9,6,250~9,7,250 +7,6,60~9,6,60 +4,8,108~5,8,108 +2,1,236~3,1,236 +2,6,177~2,9,177 +1,3,186~1,4,186 +1,7,185~1,7,186 +2,1,31~2,3,31 +5,3,32~8,3,32 +2,4,276~4,4,276 +4,5,131~6,5,131 +0,6,85~0,6,88 +2,3,190~2,5,190 +2,0,23~2,2,23 +7,4,8~9,4,8 +0,6,14~0,8,14 +0,1,5~3,1,5 +3,0,64~6,0,64 +7,3,258~7,3,260 +2,6,213~3,6,213 +3,5,221~5,5,221 +2,4,95~2,4,96 +3,4,274~6,4,274 +1,4,275~1,6,275 +9,9,205~9,9,206 +5,6,142~5,8,142 +7,7,172~7,9,172 +7,8,31~8,8,31 +0,6,196~0,9,196 +1,4,118~1,6,118 +3,3,235~5,3,235 +2,9,111~3,9,111 +2,2,260~2,4,260 +0,2,205~2,2,205 +4,6,167~5,6,167 +4,0,166~4,3,166 +5,0,10~5,1,10 +6,9,59~6,9,61 +0,0,191~2,0,191 +4,2,82~7,2,82 +2,9,148~4,9,148 +4,8,143~6,8,143 +2,4,153~4,4,153 +3,0,202~6,0,202 +7,5,206~7,7,206 +4,5,23~4,7,23 +2,3,72~5,3,72 +5,0,164~6,0,164 +4,2,187~4,4,187 +0,5,135~0,7,135 +7,6,6~9,6,6 +6,8,13~8,8,13 +4,0,143~5,0,143 +6,2,25~6,5,25 +1,0,146~1,0,146 +6,7,269~8,7,269 +9,4,204~9,7,204 +2,3,85~4,3,85 +7,6,91~9,6,91 +2,3,1~2,3,2 +4,5,84~6,5,84 +2,9,14~4,9,14 +5,5,175~7,5,175 +0,3,239~0,5,239 +0,4,170~2,4,170 +3,6,284~3,6,284 +5,3,131~5,4,131 +0,8,247~1,8,247 +1,9,204~2,9,204 +2,2,169~4,2,169 +0,2,218~0,4,218 +2,5,27~2,8,27 +1,7,276~1,7,277 +5,6,34~7,6,34 +5,6,21~7,6,21 +5,4,191~7,4,191 +0,7,75~1,7,75 +3,6,100~3,9,100 +8,2,215~8,4,215 +0,1,251~2,1,251 +1,8,130~1,8,130 +7,2,176~9,2,176 +9,7,98~9,8,98 +3,5,265~3,8,265 +5,2,214~8,2,214 +6,0,16~9,0,16 +5,5,132~5,8,132 +4,4,148~7,4,148 +1,0,93~1,1,93 +2,1,126~2,4,126 +0,4,193~2,4,193 +5,3,196~7,3,196 +1,2,105~1,2,107 +8,1,245~8,1,246 +5,1,168~5,1,170 +4,5,93~6,5,93 +4,1,34~7,1,34 +0,4,243~0,6,243 +6,8,250~6,9,250 +5,9,115~7,9,115 +6,3,245~9,3,245 +7,7,242~8,7,242 +9,7,53~9,9,53 +8,0,92~8,1,92 +6,4,280~8,4,280 +4,1,91~4,3,91 +4,1,47~4,3,47 +3,4,163~5,4,163 +6,2,154~6,5,154 +6,4,15~9,4,15 +1,8,54~3,8,54 +2,4,39~2,6,39 +1,8,212~3,8,212 +0,2,261~2,2,261 +7,7,199~7,9,199 +5,7,133~7,7,133 +7,2,122~7,5,122 +4,3,251~6,3,251 +5,4,5~8,4,5 +5,2,174~8,2,174 +5,5,250~5,8,250 +7,7,214~8,7,214 +6,5,264~7,5,264 +8,0,260~8,2,260 +3,7,112~5,7,112 +0,9,222~1,9,222 +8,4,240~8,7,240 +3,2,22~3,2,24 +4,0,216~7,0,216 +2,0,232~2,3,232 +0,6,50~3,6,50 +0,2,4~2,2,4 +7,6,167~7,7,167 +8,6,237~8,8,237 +9,7,254~9,9,254 +8,5,100~8,8,100 +5,6,1~7,6,1 +5,3,258~5,5,258 +1,7,106~2,7,106 +5,0,125~5,2,125 +2,0,14~2,0,15 +0,0,173~0,2,173 +0,5,58~2,5,58 +1,1,158~3,1,158 +0,8,177~0,9,177 +6,2,162~6,5,162 +7,6,154~7,9,154 +0,7,3~0,9,3 +5,5,147~5,7,147 +8,4,213~8,6,213 +2,8,34~4,8,34 +8,3,145~8,5,145 +9,2,205~9,4,205 +5,8,67~5,8,69 +1,5,45~1,8,45 +7,5,191~9,5,191 +4,2,65~4,4,65 +2,2,252~2,4,252 +5,4,256~5,6,256 +9,1,22~9,3,22 +5,9,70~8,9,70 +6,3,90~6,3,91 +4,2,193~4,3,193 +1,7,216~1,9,216 +0,3,182~0,5,182 +5,2,11~7,2,11 +5,4,112~5,4,114 +6,7,6~6,9,6 +0,8,9~0,8,11 +6,6,23~6,9,23 +6,3,157~6,4,157 +5,6,272~5,8,272 +0,3,283~2,3,283 +5,1,246~5,3,246 +8,9,228~8,9,230 +2,3,286~4,3,286 +9,2,252~9,4,252 +0,7,77~2,7,77 +0,4,92~0,4,94 +4,4,109~6,4,109 +5,2,181~5,5,181 +1,7,184~3,7,184 +8,0,93~8,1,93 +3,7,165~3,9,165 +1,2,254~1,5,254 +2,6,253~4,6,253 +5,7,66~8,7,66 +8,2,116~8,2,116 +1,1,54~3,1,54 +5,7,223~6,7,223 +4,4,44~4,6,44 +5,8,47~6,8,47 +2,3,15~4,3,15 +6,2,123~6,3,123 +0,2,254~0,2,256 +5,1,134~5,4,134 +3,0,196~5,0,196 +3,6,209~5,6,209 +4,8,175~4,9,175 +1,0,84~3,0,84 +6,7,134~6,7,135 +4,3,259~5,3,259 +9,3,44~9,6,44 +8,3,21~8,3,23 +7,2,127~9,2,127 +4,2,1~4,5,1 +6,9,203~7,9,203 +0,7,140~0,7,143 +1,8,249~1,9,249 +7,3,146~8,3,146 +2,7,212~3,7,212 +4,2,85~5,2,85 +6,5,146~6,8,146 +6,5,185~6,6,185 +5,3,42~5,6,42 +4,2,282~6,2,282 +2,6,199~5,6,199 +4,8,229~6,8,229 +4,9,71~5,9,71 +4,3,9~5,3,9 +6,0,252~6,3,252 +9,1,25~9,2,25 +4,6,225~4,9,225 +3,3,54~3,3,57 +7,0,64~9,0,64 +0,2,18~3,2,18 +5,6,103~7,6,103 +4,3,132~4,3,135 +4,3,154~5,3,154 +5,1,146~5,4,146 +0,3,95~0,4,95 +2,6,30~4,6,30 +1,5,121~1,7,121 +8,5,204~8,6,204 +6,2,113~8,2,113 +8,1,143~8,3,143 +5,0,201~5,3,201 +2,5,121~6,5,121 +1,2,235~1,3,235 +5,2,75~7,2,75 +5,1,7~5,4,7 +0,0,246~1,0,246 +9,4,207~9,7,207 +4,6,152~4,7,152 +0,4,128~2,4,128 +2,2,159~2,2,161 +9,5,12~9,7,12 +0,1,56~0,4,56 +8,3,20~8,5,20 +8,4,116~8,6,116 +3,3,176~5,3,176 +6,1,102~8,1,102 +2,3,152~5,3,152 +1,5,192~2,5,192 +6,4,3~8,4,3 +2,5,46~3,5,46 +0,6,56~2,6,56 +7,2,69~7,5,69 +6,7,95~6,7,98 +6,0,159~8,0,159 +3,5,95~5,5,95 +5,9,151~7,9,151 +5,3,29~5,3,30 +3,5,250~3,7,250 +6,2,218~8,2,218 +9,6,248~9,8,248 +8,6,211~8,9,211 +0,0,170~0,3,170 +2,7,70~3,7,70 +1,7,4~3,7,4 +1,5,203~2,5,203 +2,3,179~2,6,179 +2,0,35~4,0,35 +5,4,80~6,4,80 +3,6,210~3,8,210 +2,2,40~5,2,40 +7,1,82~7,1,84 +1,6,273~1,7,273 +6,9,114~8,9,114 +3,0,166~3,0,167 +3,3,51~3,3,52 +0,8,193~3,8,193 +9,5,240~9,6,240 +5,1,87~7,1,87 +1,2,227~3,2,227 +2,6,67~2,9,67 +3,6,163~5,6,163 +3,8,150~3,9,150 +1,1,277~3,1,277 +7,6,157~7,8,157 +0,0,249~0,3,249 +4,1,12~4,3,12 +6,0,13~9,0,13 +0,6,9~0,7,9 +0,4,21~0,6,21 +0,5,242~0,7,242 +1,7,223~1,9,223 +5,1,129~5,3,129 +1,9,203~4,9,203 +7,9,120~9,9,120 +4,6,269~6,6,269 +3,3,34~3,3,36 +0,3,250~1,3,250 +5,4,207~5,6,207 +9,5,96~9,8,96 +2,6,126~2,8,126 +2,6,270~3,6,270 +2,1,155~2,3,155 +1,5,109~1,7,109 +2,3,93~2,5,93 +1,3,53~1,6,53 +2,9,206~3,9,206 +3,8,260~4,8,260 +5,7,11~7,7,11 +1,7,103~3,7,103 +2,2,78~3,2,78 +0,5,14~2,5,14 +2,0,52~2,1,52 +1,7,270~1,9,270 +0,2,58~0,4,58 +3,4,166~4,4,166 +7,3,84~7,5,84 +3,9,21~3,9,21 +4,7,201~4,9,201 +6,6,293~6,7,293 +2,0,219~5,0,219 +2,7,222~3,7,222 +4,6,100~4,8,100 +9,3,142~9,5,142 +0,7,2~2,7,2 +0,2,206~0,3,206 +8,0,99~8,2,99 +4,2,80~8,2,80 +3,3,6~3,5,6 +1,0,4~3,0,4 +5,2,159~5,2,162 +7,6,249~7,8,249 +5,4,22~5,4,24 +0,1,168~1,1,168 +0,1,236~0,3,236 +4,3,207~4,6,207 +1,7,195~3,7,195 +2,5,279~5,5,279 +2,5,26~4,5,26 +0,4,158~3,4,158 +7,6,89~7,8,89 +1,8,33~2,8,33 +9,4,37~9,4,40 +4,7,92~7,7,92 +0,5,61~0,8,61 +6,4,79~8,4,79 +5,7,24~8,7,24 +2,2,148~4,2,148 +4,6,170~4,7,170 +2,1,135~2,3,135 +0,3,191~2,3,191 +1,9,221~3,9,221 +1,3,242~1,6,242 +9,6,50~9,7,50 +8,7,141~8,8,141 +2,6,263~2,7,263 +8,2,263~8,2,263 +3,8,15~3,9,15 +5,4,76~7,4,76 +6,3,111~6,7,111 +3,0,85~3,0,86 +1,0,150~3,0,150 +6,4,106~6,6,106 +6,0,132~6,0,134 +2,3,92~4,3,92 +3,7,157~3,7,157 +1,6,247~2,6,247 +3,5,160~3,8,160 +3,7,178~6,7,178 +4,3,273~6,3,273 +2,0,49~2,1,49 +1,9,226~2,9,226 +9,4,10~9,4,11 +2,5,158~4,5,158 +7,4,121~8,4,121 +4,5,198~4,7,198 +1,8,115~3,8,115 +1,6,111~3,6,111 +7,8,251~9,8,251 +1,6,36~3,6,36 +0,9,218~2,9,218 +8,2,217~8,4,217 +5,4,242~5,6,242 +7,6,16~9,6,16 +5,6,287~6,6,287 +5,8,162~8,8,162 +5,1,254~5,3,254 +1,9,1~3,9,1 +4,1,173~6,1,173 +6,0,191~6,3,191 +5,0,193~7,0,193 +7,9,202~9,9,202 +3,6,32~3,6,34 +6,0,102~7,0,102 +0,2,24~0,4,24 +5,3,91~5,3,94 +5,3,140~5,6,140 +3,7,110~5,7,110 +7,0,179~7,2,179 +3,0,227~5,0,227 +5,1,133~5,4,133 +0,5,19~1,5,19 +6,2,117~6,2,120 +6,4,57~6,7,57 +1,4,255~1,4,257 +4,8,202~5,8,202 +8,2,175~8,4,175 +3,3,32~3,3,33 +8,1,58~8,2,58 +1,1,30~1,1,32 +6,7,197~8,7,197 +3,8,60~3,8,63 +2,7,187~2,9,187 +3,6,174~3,7,174 +7,8,10~9,8,10 +7,0,196~7,1,196 +2,3,98~4,3,98 +6,4,172~6,6,172 +6,5,50~6,8,50 +4,2,129~4,5,129 +0,2,262~0,5,262 +4,9,6~5,9,6 +7,9,259~9,9,259 +1,0,228~1,2,228 +8,3,232~8,6,232 +7,2,157~7,2,158 +6,7,85~8,7,85 +5,7,273~5,8,273 +7,0,39~7,2,39 +4,2,8~5,2,8 +5,5,259~8,5,259 +0,6,160~0,7,160 +0,5,202~3,5,202 +4,9,150~7,9,150 +8,1,237~9,1,237 +6,3,166~6,6,166 +6,1,90~8,1,90 +0,7,85~0,7,87 +0,7,145~0,9,145 +8,5,266~9,5,266 +8,4,35~9,4,35 +2,3,30~2,4,30 +7,1,215~7,3,215 +6,3,210~6,5,210 +0,0,144~0,2,144 +7,6,111~7,9,111 +0,7,8~0,9,8 +0,2,156~2,2,156 +5,4,73~5,6,73 +0,9,77~2,9,77 +2,4,155~2,7,155 +3,6,267~5,6,267 +4,0,246~4,0,249 +2,1,144~5,1,144 +6,0,262~6,1,262 +6,1,78~6,2,78 +6,7,155~7,7,155 +3,4,150~3,6,150 +4,7,278~6,7,278 +8,0,161~8,0,164 +0,2,93~2,2,93 +6,7,9~8,7,9 +9,4,148~9,6,148 +7,9,257~9,9,257 +5,6,95~5,8,95 +2,6,234~4,6,234 +4,3,88~6,3,88 +5,3,225~6,3,225 +8,2,72~8,3,72 +6,4,61~6,6,61 +7,3,83~7,5,83 +1,1,254~1,1,254 +7,2,85~7,4,85 +1,4,100~1,6,100 +5,3,63~5,4,63 +2,2,15~4,2,15 +4,1,163~4,3,163 +2,2,75~2,4,75 +0,3,176~0,4,176 +0,2,45~0,6,45 +6,4,261~9,4,261 +0,0,78~2,0,78 +0,6,217~0,6,217 +0,6,131~0,6,132 +2,6,47~4,6,47 +9,6,158~9,7,158 +7,5,108~7,8,108 +8,7,1~8,7,1 +4,5,160~4,5,162 +0,6,8~2,6,8 +7,6,244~7,6,247 +8,5,268~8,8,268 +5,3,178~6,3,178 +6,7,179~6,7,181 +9,5,140~9,8,140 +3,3,248~3,5,248 +3,1,3~4,1,3 +6,7,169~8,7,169 +1,1,180~3,1,180 +2,0,87~2,0,90 +0,4,133~0,4,135 +6,6,242~7,6,242 +2,5,152~4,5,152 +0,5,64~2,5,64 +6,6,44~6,8,44 +2,5,24~2,6,24 +0,9,220~3,9,220 +0,2,55~0,5,55 +2,0,26~2,2,26 +4,8,6~4,8,8 +5,0,65~8,0,65 +0,8,63~2,8,63 +4,1,269~6,1,269 +4,1,80~6,1,80 +1,9,166~3,9,166 +0,6,212~3,6,212 +0,0,221~3,0,221 +0,4,67~1,4,67 +3,6,290~3,7,290 +6,7,191~7,7,191 +3,1,56~3,2,56 +9,2,129~9,3,129 +4,4,140~4,6,140 +0,6,91~0,8,91 +0,3,68~0,6,68 +5,3,242~7,3,242 +5,2,13~7,2,13 +6,5,52~8,5,52 +1,6,237~4,6,237 +7,0,160~9,0,160 +6,6,35~6,8,35 +5,0,68~5,0,70 +7,3,197~7,3,200 +2,0,165~2,0,167 +8,0,82~8,3,82 +3,2,226~5,2,226 +7,0,91~7,2,91 +0,9,59~3,9,59 +7,5,19~7,8,19 +0,0,152~0,2,152 +3,8,172~4,8,172 +5,1,79~8,1,79 +7,0,96~8,0,96 +3,0,141~4,0,141 +2,2,55~2,4,55 +9,3,269~9,5,269 +2,3,259~2,4,259 +7,2,74~7,4,74 +2,4,243~4,4,243 +5,1,101~5,2,101 +0,5,251~3,5,251 +8,0,115~8,2,115 +6,8,115~7,8,115 +4,3,272~4,6,272 +3,4,165~3,6,165 +4,2,152~7,2,152 +8,4,56~8,6,56 +3,9,285~5,9,285 +2,1,235~5,1,235 +8,5,235~8,7,235 +6,5,108~6,5,110 +0,1,142~2,1,142 +4,3,96~7,3,96 +9,6,47~9,9,47 +5,4,17~8,4,17 +3,7,258~3,9,258 +3,1,48~3,4,48 +4,2,234~6,2,234 +2,3,198~4,3,198 +4,0,240~4,2,240 +8,5,101~8,6,101 +2,5,225~4,5,225 +1,1,87~3,1,87 +9,0,169~9,0,171 +7,9,71~8,9,71 +4,3,35~7,3,35 +6,8,112~7,8,112 +0,0,163~3,0,163 +2,5,236~2,6,236 +1,2,19~1,4,19 +8,2,185~8,3,185 +4,6,7~7,6,7 +8,0,168~8,0,169 +0,6,176~0,9,176 +0,9,78~0,9,81 +7,2,154~8,2,154 +5,7,61~6,7,61 +1,3,56~1,3,57 +0,4,27~0,5,27 +5,7,106~5,9,106 +9,2,112~9,5,112 +5,6,134~7,6,134 +6,6,231~6,9,231 +2,6,254~4,6,254 +2,4,11~2,6,11 +5,0,127~6,0,127 +1,4,214~1,7,214 +2,2,56~2,4,56 +8,1,29~9,1,29 +1,3,213~3,3,213 +6,4,256~6,4,258 +6,7,150~7,7,150 +0,3,130~0,6,130 +6,7,244~8,7,244 +2,4,278~4,4,278 +5,9,66~6,9,66 +8,1,151~8,3,151 +6,0,213~6,4,213 +6,2,33~9,2,33 +6,6,41~9,6,41 +7,0,166~9,0,166 +5,5,150~5,5,153 +9,6,244~9,6,246 +8,2,238~8,2,238 +7,8,159~8,8,159 +4,5,30~5,5,30 +0,5,183~1,5,183 +5,6,253~5,9,253 +3,1,4~4,1,4 +3,9,16~3,9,16 +7,6,239~9,6,239 +3,3,259~3,3,260 +3,6,137~5,6,137 +3,1,93~3,4,93 +8,0,84~8,1,84 +5,3,166~5,4,166 +7,2,27~7,3,27 +2,2,186~4,2,186 +2,5,6~2,8,6 +4,9,16~4,9,16 +5,2,123~5,3,123 +3,2,195~4,2,195 +4,1,93~4,1,95 +0,1,155~0,1,155 +9,5,109~9,6,109 +5,3,237~5,5,237 +5,3,226~7,3,226 +6,3,180~8,3,180 +5,1,177~5,2,177 +2,2,123~2,6,123 +2,7,165~2,8,165 +6,0,1~6,2,1 +0,1,278~1,1,278 +3,2,149~3,4,149 +0,2,149~1,2,149 +0,7,198~2,7,198 +0,7,180~0,9,180 +4,3,20~4,6,20 +4,2,43~4,4,43 +4,1,239~6,1,239 +7,9,227~9,9,227 +0,5,131~1,5,131 +8,1,153~9,1,153 +3,5,69~5,5,69 +5,6,243~5,8,243 +6,4,195~6,7,195 +1,6,114~1,8,114 +7,7,247~7,9,247 +5,7,103~6,7,103 +9,4,4~9,6,4 +3,6,175~5,6,175 +2,7,178~2,9,178 +7,3,86~7,3,88 +0,1,176~0,1,178 +9,3,17~9,5,17 +5,6,227~5,8,227 +5,0,220~5,0,222 +6,4,189~6,7,189 +5,0,136~7,0,136 +5,6,14~5,7,14 +2,0,237~2,1,237 +4,1,257~8,1,257 +7,0,239~7,2,239 +9,6,62~9,8,62 +9,5,49~9,6,49 +7,1,246~7,4,246 +1,8,273~3,8,273 +5,6,290~7,6,290 +5,5,44~5,7,44 +2,7,105~4,7,105 +6,4,27~6,6,27 +7,3,229~7,6,229 +9,2,71~9,3,71 +3,1,248~3,1,251 +7,4,119~7,6,119 +6,5,54~8,5,54 +7,1,103~7,3,103 +6,4,115~7,4,115 +9,3,69~9,4,69 +2,1,86~2,4,86 +5,7,137~7,7,137 +6,4,113~8,4,113 +5,0,158~8,0,158 +1,3,149~1,5,149 +2,2,48~2,2,49 +1,6,197~1,8,197 +7,2,34~9,2,34 +1,3,281~1,5,281 +1,0,148~3,0,148 +1,0,70~1,2,70 +1,2,148~1,3,148 +5,0,129~7,0,129 +7,3,255~9,3,255 +6,2,112~6,3,112 +7,4,12~9,4,12 +1,9,272~1,9,272 +6,9,118~7,9,118 +3,7,256~5,7,256 +5,3,261~6,3,261 +8,2,124~8,4,124 +4,6,200~7,6,200 +2,1,47~2,3,47 +2,3,16~2,4,16 +3,2,241~3,4,241 +3,9,116~5,9,116 +4,4,51~4,5,51 +7,0,92~7,2,92 +4,8,224~7,8,224 +9,0,14~9,0,15 +5,6,259~5,7,259 +1,8,188~3,8,188 +8,4,283~8,7,283 +9,4,141~9,7,141 +4,0,51~4,2,51 +4,7,173~4,7,174 +7,2,182~9,2,182 +4,4,160~6,4,160 +3,4,44~3,5,44 +6,5,272~6,7,272 +4,3,149~4,6,149 +9,3,257~9,3,259 +0,3,89~0,6,89 +5,6,47~5,6,49 +0,5,230~2,5,230 +2,9,172~5,9,172 +8,1,130~8,4,130 +1,4,202~3,4,202 +7,2,41~7,2,43 +9,3,1~9,6,1 +7,1,71~7,3,71 +0,9,11~2,9,11 +1,7,163~4,7,163 +7,8,50~9,8,50 +7,7,68~7,9,68 +8,1,69~8,3,69 +8,6,229~8,8,229 +0,4,173~0,6,173 +4,6,228~6,6,228 +0,6,84~0,9,84 +1,5,7~3,5,7 +2,9,174~4,9,174 +5,1,36~5,3,36 +5,4,9~5,5,9 +6,7,2~6,7,5 +3,0,50~3,3,50 +4,8,102~7,8,102 +5,2,275~5,4,275 +4,2,233~4,4,233 +0,8,7~3,8,7 +8,5,284~8,6,284 +3,2,30~5,2,30 +2,1,178~3,1,178 +5,0,176~5,1,176 +9,0,113~9,2,113 +6,1,81~6,3,81 +4,4,273~5,4,273 +1,1,67~3,1,67 +5,4,158~7,4,158 +4,2,265~4,6,265 +6,2,24~9,2,24 +4,1,191~4,4,191 +1,5,228~2,5,228 +6,4,249~9,4,249 +1,1,145~1,4,145 +7,4,39~7,4,41 +0,5,246~0,8,246 +8,1,103~9,1,103 +2,0,243~4,0,243 +5,2,182~5,3,182 +5,5,155~5,5,155 +6,0,217~7,0,217 +0,8,60~2,8,60 +1,7,218~2,7,218 +1,8,1~2,8,1 +4,5,10~6,5,10 +5,2,144~5,5,144 +2,2,57~2,4,57 +3,2,197~3,2,199 +4,2,27~4,5,27 +5,1,191~5,3,191 +4,6,231~4,8,231 +7,1,146~9,1,146 +7,6,30~9,6,30 +1,3,257~2,3,257 +4,0,162~4,1,162 +4,7,263~4,9,263 +7,7,221~7,9,221 +3,2,33~3,2,37 +1,4,182~2,4,182 +9,4,243~9,7,243 +2,6,262~5,6,262 +7,6,170~7,8,170 +6,6,58~6,9,58 +4,5,72~5,5,72 +6,4,232~6,7,232 +0,6,284~0,8,284 +9,0,66~9,2,66 +5,3,61~7,3,61 +5,7,225~7,7,225 +3,6,39~3,8,39 +3,1,262~3,3,262 +9,2,20~9,3,20 +5,6,204~6,6,204 +6,6,101~6,9,101 +2,4,188~2,6,188 +5,2,19~5,5,19 +3,7,177~5,7,177 +1,5,37~1,8,37 +5,1,236~7,1,236 +8,5,7~8,8,7 +3,0,273~4,0,273 +4,2,149~4,2,151 +0,4,180~0,6,180 +5,6,164~8,6,164 +7,8,30~9,8,30 +4,8,61~7,8,61 +3,2,16~3,3,16 +6,1,174~7,1,174 +5,1,41~5,2,41 +4,6,123~6,6,123 +3,0,65~3,2,65 +1,1,231~1,4,231 +1,5,47~1,7,47 +0,5,140~0,6,140 +7,5,20~7,5,22 +9,2,145~9,4,145 +0,0,245~3,0,245 +6,1,244~8,1,244 +4,3,18~6,3,18 +6,2,216~6,5,216 +0,1,265~0,3,265 +8,2,70~8,5,70 +7,6,203~9,6,203 +5,5,277~5,7,277 +7,5,98~9,5,98 +3,1,245~3,3,245 +5,0,90~6,0,90 +4,0,17~7,0,17 +2,1,176~4,1,176 +0,0,75~3,0,75 +0,7,211~3,7,211 +1,0,182~1,1,182 +2,7,75~2,9,75 +0,3,233~2,3,233 +3,3,238~3,4,238 +4,3,195~6,3,195 +7,7,20~7,9,20 +2,5,20~2,8,20 diff --git a/aoc22/src/main.rs b/aoc22/src/main.rs new file mode 100644 index 0000000..a7f549e --- /dev/null +++ b/aoc22/src/main.rs @@ -0,0 +1,216 @@ +use std::collections::HashSet; +use std::error::Error; +use std::io::{self, Read, Write}; +use std::time::Instant; + +#[allow(unused_macros)] +macro_rules! err { + ($($tt:tt)*) => { Err(Box::::from(format!($($tt)*))) } +} + +type Result = ::std::result::Result>; + +type Coord = (isize, isize, isize); + +#[derive(Debug, Clone)] +struct Brick { + start: Coord, + end: Coord, + cubes: HashSet, +} + +fn parse_input>(input: T) -> Vec { + input + .as_ref() + .split_whitespace() + .map(|l| { + let (start, end) = l.split_once('~').unwrap(); + let mut start = start.split(',').map(|n| n.parse::()); + let mut end = end.split(',').map(|n| n.parse::()); + let start = ( + start.next().unwrap().unwrap(), + start.next().unwrap().unwrap(), + start.next().unwrap().unwrap(), + ); + let end = ( + end.next().unwrap().unwrap(), + end.next().unwrap().unwrap(), + end.next().unwrap().unwrap(), + ); + assert!(start.2 <= end.2); + Brick::new(start, end) + }) + .collect() +} + +impl Brick { + fn new(start: Coord, end: Coord) -> Self { + let mut b = Brick { + start, + end, + cubes: HashSet::new(), + }; + let mut set = HashSet::new(); + for x in start.0..=end.0 { + for y in start.1..=end.1 { + for z in start.2..=end.2 { + set.insert((x, y, z)); + } + } + } + + b.cubes = set; + b + } + fn falling(&self) -> Option { + if self.start.2 == 1 { + None + } else { + let mut start = self.start; + let mut end = self.end; + start.2 -= 1; + end.2 -= 1; + Some(Self::new(start, end)) + } + } + + fn is_intersect_hashset(&self, other: &Brick) -> bool { + self.cubes.iter().any(|p| other.cubes.contains(p)) + } + + // https://stackoverflow.com/questions/55220355/how-to-detect-whether-two-segments-in-3d-space-intersect + // https://stackoverflow.com/a/10288710 + #[allow(dead_code)] + fn is_intersect_algebra(&self, other: &Brick) -> bool { + fn dot(p1: Coord, p2: Coord) -> f64 { + (p1.0 * p2.0 + p1.1 * p2.1 + p1.2 * p2.2) as f64 + } + + fn norm2(p: Coord) -> f64 { + (p.0 * p.0 + p.1 * p.1 + p.2 * p.2) as f64 + } + + #[allow(dead_code)] + fn norm(p: Coord) -> f64 { + norm2(p).sqrt() + } + + fn cross(p1: Coord, p2: Coord) -> Coord { + ( + p1.1 * p2.2 - p2.1 * p1.2, + p1.2 * p2.0 - p2.2 * p1.0, + p1.0 * p2.1 - p2.0 * p1.1, + ) + } + + let da = ( + self.end.0 - self.start.0, + self.end.1 - self.start.1, + self.end.2 - self.start.2, + ); + + let db = ( + other.end.0 - other.start.0, + other.end.1 - other.start.1, + other.end.2 - other.start.2, + ); + + let dc = ( + other.start.0 - self.start.0, + other.start.1 - self.start.1, + other.start.2 - self.start.2, + ); + if dot(dc, cross(da, db)) != 0.0 { + return false; + } + + let s = dot(cross(dc, db), cross(da, db)) / norm2(cross(da, db)); + // https://stackoverflow.com/a/33825948 + let t = dot(cross(dc, da), cross(da, db)) / norm2(cross(da, db)); + + (0.0..=1.0).contains(&s) && (0.0..=1.0).contains(&t) + } +} + +fn part1(bricks: &[Brick]) -> Result { + let _start = Instant::now(); + + let mut bricks = bricks.to_vec(); + bricks.sort_by_key(|b| b.start.2); + + for i in 0..bricks.len() { + while let Some(next) = bricks[i].falling() { + if bricks[0..i] + .iter() + .rev() + .all(|b| !next.is_intersect_hashset(b)) + && bricks[i + 1..] + .iter() + .all(|b| !next.is_intersect_hashset(b)) + { + bricks[i] = next; + } else { + break; + } + } + } + + bricks.sort_by_key(|b| b.start.2); + let mut count = bricks.len(); + for i in 0..bricks.len() { + let mut temp = bricks.clone(); + temp.remove(i); + + for j in 0..temp.len() { + if let Some(next) = temp[j].falling() { + if temp[0..j] + .iter() + .rev() + .all(|b| !next.is_intersect_hashset(b)) + && temp[j + 1..].iter().all(|b| !next.is_intersect_hashset(b)) + { + count -= 1; + break; + } + } + } + } + + writeln!(io::stdout(), "Part 1: {count}")?; + writeln!(io::stdout(), "> Time elapsed is: {:?}", _start.elapsed())?; + Ok(count) +} + +fn main() -> Result<()> { + let mut input = String::new(); + io::stdin().read_to_string(&mut input)?; + + let bricks = parse_input(input); + part1(&bricks)?; + // part2()?; + Ok(()) +} + +#[test] +fn example_input() { + let input = "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"; + + assert!( + Brick::new((0, 1, 4), (2, 1, 4)).is_intersect_algebra(&Brick::new((1, 1, 4), (1, 1, 5))) + ); + let bricks = parse_input(input); + assert_eq!(part1(&bricks).unwrap(), 5); +} + +#[test] +fn real_input() { + let input = std::fs::read_to_string("input/input.txt").unwrap(); + let bricks = parse_input(input); + assert_eq!(part1(&bricks).unwrap(), 401); +}