Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4.  
  5. int main(int argc, char ** argv) {
  6.  if(argc < 2) printf("need input\n");
  7.  uint32_t limit = argc < 3 ? 25000000 : strtoul(argv[2], NULL, 10);
  8.  uint64_t input = 0;
  9.  char * c = argv[1];
  10.  while(*++c);
  11.  while(c-- != argv[1]) {
  12.   input <<= 8;
  13.   input += *c - '0';
  14.  }
  15.  
  16.  uint8_t * recipes = malloc(sizeof(uint8_t)*limit);
  17.  recipes[0] = 3;
  18.  recipes[1] = 7;
  19.  uint32_t elf1=0, elf2=1, len=2;
  20.  uint8_t score;
  21.  for(int i=2;i<limit;i++) recipes[i] = 0xae;
  22.  
  23.  while (len < limit) {
  24.   score = recipes[elf1] + recipes[elf2];
  25.   if (score < 10) {
  26.    recipes[len] = score;
  27.    len += 1;
  28.   } else {
  29.    recipes[len]   = score/10;
  30.    recipes[len+1] = score%10;
  31.    len += 2;
  32.   }
  33.   elf1 = (elf1 + recipes[elf1] + 1) % len;
  34.   elf2 = (elf2 + recipes[elf2] + 1) % len;
  35.   if (((*(uint64_t*)(recipes+len-6) & 0xffffffffffff) == input)
  36.     ||((*(uint64_t*)(recipes+len-7) & 0xffffffffffff) == input))
  37.    break;
  38.  }
  39.  if (len >= limit)
  40.   printf("Limit %d reached, try increasing (arg 2)\n", limit);
  41.  else
  42.   printf("%u\n", len-(score<10?6:7));
  43.  free(recipes);
  44.  return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement