Created
May 7, 2017 09:38
-
-
Save b4284/5920493b1e148ad474baa65dea70d2ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdbool.h> | |
| #include <ctype.h> | |
| #include <string.h> | |
| typedef struct { | |
| char hr1; | |
| char hr2; | |
| char delim1; | |
| char min1; | |
| char min2; | |
| char delim2; | |
| char sec1; | |
| char sec2; | |
| char nul; | |
| } time_string_t; | |
| void advance_one_second(time_string_t *t) | |
| { | |
| if (t->sec2 != '9') { | |
| t->sec2 += 1; | |
| return; | |
| } | |
| t->sec2 = '0'; | |
| if (t->sec1 != '5') { | |
| t->sec1 += 1; | |
| return; | |
| } | |
| t->sec1 = '0'; | |
| if (t->min2 != '9') { | |
| t->min2 += 1; | |
| return; | |
| } | |
| t->min2 = '0'; | |
| if (t->min1 != '5') { | |
| t->min1 += 1; | |
| return; | |
| } | |
| t->min1 = '0'; | |
| if (t->hr1 == '2' && t->hr2 == '3') { | |
| t->hr1 = '0'; | |
| t->hr2 = '0'; | |
| return; | |
| } | |
| if (t->hr2 != '9') { | |
| t->hr2 += 1; | |
| return; | |
| } | |
| t->hr2 = '0'; | |
| t->hr1 += 1; | |
| } | |
| bool is_interesting_point(const time_string_t *t) | |
| { | |
| char digcount[10] = { 0 }; | |
| digcount[t->hr1 - '0'] += 1; | |
| digcount[t->hr2 - '0'] += 1; | |
| digcount[t->min1 - '0'] += 1; | |
| digcount[t->min2 - '0'] += 1; | |
| digcount[t->sec1 - '0'] += 1; | |
| digcount[t->sec2 - '0'] += 1; | |
| int digs = 0; | |
| for (unsigned int i = 0; i < sizeof digcount; ++i) { | |
| if (digcount[i] > 0) { | |
| digs += 1; | |
| } | |
| } | |
| if (digs > 2) { | |
| return false; | |
| } else { | |
| return true; | |
| } | |
| } | |
| int find_interesting_points(time_string_t t1, time_string_t t2) | |
| { | |
| advance_one_second(&t2); | |
| int count = 0; | |
| do { | |
| if (is_interesting_point(&t1)) { | |
| count += 1; | |
| } | |
| advance_one_second(&t1); | |
| } while (memcmp(&t1, &t2, sizeof t1) != 0); | |
| return count; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| time_string_t t1; | |
| time_string_t t2; | |
| if (argc < 3) { | |
| return 1; | |
| } else { | |
| memcpy(&t1, argv[1], sizeof t1); | |
| memcpy(&t2, argv[2], sizeof t2); | |
| printf("number of interesting points between %s and %s is %d", | |
| argv[1], argv[2], find_interesting_points(t1, t2)); | |
| return 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment