day2p2 completed
This commit is contained in:
BIN
day1/day1p2
Executable file
BIN
day1/day1p2
Executable file
Binary file not shown.
96
day1/day1p2.c
Normal file
96
day1/day1p2.c
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
Dial from 0–99, start at 50.
|
||||||
|
You get lines like L68 or R5:
|
||||||
|
L = move left (subtract)
|
||||||
|
R = move right (add)
|
||||||
|
Wrap around with modulo 100.
|
||||||
|
After each move, check if the dial is on 0.
|
||||||
|
The password = how many times you land on 0 during the whole sequence.
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
printf("Advent Of Code Day 1 part 2\n\n");
|
||||||
|
|
||||||
|
// remove argc so gcc wont cry
|
||||||
|
(void)argc;
|
||||||
|
|
||||||
|
// base vars
|
||||||
|
int start = 50;
|
||||||
|
int pos = start;
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
// read the input file
|
||||||
|
char* inpf = NULL;
|
||||||
|
inpf = argv[1];
|
||||||
|
if (inpf == NULL) inpf = "input.txt";
|
||||||
|
|
||||||
|
FILE* ptr = fopen(inpf, "r");
|
||||||
|
if(ptr == NULL) {
|
||||||
|
printf("error: no such file %s\n", inpf);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("first position: %d\n", pos);
|
||||||
|
// process instructions
|
||||||
|
char line[9]; // 8 chars + eol
|
||||||
|
while(fscanf(ptr, "%9s", line) == 1) {
|
||||||
|
char drc = line[0];
|
||||||
|
int num = atoi(&line[1]);
|
||||||
|
printf("%c %d\n", drc, num);
|
||||||
|
|
||||||
|
//if (drc == 'R') pos += num;
|
||||||
|
//if (drc == 'L') pos -= num;
|
||||||
|
|
||||||
|
if (drc == 'R') {
|
||||||
|
for(int i = 0; i < num; i++) {
|
||||||
|
if (pos + 1 < 100) {
|
||||||
|
pos += 1;
|
||||||
|
} else {
|
||||||
|
pos += 1;
|
||||||
|
pos -= 100;
|
||||||
|
//printf("overflow -=100\n");
|
||||||
|
}
|
||||||
|
if (pos == 0) counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (drc == 'L') {
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
if (pos - 1 >= 0) {
|
||||||
|
pos -= 1;
|
||||||
|
} else {
|
||||||
|
pos -= 1;
|
||||||
|
pos += 100;
|
||||||
|
}
|
||||||
|
if (pos == 0) counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (pos > 99) {
|
||||||
|
//pos -= 100;
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
if (pos == 0) counter++;
|
||||||
|
pos -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (pos < 0){
|
||||||
|
//pos += 100;
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
if (pos == 0) counter++;
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//if (pos == 0) counter++;
|
||||||
|
|
||||||
|
printf("current position: %d\n", pos);
|
||||||
|
printf("counter is: %d\n", counter);
|
||||||
|
}
|
||||||
|
|
||||||
|
//printf("current position: %d\n", pos);
|
||||||
|
printf("\npassword is: %d\n", counter);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
9
day1/input_day2.tests.txt
Normal file
9
day1/input_day2.tests.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
R1000
|
||||||
|
L1000
|
||||||
|
L50
|
||||||
|
R1
|
||||||
|
L1
|
||||||
|
L1
|
||||||
|
R1
|
||||||
|
R100
|
||||||
|
R1
|
||||||
Reference in New Issue
Block a user