Advertisement
mosredna

AoC 2021 day 11

Dec 11th, 2021 (edited)
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal.h>
  4. #include <LedControl.h>
  5.  
  6. LedControl lc = LedControl(7, 9, 8);
  7. const int rs = 12, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 5;
  8. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  9.  
  10. int data[10][10] = {
  11.     {4,7,2,1,2,2,4,6,6,3},
  12.     {6,8,7,5,4,1,5,2,7,6},
  13.     {2,7,4,2,4,4,8,4,2,8},
  14.     {4,8,7,8,2,3,1,5,5,6},
  15.     {5,6,8,4,6,4,3,7,4,3},
  16.     {3,5,5,3,6,8,1,8,6,6},
  17.     {4,7,8,8,1,8,3,6,2,5},
  18.     {4,2,5,5,8,5,6,5,3,2},
  19.     {1,4,1,5,8,1,8,7,7,5},
  20.     {2,3,2,6,8,8,6,1,2,5}
  21. };
  22.  
  23. int flashes = 0;
  24. void flash(int x,int y){
  25.     flashes++;
  26.     for (int yy = y-1; yy  <= y+1; yy++) {
  27.         for (int xx = x-1; xx  <= x+1; xx++) {
  28.             if((x==xx && y == yy) || xx<0||yy<0||xx>9||yy>9)continue;
  29.             data[yy][xx]++;
  30.             if(data[yy][xx]==10){
  31.                 flash(xx,yy);
  32.                 data[yy][xx]++;
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. bool step(){
  39.  
  40.     for (int y = 0; y  < 10; y++) {
  41.         for (int x = 0; x  < 10; x++) {
  42.             data[y][x]++;
  43.             if(data[y][x]==10){
  44.                 flash(x,y);
  45.                 data[y][x]++;
  46.             }
  47.         }
  48.     }
  49.     int amount = 0;
  50.     for (int y = 0; y  < 10; y++) {
  51.         for (int x = 0; x  < 10; x++) {
  52.             if(data[y][x] > 9)
  53.             {
  54.                 lc.setLed(0,x,y,true);
  55.                 amount ++;
  56.                 data[y][x] = 0;
  57.             }
  58.             else{
  59.                  lc.setLed(0,x,y,false);
  60.             }
  61.         }
  62.     }
  63.     if(amount == 100){
  64.         return true;
  65.     }
  66.     return false;
  67. }
  68.  
  69. int steps = 1;
  70.  
  71. void setup() {
  72.     Serial.begin(9600);
  73.     while (!Serial);
  74.  
  75.     lc.shutdown(0,false);
  76.     lc.setIntensity(0,8);
  77.     lc.clearDisplay(0);
  78.    
  79.     lcd.begin(16, 2);
  80.     lcd.setCursor(0, 0);
  81.     lcd.print(" FLASHES! ");
  82.     lcd.setCursor(0, 1);
  83.     lcd.print( 0 ); delay(1000);
  84.  
  85.     while(!step()){
  86.         steps++;
  87.         lcd.setCursor(0, 1);
  88.         lcd.print( steps);
  89.         delay(25);
  90.     }
  91.  
  92.     delay(1000);
  93.     lc.clearDisplay(0);
  94. }
  95.  
  96. void loop() {
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement