Day 1
Day 1
Day 1 looked simple on the surface, but I ended up taking an approach that i learnt in my DSA lectures last semester.
how i approached the problem
The dial moves through numbers from 0 to 99 and wraps cleanly on both sides. That immediately made me think of a circular doubly linked list. I didn’t want to keep doing manual modular arithmetic every time the dial moved, so I figured it would be easier to represent the dial as an actual ring in memory.
A circular doubly linked list felt like a natural fit. Each node represented one number on the dial, and moving left or right became as simple as walking to prev or next.
building the dial
I created 100 nodes, linked them together, then closed the loop by connecting the last node back to the first. After that, I moved a pointer through the list until I reached the starting value of 50. From there, the pointer acted as the dial.
This structure ended up making the rotations really straightforward. The instructions from the input look like L68 or R5. Instead of jumping with math, I literally rotated through the list click by click.
counting for part 2
For part two, the puzzle wants the number of times the dial hits zero during any click. Since I was already moving one step at a time, I just checked the value on each step and increased a counter whenever it landed on zero.
here is my code in c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
int value;
struct Node *next;
struct Node *prev;
} Node;
int main() {
Node *head = NULL;
Node *prev = NULL;
for (int i = 0; i < 100; i++) {
Node *n = malloc(sizeof(Node));
n->value = i;
n->next = NULL;
n->prev = prev;
if (prev != NULL) {
prev->next = n;
} else {
head = n;
}
prev = n;
}
head->prev = prev;
prev->next = head;
Node *ptr = head;
while (ptr->value != 50)
ptr = ptr->next;
int countZero = 0;
char cmd[20];
while (scanf("%19s", cmd) == 1) {
char dir = cmd[0];
int steps = atoi(cmd + 1);
for (int i = 0; i < steps; i++) {
if (dir == 'L') {
ptr = ptr->prev;
} else {
ptr = ptr->next;
}
if (ptr->value == 0) {
countZero++;
}
}
}
printf("%d\n", countZero);
return 0;
}