Compare commits
43 Commits
3bab0c14e3
...
main
Author | SHA1 | Date | |
---|---|---|---|
b6a90cf203 | |||
6358f4e466 | |||
d646d753a6 | |||
9dce552df2 | |||
aca4a85101 | |||
7dd87b8be8 | |||
2b07571ffd | |||
52a18f5020 | |||
93a6896c5b | |||
0d4b5c407a | |||
02705353ef | |||
4d8de674b7 | |||
8dc9727141 | |||
964d1ad793 | |||
b8063e48e0 | |||
0f250ab844 | |||
1066916742 | |||
2db41dde7c | |||
e47dc820d5 | |||
9f14ee2dd0 | |||
06c645f064
|
|||
0c9fc4f017 | |||
a27e188c9e
|
|||
6915c35fed
|
|||
d3f2406b23
|
|||
0a54b66d2d
|
|||
ac24a28e44
|
|||
af707fbd82
|
|||
9962f51389
|
|||
8c2642d20b
|
|||
d2b77ff64f
|
|||
0db2f761dc
|
|||
ea75c49044
|
|||
5c66ae0bed
|
|||
4c0bb00de2
|
|||
5f233de4f8
|
|||
184e730e66
|
|||
60c360d54d
|
|||
83ca17f8c3
|
|||
da9286a477
|
|||
1369b76f42
|
|||
8566d61d02
|
|||
5702b333c6
|
37
.drone.yml
37
.drone.yml
@ -1,37 +0,0 @@
|
|||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: default
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: install
|
|
||||||
image: composer
|
|
||||||
commands:
|
|
||||||
- composer install
|
|
||||||
|
|
||||||
- name: phpstan
|
|
||||||
image: php:8.3
|
|
||||||
depends_on:
|
|
||||||
- install
|
|
||||||
commands:
|
|
||||||
- vendor/bin/phpstan analyse
|
|
||||||
|
|
||||||
- name: rector
|
|
||||||
image: php:8.3
|
|
||||||
depends_on:
|
|
||||||
- install
|
|
||||||
commands:
|
|
||||||
- vendor/bin/rector process --dry-run
|
|
||||||
|
|
||||||
- name: pest
|
|
||||||
image: php:8.3
|
|
||||||
depends_on:
|
|
||||||
- install
|
|
||||||
commands:
|
|
||||||
- vendor/bin/pest
|
|
||||||
|
|
||||||
- name: style check
|
|
||||||
image: php:8.3
|
|
||||||
depends_on:
|
|
||||||
- install
|
|
||||||
commands:
|
|
||||||
- vendor/bin/php-cs-fixer fix
|
|
69
.github/workflows/ci.yml
vendored
Normal file
69
.github/workflows/ci.yml
vendored
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
name: CI Pipeline
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
install:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: composer:latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout Code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: composer install
|
||||||
|
|
||||||
|
phpstan:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: install
|
||||||
|
container:
|
||||||
|
image: php:8.4
|
||||||
|
steps:
|
||||||
|
- name: Checkout Code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: PHPStan Analysis
|
||||||
|
run: vendor/bin/phpstan analyse
|
||||||
|
|
||||||
|
rector:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: install
|
||||||
|
container:
|
||||||
|
image: php:8.4
|
||||||
|
steps:
|
||||||
|
- name: Checkout Code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Rector Dry Run
|
||||||
|
run: vendor/bin/rector process --dry-run
|
||||||
|
|
||||||
|
pest:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: install
|
||||||
|
container:
|
||||||
|
image: php:8.4
|
||||||
|
steps:
|
||||||
|
- name: Checkout Code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Run Pest Tests
|
||||||
|
run: vendor/bin/pest
|
||||||
|
|
||||||
|
style-check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: install
|
||||||
|
container:
|
||||||
|
image: php:8.4
|
||||||
|
steps:
|
||||||
|
- name: Checkout Code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: PHP CS Fixer
|
||||||
|
run: vendor/bin/php-cs-fixer fix
|
@ -2,7 +2,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$finder = PhpCsFixer\Finder::create()
|
$finder = PhpCsFixer\Finder::create()
|
||||||
->in(__DIR__.'/src-php/');
|
->in(__DIR__.'/src/');
|
||||||
|
|
||||||
$config = new PhpCsFixer\Config();
|
$config = new PhpCsFixer\Config();
|
||||||
|
|
||||||
|
11
README.md
11
README.md
@ -5,12 +5,7 @@
|
|||||||
In this repository, you'll find my Advent of Code framework and solutions. If you want to
|
In this repository, you'll find my Advent of Code framework and solutions. If you want to
|
||||||
use this framework for your own solution, just remove all data in the `./data` folder.
|
use this framework for your own solution, just remove all data in the `./data` folder.
|
||||||
|
|
||||||
It provides both a PHP and a Go framework. The PHP framework is the most complete one while
|
## 🛠 Setup and running
|
||||||
the Go framework is still a work in progress and is meant to learn Go.
|
|
||||||
|
|
||||||
## PHP
|
|
||||||
|
|
||||||
### 🛠 Setup and running
|
|
||||||
- Run `composer install` to install the dependencies.
|
- Run `composer install` to install the dependencies.
|
||||||
- Run `./aoc day {day} {year?}` to run the solution for a specific day. If year is not given, use the current year (for example `./aoc day 1` to run the code for day 1 2023)
|
- Run `./aoc day {day} {year?}` to run the solution for a specific day. If year is not given, use the current year (for example `./aoc day 1` to run the code for day 1 2023)
|
||||||
- Run `./aoc test {day} {year?}` to run the solution against the specified results.
|
- Run `./aoc test {day} {year?}` to run the solution against the specified results.
|
||||||
@ -20,12 +15,12 @@ the Go framework is still a work in progress and is meant to learn Go.
|
|||||||
> - You can add `--one` or `--two` (or `-1` and `-2`) to the day to run the solution for part 1 or part 2.
|
> - You can add `--one` or `--two` (or `-1` and `-2`) to the day to run the solution for part 1 or part 2.
|
||||||
> - You can add `--skip-example` (or `-s`) to skip the example data.
|
> - You can add `--skip-example` (or `-s`) to skip the example data.
|
||||||
|
|
||||||
### 🧪 Testing and quality control
|
## 🧪 Testing and quality control
|
||||||
- Run `./vendor/bin/pest` to automatically validate the solutions.
|
- Run `./vendor/bin/pest` to automatically validate the solutions.
|
||||||
- Run `./vendor/bin/phpstan analyse` to run static analysis on the code.
|
- Run `./vendor/bin/phpstan analyse` to run static analysis on the code.
|
||||||
- Run `./vendor/bin/php-cs-fixer fix` to run (and fix) code style checks.
|
- Run `./vendor/bin/php-cs-fixer fix` to run (and fix) code style checks.
|
||||||
|
|
||||||
### 🧩 Add a new puzzle/solution
|
## 🧩 Add a new puzzle/solution
|
||||||
- Run `./aoc new {day?} {year?}` to create a new puzzle/solution.
|
- Run `./aoc new {day?} {year?}` to create a new puzzle/solution.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# https://taskfile.dev
|
# https://taskfile.dev
|
||||||
|
|
||||||
version: '3'
|
version: "3"
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
@ -11,21 +11,21 @@ tasks:
|
|||||||
check:
|
check:
|
||||||
cmds:
|
cmds:
|
||||||
- task: echo_title
|
- task: echo_title
|
||||||
vars: {TITLE: Running php-cs-fixer...}
|
vars: { TITLE: Running php-cs-fixer... }
|
||||||
- task: style
|
- task: style
|
||||||
- task: echo_title
|
- task: echo_title
|
||||||
vars: {TITLE: Running phpstan...}
|
vars: { TITLE: Running phpstan... }
|
||||||
- task: phpstan
|
- task: phpstan
|
||||||
- task: echo_title
|
- task: echo_title
|
||||||
vars: { TITLE: Running rector... }
|
vars: { TITLE: Running rector... }
|
||||||
- task: rector
|
- task: rector
|
||||||
- task: echo_title
|
- task: echo_title
|
||||||
vars: {TITLE: Running phpunit}
|
vars: { TITLE: Running pest }
|
||||||
- task: phpunit
|
- task: pest
|
||||||
|
|
||||||
style:
|
style:
|
||||||
cmds:
|
cmds:
|
||||||
- php vendor/bin/php-cs-fixer fix
|
- PHP_CS_FIXER_IGNORE_ENV=1 php vendor/bin/php-cs-fixer fix
|
||||||
silent: true
|
silent: true
|
||||||
|
|
||||||
phpstan:
|
phpstan:
|
||||||
@ -38,7 +38,7 @@ tasks:
|
|||||||
- php vendor/bin/rector process
|
- php vendor/bin/rector process
|
||||||
silent: true
|
silent: true
|
||||||
|
|
||||||
phpunit:
|
pest:
|
||||||
cmds:
|
cmds:
|
||||||
- php vendor/bin/pest
|
- php vendor/bin/pest
|
||||||
silent: true
|
silent: true
|
||||||
|
2
aoc
2
aoc
@ -11,7 +11,7 @@ use trizz\AdventOfCode\Commands\TestDay;
|
|||||||
|
|
||||||
(new CollisionProvider())->register();
|
(new CollisionProvider())->register();
|
||||||
|
|
||||||
require_once __DIR__.'/src-php/bootstrap.php';
|
require_once __DIR__.'/src/bootstrap.php';
|
||||||
|
|
||||||
$application = new Application('Advent of Code by trizz');
|
$application = new Application('Advent of Code by trizz');
|
||||||
|
|
||||||
|
21
aoc.go
21
aoc.go
@ -1,21 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
y22 "adventofcode/data/Y22"
|
|
||||||
"adventofcode/src-go/helpers"
|
|
||||||
"fmt"
|
|
||||||
"github.com/fatih/color"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
runY22()
|
|
||||||
day, year := helpers.GetDayYear()
|
|
||||||
|
|
||||||
fmt.Println(day, year)
|
|
||||||
}
|
|
||||||
|
|
||||||
func runY22() {
|
|
||||||
color.HiYellow("\n--- Advent of Code 2022 ---\n\n")
|
|
||||||
helpers.PrintDayResults(y22.Day1())
|
|
||||||
helpers.PrintDayResults(y22.Day2())
|
|
||||||
}
|
|
@ -13,24 +13,25 @@
|
|||||||
"php": "^8.3",
|
"php": "^8.3",
|
||||||
"ext-mbstring": "*",
|
"ext-mbstring": "*",
|
||||||
"cebe/markdown": "^1.2",
|
"cebe/markdown": "^1.2",
|
||||||
"laravel/prompts": "^0.1.13",
|
"laravel/prompts": "^0.3",
|
||||||
"nette/php-generator": "^4.1",
|
"nette/php-generator": "^4.1",
|
||||||
"phppkg/cli-markdown": "^2.0",
|
"phppkg/cli-markdown": "^2.0",
|
||||||
"symfony/console": "^6"
|
"symfony/console": "^7"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"friendsofphp/php-cs-fixer": "^3.3",
|
"friendsofphp/php-cs-fixer": "^3.3",
|
||||||
"jetbrains/phpstorm-attributes": "^1.0",
|
"jetbrains/phpstorm-attributes": "^1.0",
|
||||||
"nunomaduro/collision": "^8",
|
"nunomaduro/collision": "^8",
|
||||||
"pestphp/pest": "^2.26",
|
"pestphp/pest": "^3.5",
|
||||||
"phpstan/phpstan": "^1.2",
|
"phpstan/phpstan": "^2.0",
|
||||||
"rector/rector": "^0.19",
|
"rector/rector": "^2.0",
|
||||||
"robiningelbrecht/phpunit-pretty-print": "^1.2",
|
"robiningelbrecht/phpunit-pretty-print": "^1.2",
|
||||||
"symfony/var-dumper": "^7"
|
"symfony/var-dumper": "^7"
|
||||||
},
|
},
|
||||||
|
"minimum-stability": "dev",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"trizz\\AdventOfCode\\": "src-php/"
|
"trizz\\AdventOfCode\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
|
1928
composer.lock
generated
1928
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -21,11 +21,9 @@ final class Day2 extends Solution
|
|||||||
$horizontal = 0;
|
$horizontal = 0;
|
||||||
|
|
||||||
foreach ($data as $current) {
|
foreach ($data as $current) {
|
||||||
/**
|
|
||||||
* @var string $direction
|
|
||||||
* @var int $distance
|
|
||||||
*/
|
|
||||||
[$direction, $distance] = explode(' ', $current);
|
[$direction, $distance] = explode(' ', $current);
|
||||||
|
$direction = (string) $direction;
|
||||||
|
$distance = (int) $distance;
|
||||||
|
|
||||||
match ($direction) {
|
match ($direction) {
|
||||||
'forward' => $horizontal += $distance,
|
'forward' => $horizontal += $distance,
|
||||||
@ -46,11 +44,9 @@ final class Day2 extends Solution
|
|||||||
$horizontal = 0;
|
$horizontal = 0;
|
||||||
|
|
||||||
foreach ($data as $current) {
|
foreach ($data as $current) {
|
||||||
/**
|
|
||||||
* @var string $direction
|
|
||||||
* @var int $distance
|
|
||||||
*/
|
|
||||||
[$direction, $distance] = explode(' ', $current);
|
[$direction, $distance] = explode(' ', $current);
|
||||||
|
$direction = (string) $direction;
|
||||||
|
$distance = (int) $distance;
|
||||||
|
|
||||||
// Can't use 'match' here because of the multiple expressions for 'forward'.
|
// Can't use 'match' here because of the multiple expressions for 'forward'.
|
||||||
switch ($direction) {
|
switch ($direction) {
|
||||||
|
@ -52,6 +52,10 @@ final class Day7 extends Solution
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (empty($fuelPerPosition)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return (int) min($fuelPerPosition);
|
return (int) min($fuelPerPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ final class Day8 extends Solution
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[\Override]
|
#[\Override]
|
||||||
public function part2(array $data): int|string
|
public function part2(array $data): int
|
||||||
{
|
{
|
||||||
$sequences = [];
|
$sequences = [];
|
||||||
|
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
package Y22
|
|
||||||
|
|
||||||
import (
|
|
||||||
"adventofcode/src-go/helpers"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Day1() helpers.DayResults {
|
|
||||||
day1Example := helpers.ReadFileByLine("data/Y22/day1/example.txt")
|
|
||||||
day1 := helpers.ReadFileByLine("data/Y22/day1/data.txt")
|
|
||||||
|
|
||||||
calorieCount := calculateCalories(day1Example)
|
|
||||||
part1Example := calorieCount[len(calorieCount)-1]
|
|
||||||
part2Example := calorieCount[len(calorieCount)-1] + calorieCount[len(calorieCount)-2] + calorieCount[len(calorieCount)-3]
|
|
||||||
|
|
||||||
calorieCount = calculateCalories(day1)
|
|
||||||
part1Result := calorieCount[len(calorieCount)-1]
|
|
||||||
part2Result := calorieCount[len(calorieCount)-1] + calorieCount[len(calorieCount)-2] + calorieCount[len(calorieCount)-3]
|
|
||||||
|
|
||||||
results := helpers.DayResults{
|
|
||||||
Day: 1,
|
|
||||||
Year: 2022,
|
|
||||||
Part1Example: part1Example,
|
|
||||||
Part1: part1Result,
|
|
||||||
Part2Example: part2Example,
|
|
||||||
Part2: part2Result,
|
|
||||||
}
|
|
||||||
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
func calculateCalories(data []string) []int {
|
|
||||||
var calorieCount []int
|
|
||||||
total := 0
|
|
||||||
for _, line := range data {
|
|
||||||
if line == "" {
|
|
||||||
calorieCount = append(calorieCount, total)
|
|
||||||
total = 0
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val, err := strconv.Atoi(line)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
total += val
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(calorieCount, func(i, j int) bool {
|
|
||||||
return calorieCount[i] < calorieCount[j]
|
|
||||||
})
|
|
||||||
|
|
||||||
return calorieCount
|
|
||||||
}
|
|
@ -1,93 +0,0 @@
|
|||||||
package Y22
|
|
||||||
|
|
||||||
import (
|
|
||||||
"adventofcode/src-go/helpers"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
aRock string = "A"
|
|
||||||
aPaper string = "B"
|
|
||||||
aScissors string = "C"
|
|
||||||
|
|
||||||
bRock string = "X"
|
|
||||||
bPaper string = "Y"
|
|
||||||
bScissors string = "Z"
|
|
||||||
|
|
||||||
resultLose string = "X"
|
|
||||||
resultDraw string = "Y"
|
|
||||||
resultWin string = "Z"
|
|
||||||
)
|
|
||||||
|
|
||||||
var scoreTable = map[string]map[string]int{
|
|
||||||
aRock: {
|
|
||||||
bRock: 4,
|
|
||||||
bPaper: 8,
|
|
||||||
bScissors: 3,
|
|
||||||
},
|
|
||||||
aPaper: {
|
|
||||||
bRock: 1,
|
|
||||||
bPaper: 5,
|
|
||||||
bScissors: 9,
|
|
||||||
},
|
|
||||||
aScissors: {
|
|
||||||
bRock: 7,
|
|
||||||
bPaper: 2,
|
|
||||||
bScissors: 6,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var winOrLoseTable = map[string]map[string]string{
|
|
||||||
aRock: {
|
|
||||||
resultWin: bPaper,
|
|
||||||
resultDraw: bRock,
|
|
||||||
resultLose: bScissors,
|
|
||||||
},
|
|
||||||
aPaper: {
|
|
||||||
resultWin: bScissors,
|
|
||||||
resultDraw: bPaper,
|
|
||||||
resultLose: bRock,
|
|
||||||
},
|
|
||||||
aScissors: {
|
|
||||||
resultWin: bRock,
|
|
||||||
resultDraw: bScissors,
|
|
||||||
resultLose: bPaper,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func Day2() helpers.DayResults {
|
|
||||||
day2Example := helpers.ReadFileByLine("data/Y22/day2/example.txt")
|
|
||||||
day2 := helpers.ReadFileByLine("data/Y22/day2/data.txt")
|
|
||||||
|
|
||||||
results := helpers.DayResults{
|
|
||||||
Day: 2,
|
|
||||||
Year: 2022,
|
|
||||||
Part1Example: part1(day2Example),
|
|
||||||
Part1: part1(day2),
|
|
||||||
Part2Example: part2(day2Example),
|
|
||||||
Part2: part2(day2),
|
|
||||||
}
|
|
||||||
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
func part1(input []string) int {
|
|
||||||
score := 0
|
|
||||||
for _, line := range input {
|
|
||||||
data := strings.Split(line, " ")
|
|
||||||
score += scoreTable[data[0]][data[1]]
|
|
||||||
}
|
|
||||||
|
|
||||||
return score
|
|
||||||
}
|
|
||||||
|
|
||||||
func part2(input []string) int {
|
|
||||||
score := 0
|
|
||||||
for _, line := range input {
|
|
||||||
data := strings.Split(line, " ")
|
|
||||||
scoreValue := winOrLoseTable[data[0]][data[1]]
|
|
||||||
score += scoreTable[data[0]][scoreValue]
|
|
||||||
}
|
|
||||||
|
|
||||||
return score
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package Y22
|
|
||||||
|
|
||||||
import (
|
|
||||||
Y22D1 "adventofcode/data/Y22/day1"
|
|
||||||
Y22D2 "adventofcode/data/Y22/day2"
|
|
||||||
"adventofcode/src-go/helpers"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Day1() helpers.DayResults {
|
|
||||||
return Y22D1.Day1()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Day2() helpers.DayResults {
|
|
||||||
return Y22D2.Day2()
|
|
||||||
}
|
|
@ -69,7 +69,7 @@ final class Day2 extends Solution
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<int, array<int, string>>
|
* @return array{string, non-empty-list<string>}
|
||||||
*/
|
*/
|
||||||
private function getHand(string $line): array
|
private function getHand(string $line): array
|
||||||
{
|
{
|
||||||
|
@ -30,6 +30,17 @@ final class Day3 extends Solution
|
|||||||
$numbers = $this->checkLocation($row, $col);
|
$numbers = $this->checkLocation($row, $col);
|
||||||
$top = (int) $this->processNumbers($numbers['top'], sum: true);
|
$top = (int) $this->processNumbers($numbers['top'], sum: true);
|
||||||
$bottom = (int) $this->processNumbers($numbers['bottom'], sum: true);
|
$bottom = (int) $this->processNumbers($numbers['bottom'], sum: true);
|
||||||
|
if ($numbers === []) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($numbers['left'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($numbers['right'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$score += $top + $bottom + max($numbers['left']) + max($numbers['right']);
|
$score += $top + $bottom + max($numbers['left']) + max($numbers['right']);
|
||||||
}
|
}
|
||||||
@ -54,6 +65,18 @@ final class Day3 extends Solution
|
|||||||
|
|
||||||
$top = (array) $this->processNumbers($numbers['top']);
|
$top = (array) $this->processNumbers($numbers['top']);
|
||||||
$bottom = (array) $this->processNumbers($numbers['bottom']);
|
$bottom = (array) $this->processNumbers($numbers['bottom']);
|
||||||
|
if ($numbers === []) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($numbers['left'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($numbers['right'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$left = max($numbers['left']);
|
$left = max($numbers['left']);
|
||||||
$right = max($numbers['right']);
|
$right = max($numbers['right']);
|
||||||
|
|
||||||
@ -81,7 +104,7 @@ final class Day3 extends Solution
|
|||||||
if ($numbers[0] !== 0 && $numbers[1] === 0 && $numbers[2] !== 0) {
|
if ($numbers[0] !== 0 && $numbers[1] === 0 && $numbers[2] !== 0) {
|
||||||
$result[] = $numbers[0];
|
$result[] = $numbers[0];
|
||||||
$result[] = $numbers[2];
|
$result[] = $numbers[2];
|
||||||
} else {
|
} elseif ($numbers !== []) {
|
||||||
$result[] = max($numbers);
|
$result[] = max($numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +150,7 @@ final class Day3 extends Solution
|
|||||||
return $numbers;
|
return $numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getNumber(int $row, int $col, string $direction = null): ?string
|
private function getNumber(int $row, int $col, ?string $direction = null): ?string
|
||||||
{
|
{
|
||||||
$number = $this->matrix[$row][$col] ?? null;
|
$number = $this->matrix[$row][$col] ?? null;
|
||||||
|
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
namespace trizz\AdventOfCode\Y23;
|
namespace trizz\AdventOfCode\Y23;
|
||||||
|
|
||||||
final class Day5 extends \trizz\AdventOfCode\Solution
|
use trizz\AdventOfCode\Solution;
|
||||||
|
|
||||||
|
final class Day5 extends Solution
|
||||||
{
|
{
|
||||||
public static null|int|string $part1ExampleResult = 35;
|
public static null|int|string $part1ExampleResult = 35;
|
||||||
|
|
||||||
@ -47,6 +49,10 @@ final class Day5 extends \trizz\AdventOfCode\Solution
|
|||||||
$locations[$seed] = $location;
|
$locations[$seed] = $location;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($locations === []) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return min($locations);
|
return min($locations);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +103,7 @@ final class Day5 extends \trizz\AdventOfCode\Solution
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($line) && !str_contains($line, ':')) {
|
if (!empty($line) && !str_contains($line, ':')) {
|
||||||
[$destinationRange, $sourceRangeStart, $rangeLength] = array_values(array_map('intval', explode(' ', $line)));
|
[$destinationRange, $sourceRangeStart, $rangeLength] = array_map('intval', explode(' ', $line));
|
||||||
$this->maps[$currentMap][] = [
|
$this->maps[$currentMap][] = [
|
||||||
'destinationRange' => $destinationRange,
|
'destinationRange' => $destinationRange,
|
||||||
'sourceRangeStart' => $sourceRangeStart,
|
'sourceRangeStart' => $sourceRangeStart,
|
||||||
|
66
data/Y24/day1/Day1.php
Normal file
66
data/Y24/day1/Day1.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace trizz\AdventOfCode\Y24;
|
||||||
|
|
||||||
|
use trizz\AdventOfCode\Solution;
|
||||||
|
|
||||||
|
final class Day1 extends Solution
|
||||||
|
{
|
||||||
|
public static null|int|string $part1ExampleResult = 11;
|
||||||
|
|
||||||
|
public static null|int|string $part1Result = 1882714;
|
||||||
|
|
||||||
|
public static null|int|string $part2ExampleResult = 31;
|
||||||
|
|
||||||
|
public static null|int|string $part2Result = 19437052;
|
||||||
|
|
||||||
|
#[\Override]
|
||||||
|
public function part1(array $data): int
|
||||||
|
{
|
||||||
|
[$listLeft, $listRight] = $this->getList($data);
|
||||||
|
$score = 0;
|
||||||
|
foreach ($listLeft as $index => $left) {
|
||||||
|
$right = $listRight[$index];
|
||||||
|
$score += abs($left - $right);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $score;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[\Override]
|
||||||
|
public function part2(array $data): int
|
||||||
|
{
|
||||||
|
$score = 0;
|
||||||
|
[$listLeft, $listRight] = $this->getList($data);
|
||||||
|
|
||||||
|
foreach ($listLeft as $index => $left) {
|
||||||
|
$right = count(array_filter($listRight, fn ($x): bool => $x === $left));
|
||||||
|
if ($right > 0) {
|
||||||
|
$score += $left * $right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $score;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int,string> $data
|
||||||
|
*
|
||||||
|
* @return array<int,int[]>
|
||||||
|
*/
|
||||||
|
private function getList(array $data): array
|
||||||
|
{
|
||||||
|
$listLeft = [];
|
||||||
|
$listRight = [];
|
||||||
|
foreach ($data as $x) {
|
||||||
|
[$a1, $_, $_, $b1] = explode(' ', $x);
|
||||||
|
$listLeft[] = (int) $a1;
|
||||||
|
$listRight[] = (int) $b1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sort($listLeft);
|
||||||
|
sort($listRight);
|
||||||
|
|
||||||
|
return [$listLeft, $listRight];
|
||||||
|
}
|
||||||
|
}
|
1001
data/Y24/day1/data.txt
Normal file
1001
data/Y24/day1/data.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
data/Y24/day1/example.txt
Normal file
7
data/Y24/day1/example.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
||||||
|
|
11
go.mod
11
go.mod
@ -1,11 +0,0 @@
|
|||||||
module adventofcode
|
|
||||||
|
|
||||||
go 1.21.4
|
|
||||||
|
|
||||||
require github.com/fatih/color v1.17.0
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
|
||||||
golang.org/x/sys v0.18.0 // indirect
|
|
||||||
)
|
|
15
go.sum
15
go.sum
@ -1,15 +0,0 @@
|
|||||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
|
||||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
|
||||||
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
|
|
||||||
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
|
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
|
||||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
|
||||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
||||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
|
||||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
@ -39,8 +39,3 @@ parameters:
|
|||||||
message: "#^Property trizz\\\\AdventOfCode\\\\Y21\\\\Day8\\:\\:\\$patternDigits type has no value type specified in iterable type array\\.$#"
|
message: "#^Property trizz\\\\AdventOfCode\\\\Y21\\\\Day8\\:\\:\\$patternDigits type has no value type specified in iterable type array\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
path: data/Y21/day8/Day8.php
|
path: data/Y21/day8/Day8.php
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Method trizz\\\\AdventOfCode\\\\Y23\\\\Day2\\:\\:getHand\\(\\) should return array\\<int, array\\<int, string\\>\\> but returns array\\<int, array\\<int, string\\>\\|string\\>\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: data/Y23/day2/Day2.php
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
parameters:
|
parameters:
|
||||||
level: 8
|
level: 8
|
||||||
paths:
|
paths:
|
||||||
- src-php
|
- src
|
||||||
- data
|
- data
|
||||||
|
|
||||||
includes:
|
includes:
|
||||||
|
@ -9,7 +9,7 @@ use Rector\Set\ValueObject\SetList;
|
|||||||
return static function (RectorConfig $rectorConfig): void {
|
return static function (RectorConfig $rectorConfig): void {
|
||||||
$rectorConfig->paths(
|
$rectorConfig->paths(
|
||||||
[
|
[
|
||||||
__DIR__.'/src-php',
|
__DIR__.'/src',
|
||||||
__DIR__.'/data',
|
__DIR__.'/data',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
@ -17,7 +17,7 @@ return static function (RectorConfig $rectorConfig): void {
|
|||||||
// define sets of rules
|
// define sets of rules
|
||||||
$rectorConfig->sets(
|
$rectorConfig->sets(
|
||||||
[
|
[
|
||||||
LevelSetList::UP_TO_PHP_83,
|
LevelSetList::UP_TO_PHP_84,
|
||||||
SetList::CODE_QUALITY,
|
SetList::CODE_QUALITY,
|
||||||
SetList::CODING_STYLE,
|
SetList::CODING_STYLE,
|
||||||
SetList::TYPE_DECLARATION,
|
SetList::TYPE_DECLARATION,
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
package helpers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DayResults struct {
|
|
||||||
Day int `json:"day"`
|
|
||||||
Year int `json:"year"`
|
|
||||||
Part1Example int `json:"part1-example"`
|
|
||||||
Part1 int `json:"part1"`
|
|
||||||
Part2Example int `json:"part2-example"`
|
|
||||||
Part2 int `json:"part2"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintDayResults(results DayResults) {
|
|
||||||
yellow := color.New(color.FgYellow).SprintFunc()
|
|
||||||
gray := color.New(color.FgHiBlack).SprintFunc()
|
|
||||||
|
|
||||||
color.HiBlue("Day %d%s", results.Day, gray("/"+strconv.Itoa(results.Year)))
|
|
||||||
color.Green(" Part 1 (example): %s", yellow(results.Part1Example))
|
|
||||||
color.HiGreen(" Part 1 : %s", yellow(results.Part1))
|
|
||||||
color.Blue(" " + strings.Repeat("-", 23))
|
|
||||||
color.Green(" Part 2 (example): %s", yellow(results.Part2Example))
|
|
||||||
color.HiGreen(" Part 2 : %s", yellow(results.Part2))
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
package helpers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ReadFileByLine(path string) []string {
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer func(file *os.File) {
|
|
||||||
err := file.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}(file)
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
var returnData []string
|
|
||||||
for scanner.Scan() {
|
|
||||||
returnData = append(returnData, scanner.Text())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return returnData
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDayYear() (int, int) {
|
|
||||||
// get current year
|
|
||||||
year, _, day := time.Now().Date()
|
|
||||||
// Change 2023 to 23
|
|
||||||
year = year % 100
|
|
||||||
|
|
||||||
if len(os.Args) == 2 {
|
|
||||||
day, _ := strconv.Atoi(os.Args[1])
|
|
||||||
return day, year
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(os.Args) == 3 {
|
|
||||||
day, _ := strconv.Atoi(os.Args[1])
|
|
||||||
year, _ := strconv.Atoi(os.Args[2])
|
|
||||||
|
|
||||||
return day, year
|
|
||||||
}
|
|
||||||
|
|
||||||
return day, year
|
|
||||||
}
|
|
@ -78,7 +78,7 @@ abstract class AbstractDayCommand extends Command
|
|||||||
protected function loadClass(): Solution
|
protected function loadClass(): Solution
|
||||||
{
|
{
|
||||||
require_once sprintf('%s/Y%d/day%d/Day%d.php', DATA_DIR, $this->year, $this->day, $this->day);
|
require_once sprintf('%s/Y%d/day%d/Day%d.php', DATA_DIR, $this->year, $this->day, $this->day);
|
||||||
$className = sprintf('%s\\Y%d\\Day%d', substr(__NAMESPACE__, 0, -9), $this->year, $this->day);
|
$className = sprintf('%s\Y%d\Day%d', substr(__NAMESPACE__, 0, -9), $this->year, $this->day);
|
||||||
|
|
||||||
/** @var Solution $class */
|
/** @var Solution $class */
|
||||||
$class = new $className();
|
$class = new $className();
|
3
tests/AoC_2024Test.php
Normal file
3
tests/AoC_2024Test.php
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
testYear(24);
|
Reference in New Issue
Block a user