diff --git a/public/Day.php b/public/Day.php new file mode 100644 index 0000000..214e5bf --- /dev/null +++ b/public/Day.php @@ -0,0 +1,16 @@ + */ + protected array $input; + + public function __construct() { + $number = explode("Day", get_class($this))[1]; + + $this->input = explode("\n", file_get_contents("../input/$number.txt"), -1); + } + + abstract function part1(): string; + abstract function part2(): string; +} \ No newline at end of file diff --git a/public/days/Day1.php b/public/days/Day1.php new file mode 100644 index 0000000..532ad58 --- /dev/null +++ b/public/days/Day1.php @@ -0,0 +1,46 @@ +input as $line){ + [$left[], $right[]] = array_map("intval", explode(" ", $line)); + } + + sort($left); + sort($right); + + $sum = 0; + + for ($i = 0; $i < count($left); $i++){ + $sum += abs($left[$i] - $right[$i]); + } + + return strval($sum); + } + + function part2(): string { + $left = []; + $rightCounts = []; + + foreach ($this->input as $line){ + [$left[], $right] = array_map("intval", explode(" ", $line)); + if (array_key_exists($right, $rightCounts)){ + $rightCounts[$right]++; + } else { + $rightCounts[$right] = 1; + } + } + + $sum = 0; + + foreach ($left as $number){ + $sum += $number * ($rightCounts[$number] ?? 0); + } + + return strval($sum); + } +} \ No newline at end of file diff --git a/public/fetch.php b/public/fetch.php index e4ab226..b02ca24 100644 --- a/public/fetch.php +++ b/public/fetch.php @@ -8,7 +8,7 @@ foreach (parse_ini_file("../.env") as $key => $value){ for ($i = 1; $i <= 25; $i++){ $context = stream_context_create([ 'http' => [ - 'header' => 'Cookie: session=' . $_ENV["SESSION_COOKIE"] . '\r\n' + 'header' => 'Cookie: session=' . $_ENV["SESSION_COOKIE"] . "\r\n" ] ]); $contents = file_get_contents("https://adventofcode.com/2024/day/$i/input", context: $context); diff --git a/public/run.php b/public/run.php new file mode 100644 index 0000000..b76c368 --- /dev/null +++ b/public/run.php @@ -0,0 +1,38 @@ +$method(); + $t2 = microtime(true); + + $time = $t2 - $t1; + + $response = [ + 'result' => $result, + 'time' => $time, + 'data' => null + ]; + + + echo json_encode($response); + die(); + } + } +} \ No newline at end of file