aboutsummaryrefslogtreecommitdiff
path: root/day11/day11.c
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-11 11:24:34 +0000
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-11 11:24:34 +0000
commitc2744420f190800dac8bc9c17a058e3998291cc9 (patch)
tree112bff60ac419f2a3bbbb69ad0e7452d578b56d5 /day11/day11.c
parent0426961debc64dd2dbe9498e442a01bba39208d8 (diff)
Day 11
Diffstat (limited to 'day11/day11.c')
-rw-r--r--day11/day11.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/day11/day11.c b/day11/day11.c
new file mode 100644
index 0000000..3a12820
--- /dev/null
+++ b/day11/day11.c
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+char* incr(char password[8]) {
+ int i, rollover = 1;
+
+ for(i = 7; (i >= 0) && rollover; i--) {
+ if(password[i] == 'z') {
+ password[i] = 'a';
+ } else {
+ password[i] += 1;
+ rollover = 0;
+ }
+ }
+
+ return password;
+}
+
+char check(char* password) {
+ char i, flags = 2, firstpair = 0;
+
+ for(i = 0; i < 5; i++)
+ if(password[i+1] == password[i] + 1 && password[i+2] == password[i] + 2)
+ flags |= 1;
+
+ for(i = 0; i < 8; i++)
+ if(password[i] == 'i' || password[i] == 'o' || password[i] == 'l')
+ flags &= (~2);
+
+ for(i = 0; i < 7; i++) {
+ if(password[i] == password[i+1]) {
+ if(firstpair) {
+ if(password[i] != firstpair)
+ flags |= 4;
+ }
+ else
+ firstpair = password[i];
+ i++;
+ }
+ }
+
+ return (flags == 7);
+}
+
+char* next(char* password) {
+ char good = 0;
+
+ while(!good) {
+ password = incr(password);
+ good = check(password);
+ }
+
+ return password;
+}
+
+int main() {
+ char *password;
+ password = malloc(10*sizeof(char));
+ scanf("%s", password);
+
+ password = next(password);
+ printf("First new password: %s\n", password);
+
+ password = next(password);
+ printf("Second new password: %s\n", password);
+
+ free(password);
+
+ return 0;
+}