Guest User

Untitled

a guest
Dec 9th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "advent.h"
  3.  
  4. long long compute_length(int n) {
  5.     enum {Normal, Read_NChars, Read_Times};
  6.     long long length = 0;
  7.     char state = Normal;
  8.     int nchars, times;
  9.  
  10.     while(n) {
  11.         if (!Serial.available())
  12.             continue;
  13.            
  14.         char c = Serial.read();
  15.         if (c <= 32) {
  16.             return length;
  17.         }
  18.         n--;
  19.         switch(state) {
  20.             case Normal:
  21.                 if (c == '(') {
  22.                     state = Read_NChars;
  23.                     nchars = 0;
  24.                 }
  25.                 else {
  26.                     length++;
  27.                 }
  28.                 break;
  29.             case Read_NChars:
  30.                 if (c == 'x') {
  31.                     state = Read_Times;
  32.                     times = 0;
  33.                 }
  34.                 else {
  35.                     nchars = nchars * 10 + c - '0';
  36.                 }
  37.                 break;
  38.             case Read_Times:
  39.                 if (c == ')') {
  40.                     state = Normal;
  41.                     length += times * compute_length(nchars);
  42.                     n -= nchars;
  43.                     digitalWrite(2, HIGH);
  44.                     showNumber(nchars, 4);
  45.                     showNumber(times, 0);
  46.                     digitalWrite(2, LOW);
  47.                 }
  48.                 else {
  49.                     times = times * 10 + c - '0';
  50.                 }
  51.                 break;
  52.         }
  53.     }
  54.     return length;
  55. }
  56.  
  57. long long tot_length;
  58.  
  59. void setup() {
  60.     pinMode(2, OUTPUT);
  61.     Serial.begin(9600);
  62.     tot_length = compute_length(100000);
  63. }
  64.  
  65. void loop() {
  66.     for(int pos = -11; pos < 8; pos++) {
  67.         lcd.clearDisplay(0);
  68.         showNumber(tot_length, pos, 12);
  69.         tone(2, 30, 290);
  70.         delay(300);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment