summaryrefslogtreecommitdiff
path: root/2023/06/src/part2.cpp
blob: db35b577f2d24163617c35d250500a4cd12a7c24 (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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <vector>
#include <cmath>

int main(void)
{
    long time{};
    long distance{};

    std::ifstream input{ "resources/input.txt" };
    if (input.is_open())
    {
        long n;
        std::string line;

        /* Time */
        std::getline(input,line);
        for(unsigned char c : line)
        {
            if (std::isdigit(c))
            {
                time *= 10;
                time += c - '0';
            }
        }

        /* Distance */
        std::getline(input,line);
        for(unsigned char c : line)
        {
            if (std::isdigit(c))
            {
                distance *= 10;
                distance += c - '0';
            }
        }

    }
    input.close();

    double delta = std::sqrt(time * time - 4 * distance);
    long from{ static_cast<long>(1 + (time - delta) / 2)};
    long to{ static_cast<long>(std::ceil((time + delta) / 2)) };

    std::cout << to - from << std::endl;
    return 0;
}