summaryrefslogtreecommitdiff
path: root/2022/day05/main.c
blob: f6af2ea7b97bfdcc75f4082177d1577a586a4ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

#include <stdio.h>
#include <stdlib.h>

struct StackItem_;

typedef struct StackItem_ {
    char value;
    struct StackItem_ *next, *prev;
} StackItem;

typedef struct Stack_ {
    StackItem *top, *bottom;
} Stack;

void push_top(Stack *stack, StackItem *item) {
    if (stack->top)
    {
        StackItem* old = stack->top;
        old->next = item;
        item->prev = old;
    }
    if (!stack->top) stack->bottom = item;
    stack->top = item;
}

void push_back(Stack *stack, StackItem *item) {
    if (stack->bottom)
    {
        StackItem* old = stack->bottom;
        old->prev = item;
        item->next = old;
    }
    if (!stack->bottom) stack->top = item;
    stack->bottom = item;
}

StackItem* pop_top(Stack *stack) {
    StackItem* old = stack->top;
    stack->top = old->prev;
    if (old->prev != 0)
    {
        stack->top->next = 0;
    }
    old->next = old->prev = 0;

    return old;
}

StackItem* pop_back(Stack *stack) {
    StackItem* old = stack->bottom;
    stack->bottom = old->next;
    stack->bottom->prev = 0;
    old->next = old->prev = 0;

    return old;
}

void printCargo(Stack *cargo, size_t len) 
{
    for (int i = 0; i < len; i++)
    {
        for ( StackItem *e = cargo[i].bottom; e != 0; e = e->next )
        {
            printf("%c", e->value);
        }
        printf("\n");
    }
}

int main()
{
    char *line = 0;
    size_t len;
    Stack *cargo;
    size_t cargos;
    ssize_t nread;

    int amount, from, to;

    while ((nread = getline(&line, &len, stdin)) != 0)
    {
        if ( line[1] == '1' ) continue;
        if ( line[0] == '\n' ) break;
        if ( !cargo ) {
            cargos = (nread+1)/4;
            cargo = (Stack*)malloc( sizeof(Stack) * cargos );
        }

        for (int i = 0; i < cargos; i++ ) {
            char load = line[i*4+1];
            if ( load != ' ' ) {
                StackItem *box = malloc(sizeof(StackItem));
                box->value = line[i*4+1];
                box->next = box->prev = 0;
                push_back(&cargo[i], box);
            } 
        }
    }

    printCargo(cargo, cargos);
    printf("---------------\n");

    while (scanf("move %d from %d to %d\n", &amount, &from, &to) != EOF)
    {
        printf( "%d %d %d\n", amount, from, to );
        for (int i = 0; i < amount; i++)
        {
            StackItem *item = pop_top(&cargo[from-1]);
            push_top(&cargo[to-1], item);
            
            printCargo(cargo, cargos);
            printf("---------------\n");
        }
    }

    for ( int i = 0; i < cargos; i++ )
    {
        printf( "%c", cargo[i].top->value );
    }
    printf("\n");
}