File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Failedcode \Aoc2022 \Days ;
4
4
5
+ use Failedcode \Aoc2022 \Utils ;
6
+
5
7
abstract class AbstractDay
6
8
{
9
+ protected Utils $ util ;
10
+
11
+ public function __construct ()
12
+ {
13
10000
code>
+ $ this ->util = new Utils ();
14
+ }
15
+
7
16
public function solve_part_1 (): string
8
17
{
9
18
return "TODO " ;
Original file line number Diff line number Diff line change 5
5
class Day1 extends AbstractDay
6
6
{
7
7
8
+ public function solve_part_1 (): string
9
+ {
10
+ $ calories = $ this ->getCalories ();
11
+ return max ($ calories );
12
+ }
13
+
14
+ public function solve_part_2 (): string
15
+ {
16
+ $ calories = $ this ->getCalories ();
17
+ $ caloriesMax = 0 ;
18
+ for ($ i = 0 ; $ i < 3 ; $ i += 1 ) {
19
+ $ maxVal = max ($ calories );
20
+ $ position = array_search ($ maxVal , $ calories );
21
+ unset($ calories [$ position ]);
22
+ $ caloriesMax += (int )$ maxVal ;
23
+ }
24
+ return $ caloriesMax ;
25
+ }
26
+
27
+ public function getCalories (): array
28
+ {
29
+ $ list = $ this ->util ->loadInput (1 );
30
+ $ calories = [];
31
+ $ currentSum = 0 ;
32
+ foreach ($ list as $ row ) {
33
+ if (trim ($ row ) === '' ) {
34
+ $ calories [] = $ currentSum ;
35
+ $ currentSum = 0 ;
36
+ continue ;
37
+ }
38
+ $ currentSum += (int )$ row ;
39
+ }
40
+ return $ calories ;
41
+ }
8
42
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Failedcode \Aoc2022 ;
4
+
5
+ class Utils
6
+ {
7
+ public function loadInput ($ day , $ type ="day " ): array
8
+ {
9
+ $ filePath = "input/ {$ type }{$ day }.txt " ;
10
+ if (!file_exists ($ filePath )) {
11
+ // TODO: download input
12
+ throw new \Exception ("Input missing: ' $ filePath' " , 1670457206416 );
13
+ }
14
+ $ content = file_get_contents ($ filePath );
15
+ return explode ("\n" , $ content );
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments