aboutsummaryrefslogtreecommitdiffstats
path: root/day14/part2.cpp
blob: 077739578c82fb2e62899814e233d0b95cfe8929 (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
#include <array>
#include <iostream>
#include <string>

int consumeInt(std::string& s)
{
    int n = 0;
    int i;

    for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
        n = n * 10 + s[i] - '0';

    s = s.substr(i);
    return n;
}

int main()
{
    std::array<std::array<char, 480>, 165> map;
    std::string line;

    for (auto& m : map)
        m.fill('.');

    while (1) {
        std::getline(std::cin, line);
        if (std::cin.eof())
            break;

        auto px = consumeInt(line);
        line = line.substr(1);
        auto py = consumeInt(line);

        while (!line.empty()) {
            line = line.substr(4);
            auto x = consumeInt(line);
            line = line.substr(1);
            auto y = consumeInt(line);

            if (x != px) {
                for (auto i = std::min(x, px); i <= std::max(x, px); ++i)
                    map[y][500 - i + map[y].size() / 2] = '#';
            } else {
                for (auto i = std::min(y, py); i <= std::max(y, py); ++i)
                    map[i][500 - x + map[y].size() / 2] = '#';
            }

            px = x;
            py = y;
        }
    }

    int sands = 0;
    while (map[0][map[0].size() / 2] != '*') {
        ++sands;
        int x = map[0].size() / 2, y = 0;
        while (1) {
            if (y > map.size() - 2) {
                map[y][x] = '*';
                break;
            } else if (map[y + 1][x] == '.') {
                ++y;
            } else if (map[y + 1][x + 1] == '.') {
                ++y, ++x;
            } else if (map[y + 1][x - 1] == '.') {
                ++y, --x;
            } else {
                map[y][x] = '*';
                break;
            }
        }
    }

    for (auto& m : map) {
        for (auto& n : m)
            std::cout << n;
        std::cout << std::endl;
    }

    std::cout << sands << std::endl;

    return 0;
}