summaryrefslogtreecommitdiff
path: root/2023/01/src/part2.cpp
blob: 77b5067ddc56dd2c2e2bcc7f07ab6ffb730a2c68 (plain) (blame)
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
#include <iostream>
#include <fstream>
#include <vector>

int update(int status[9], char c)
{
    static const std::vector<std::string> words{
        "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine"
    };

    int ret{};

    for (int a{}; a < words.size(); ++a)
    {
        if (words[a][status[a]] == c)
        {
            ++status[a];
        }
        else
        {
            status[a] = words[a][0] == c;
        }

        if (status[a] == words[a].size())
        {
            ret = a+1;
        }
    }
    return ret;
}

int search(const std::string& line)
{
    int status[9]{};

    int fst{}, lst{};
    for (char c : line)
    {
        int d = update(status, c);
        if (std::isdigit(c))
        {
            d = c - '0';
        }

        if (d > 0)
        {
            if (fst <= 0)
            {
                fst = d;
            }
            lst = d;
        }
    }
    return fst * 10 + lst;
}


int main(void)
{
    int answer{};

    std::ifstream input{ "./resources/input.txt" };
    if (input)
    {
        std::string line;
        while(not std::getline(input, line).eof())
        {
            answer += search(line);
        }

    }
    input.close();

    std::cout << answer << std::endl;
    return 0;
}