This commit is contained in:
matthias@arch 2022-12-15 20:16:46 +01:00
parent 06a09afccd
commit 0280e0c080
5 changed files with 291 additions and 1 deletions

3
14/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
sandtest.txt
.vimspector.json
day14

9
14/README.md Normal file
View File

@ -0,0 +1,9 @@
# [Day 14](https://adventofcode.com/2022/day/14)
:gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift::gift:
Today's language: **C++**
```shell
g++ -std=c++20 main.cpp -o day14 -lgzutil
./day14 i-dont-like-sand.txt
```

130
14/i-dont-like-sand.txt Normal file
View File

@ -0,0 +1,130 @@
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
510,43 -> 515,43
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
525,54 -> 537,54 -> 537,53
504,47 -> 509,47
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
489,136 -> 494,136
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
460,93 -> 464,93
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
469,91 -> 473,91
496,15 -> 496,16 -> 510,16
500,138 -> 505,138
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
469,87 -> 473,87
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
522,49 -> 527,49
463,91 -> 467,91
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
496,136 -> 501,136
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
472,93 -> 476,93
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
486,138 -> 491,138
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
481,130 -> 481,131 -> 494,131
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
514,45 -> 519,45
518,47 -> 523,47
501,49 -> 506,49
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
484,100 -> 488,100
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
481,102 -> 485,102
475,91 -> 479,91
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
478,93 -> 482,93
492,134 -> 497,134
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
472,89 -> 476,89
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
507,45 -> 512,45
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
472,84 -> 482,84
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
508,49 -> 513,49
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
466,93 -> 470,93
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
511,47 -> 516,47
493,138 -> 498,138
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
499,102 -> 503,102
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
525,54 -> 537,54 -> 537,53
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
496,15 -> 496,16 -> 510,16
487,98 -> 491,98
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
490,96 -> 494,96
496,100 -> 500,100
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
493,98 -> 497,98
466,89 -> 470,89
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
481,130 -> 481,131 -> 494,131
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
487,102 -> 491,102
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
493,102 -> 497,102
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
515,49 -> 520,49
490,100 -> 494,100

148
14/main.cpp Normal file
View File

@ -0,0 +1,148 @@
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <gz-util/string/utility.hpp>
#include <gz-util/string/from_string.hpp>
#include <gz-util/string/to_string.hpp>
#define PART2
#ifdef PART2
constexpr unsigned CAVE_WIDTH = 401;
#else
constexpr unsigned CAVE_WIDTH = 201;
#endif
constexpr unsigned CAVE_HEIGHT = 170;
constexpr char EMPTY = '.';
constexpr char WALL = '#';
constexpr char SAND = 'O';
class Cave {
public:
Cave(const char* filename);
char at(unsigned x, unsigned y) const { return cave.at(transform(x) + y * charsPerRow); }
void set(unsigned x, unsigned y, char c) { cave.at(transform(x) + y * charsPerRow) = c; }
void print() const { std::cout << cave << std::endl; };
// ret true if sand stays somewhere
bool addSand(unsigned x, unsigned y);
private:
// return true if sand was moved
bool moveSand(unsigned& x, unsigned& y);
// position to x-index, 500 should be in the middle
unsigned transform(unsigned x) const { return x - 500 + ((charsPerRow - 1) / 2) ; }
std::string cave;
unsigned charsPerRow = CAVE_WIDTH + 1; // +1 for newline
unsigned rows = CAVE_HEIGHT;
};
Cave::Cave(const char* filename) {
cave.resize(charsPerRow * rows, EMPTY);
for (std::string::size_type i = charsPerRow-1; i < cave.size(); i += charsPerRow) {
cave[i] = '\n';
}
std::ifstream file(filename);
std::vector<std::string_view> walls;
std::string buf;
unsigned maxY = 0;
if (!file.is_open()) {
throw std::runtime_error("Could not open file");
}
while (std::getline(file, buf)) {
walls = gz::util::splitStringInVector<std::string_view>(std::string_view(buf), " -> ");
unsigned startX = 0, startY = 0;
unsigned endX = 0, endY = 0;
std::cout << gz::toString(walls);
for (auto& wall : walls) {
auto comma = wall.find(',');
endX = gz::fromString<unsigned>(std::string(wall.substr(0, comma)));
endY = gz::fromString<unsigned>(std::string(wall.substr(comma + 1)));
if (endY > maxY) { maxY = endY; }
if (startX != 0) {
this->set(endX, endY, WALL); // will be left out by while loops
if (startX == endX) {
while (startY != endY) {
this->set(startX, startY, WALL);
startY += startY < endY ? 1 : -1;
}
}
else {
while (startX != endX) {
this->set(startX, startY, WALL);
startX += startX < endX ? 1 : -1;
}
}
}
startX = endX;
startY = endY;
}
}
#ifdef PART2
// draw line
for (unsigned x = 0; x < charsPerRow - 1; x++) {
cave.at(x + charsPerRow * (maxY+2)) = WALL;
}
#endif
}
bool Cave::addSand(unsigned x, unsigned y) {
if (at(x, y) == SAND) { return false; } // entrance blocked
set(x, y, SAND);
bool hasMoved = moveSand(x, y);
while (hasMoved) {
hasMoved = moveSand(x, y);
/* printf("Falling sand: x=%u, y=%u\n", x, y); */
if (y == rows - 1) { // falling through floor
set(x, y, EMPTY);
return false;
}
}
return true;
}
bool Cave::moveSand(unsigned& x, unsigned& y) {
// try falling down
if (at(x, y+1) == EMPTY) {
set(x, y, EMPTY);
set(x, ++y, SAND);
return true;
}
// try fall to left
else if (at(x-1, y+1) == EMPTY) {
set(x, y, EMPTY);
set(--x, ++y, SAND);
return true;
}
// try fall to right
else if (at(x+1, y+1) == EMPTY) {
set(x, y, EMPTY);
set(++x, ++y, SAND);
return true;
}
return false;
}
int main(int argc, char** argv) {
const char* filename;
if (argc != 2) {
/* printf("Need exactly one argument, the filename\n"); */
/* return 1; */
filename = "i-dont-like-sand.txt";
}
else {
filename = argv[1];
}
Cave cave(filename);
cave.print();
unsigned sandCount = 0;
while (cave.addSand(500, 0)) {
/* std::cout << "." << std::endl; */
sandCount++;
}
cave.set(500, 0, 'S');
cave.print();
printf("I DONT LIKE SAND %d/10!!!\n", sandCount);
}

View File

@ -22,7 +22,7 @@ Also, check out the repositories of my friends who do stuff in go, Visual Basic,
- **Assembly, x86-64**: day 4
- **Bash**: day 1, 7
- **C**: day 3, 4
- **C++**: day 2, 8
- **C++**: day 2, 8, 14
- **Java**: day 10
- **Javascript**: day 6
- **php**: day 9