/* CLAB / 2023 / LT2 */

#include "common.h"

#define NUM_BINS 10

int hist[NUM_BINS];

void construct_histogram(void)
{
    unsigned int x, i;

    while (1 == scanf("%d", &x)) {
        if (x < 1 || x >= 1000) {
            fprintf(stderr, "Skipping invalid input %d", x);
            continue;
        }
        hist[x / 100]++;
    }
    for (i = 0; i < NUM_BINS-1; i++)
        printf("%d ", hist[i]);
    printf("%d\n", hist[i]);
    return;
}

void draw_histogram(void)
{
    int i, j, min, max;

    scanf("%d", &hist[0]);
    min = max = hist[0];
    for (i = 1; i < NUM_BINS; i++) {
        scanf("%d", &hist[i]);
        if (hist[i] > max)
            max = hist[i];
        else if (hist[i] < min)
            min = hist[i];
    }
    for (j = max; j >= 1; j--) {
        for (i = 0; i < NUM_BINS; i++)
            if (hist[i] > 0 && hist[i] >= j) printf("#");
            else printf(" ");
        putchar('\n');
    }
    if (min >= 0) return;
    for (i = 0; i < NUM_BINS; i++) putchar('-');
    putchar('\n');
    for (j = -1; j >= min; j--) {
        for (i = 0; i < NUM_BINS; i++)
            if (hist[i] < 0 && hist[i] <= j) putchar('#');
            else putchar(' ');
        putchar('\n');
    }
    return;
}

void traverse_histogram(void)
{
    int i, distance;

    for (i = 0; i < NUM_BINS; i++)
        scanf("%d", &hist[i]);
    distance = hist[0];
    for (i = 1; i < NUM_BINS; i++)
        distance += abs(hist[i] - hist[i-1]);
    distance += hist[i-1] + NUM_BINS;
    printf("%d\n", distance);
    return;
}

int main(int ac, char *av[])
{

    if (ac < 2)
        ERR_MESG("Usage: histogram <a | b | c>");
    switch (av[1][0]) {
    case 'a':
        construct_histogram();
        break;
    case 'b':
        draw_histogram();
        break;
    case 'c':
        traverse_histogram();
        break;
    }
    return 0;
}
