From 8b73ada230812c33e541ec8606311e59745f4ac5 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Wed, 10 Dec 2025 15:01:52 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Finit=20le=20jour=202=20et=20commen?= =?UTF-8?q?ce=20le=201=20et=20le=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ add.sh | 19 +++++++++++++++++++ day_1/part_1.php | 35 +++++++++++++++++++++++++++++++++++ day_2/part_1.php | 28 ++++++++++++++++++++++++++++ day_2/part_2.php | 37 +++++++++++++++++++++++++++++++++++++ day_3/part_1.php | 18 ++++++++++++++++++ 6 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100755 add.sh create mode 100644 day_1/part_1.php create mode 100644 day_2/part_1.php create mode 100644 day_2/part_2.php create mode 100644 day_3/part_1.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5a90de --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +input.txt +/.idea \ No newline at end of file diff --git a/add.sh b/add.sh new file mode 100755 index 0000000..df5c3ec --- /dev/null +++ b/add.sh @@ -0,0 +1,19 @@ +#!/usr/bin/zsh + +SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )" +NUMBER=$1 + +if [ ! -d "${SCRIPT_PATH}/day_${NUMBER}" ]; then + mkdir "${SCRIPT_PATH}/day_${NUMBER}" +else + echo "folder ${SCRIPT_PATH}/day_${NUMBER} already exists" + exit +fi + +cd "${SCRIPT_PATH}/day_${NUMBER}" || exit + +touch input.txt part_1.php part_2.php + +echo "DONE" + +exit diff --git a/day_1/part_1.php b/day_1/part_1.php new file mode 100644 index 0000000..5ae246d --- /dev/null +++ b/day_1/part_1.php @@ -0,0 +1,35 @@ +!empty($line)); + +$count = 0; +$position = 50; + +echo 'The dial starts by pointing at 50'."\n"; + +function checkPosition(&$position) +{ + if ($position < 0) { + $position = 100 - abs($position); + } elseif ($position > 99) { + $position = $position - 100; + } +} + +foreach ($lines as $line) { + $direction = substr($line, 0, 1); + $number = (int)substr($line, 1) % 100; + if ($direction === 'L') { + $position -= $number; + checkPosition($position); + } elseif ($direction === 'R') { + $position += $number; + checkPosition($position); + } + + if ($position === 0) { + $count++; + } + + echo sprintf('The dial is rotated %s to point at %u | count : %u%s', $line,$position, $count, "\n"); +} \ No newline at end of file diff --git a/day_2/part_1.php b/day_2/part_1.php new file mode 100644 index 0000000..e0db408 --- /dev/null +++ b/day_2/part_1.php @@ -0,0 +1,28 @@ + explode('-', $interval), explode(',', file_get_contents('./input.txt'))); + +$invalids = []; + +foreach ($intervals as $interval) { + [$start, $end] = $interval; + $current = $start; + while ($current <= $end) { + if (is_invalid($current)) { + $invalids[] = $current; + } + + $current++; + } +} + +function is_invalid($current): bool { + if (strlen($current) % 2 !== 0) { + return false; + } + + $middle = strlen($current) / 2; + return substr($current, 0, $middle) === substr($current, $middle); +} + +echo array_sum($invalids); \ No newline at end of file diff --git a/day_2/part_2.php b/day_2/part_2.php new file mode 100644 index 0000000..2d7bc47 --- /dev/null +++ b/day_2/part_2.php @@ -0,0 +1,37 @@ + explode('-', $interval), explode(',', file_get_contents('./input.txt'))); + +$invalids = []; + +foreach ($intervals as $interval) { + [$start, $end] = $interval; + $current = $start; + while ($current <= $end) { + if (is_invalid($current)) { + $invalids[] = $current; + } + + $current++; + } +} + +function is_invalid(string $current): bool { + for ($i = 1, $l = strlen($current); $i < $l; $i++) { + $parts = str_split($current, $i); + $is_equals = true; + for ($j = 0, $m = count($parts) - 1; $j < $m; $j++) { + if ($parts[$j] !== $parts[$j + 1]) { + $is_equals = false; + continue; + } + } + if ($is_equals) { + return true; + } + } + + return false; +} + +echo array_sum($invalids); \ No newline at end of file diff --git a/day_3/part_1.php b/day_3/part_1.php new file mode 100644 index 0000000..7a8369b --- /dev/null +++ b/day_3/part_1.php @@ -0,0 +1,18 @@ +