diff options
Diffstat (limited to '2017/5.c')
-rw-r--r-- | 2017/5.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/2017/5.c b/2017/5.c new file mode 100644 index 0000000..c782816 --- /dev/null +++ b/2017/5.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> + + +int main(void){ + freopen("5.in","r",stdin); + + int capjumps=128,numjumps=0; + int *jumps=malloc(capjumps*sizeof(int)); + + while(true){ + int target; + scanf("%d",&target); + if(feof(stdin))break; + if(numjumps==capjumps){ + capjumps*=2; + jumps=realloc(jumps,capjumps*sizeof(int)); + } + jumps[numjumps++]=target; + } + + int *jumps1=malloc(numjumps*sizeof(int)); + memcpy(jumps1,jumps,numjumps*sizeof(int)); + + int at=0; + int nsteps=0; + while(0<=at&&at<numjumps){ + at+=jumps1[at]++; + nsteps++; + } + printf("%d\n",nsteps); + + memcpy(jumps1,jumps,numjumps*sizeof(int)); + + at=0; + nsteps=0; + while(0<=at&&at<numjumps){ + int next=at+jumps1[at]; + if(jumps1[at]>=3)jumps1[at]--; + else jumps1[at]++; + at=next; + nsteps++; + } + printf("%d\n",nsteps); + + free(jumps1); + free(jumps); +} |