Passed
Push — master ( 8c822c...e5da76 )
by Daan
01:27
created

Day1::loadInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DaanMooij\AdventOfCode\Year2018;
4
5
use DaanMooij\AdventOfCode\Day;
6
7
class Day1 implements Day
8
{
9
    /**
10
     * @var array
11
     */
12
    private array $changes;
13
14
    public function loadInput(): void
15
    {
16
        $file = fopen(__DIR__ . '/input/day-1.txt', "r");
17
        while (!feof($file)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
        while (!feof(/** @scrutinizer ignore-type */ $file)) {
Loading history...
18
            $this->changes[] = intval(fgets($file));
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
            $this->changes[] = intval(fgets(/** @scrutinizer ignore-type */ $file));
Loading history...
19
        }
20
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
21
    }
22
23
    public function solve()
24
    {
25
        $result = $this->calculateFrequency();
26
27
        printf('The resulting frequency is: %s', $result);
28
    }
29
30
    public function setChanges(array $changes): void
31
    {
32
        $this->changes = $changes;
33
    }
34
35
    public function getChanges(): array
36
    {
37
        return $this->changes;
38
    }
39
40
    /**
41
     * @param int $frequency -> The starting frequency
42
     * @return int
43
     */
44
    public function calculateFrequency(int $frequency = 0): int
45
    {
46
        foreach ($this->changes as $change) {
47
            $frequency += $change;
48
        }
49
50
        return $frequency;
51
    }
52
53
    /**
54
     * @param int $frequency -> The starting frequency
55
     * @return int
56
     */
57
    public function getFrequencyReachedTwice(int $frequency = 0): int
58
    {
59
        $memory = [$frequency => 1];
60
61
        $found = false;
62
        while (!$found) {
63
            foreach ($this->changes as $change) {
64
                $frequency += $change;
65
66
                if (isset($memory[$frequency])) {
67
                    $found = true;
68
                    break;
69
                }
70
71
                $memory[$frequency] = 1;
72
            }
73
        }
74
75
        return $frequency;
76
    }
77
}
78