Compare commits

..

2 Commits

  1. 91
      public/days/Day2.php
  2. 15
      public/index.php
  3. 11
      public/main.js
  4. 2
      public/run.php
  5. 5
      public/styles.css

@ -0,0 +1,91 @@
<?php
final class Day2 extends Day {
function part1(): string {
$safeCount = 0;
foreach ($this->input as $line){
$levels = array_map('intval', explode(' ', $line));
$isSafe = true;
$direction = null;
for ($i = 0; $i < count($levels) - 1; $i++){
$levelNow = $levels[$i];
$levelThen = $levels[$i + 1];
$change = abs($levelThen - $levelNow);
if ($change < 1 || $change > 3){
$isSafe = false;
break;
}
if ($direction === null){
$direction = $levelThen > $levelNow;
}
if ($direction !== ($levelThen > $levelNow)){
$isSafe = false;
break;
}
}
if ($isSafe)
$safeCount++;
}
return strval($safeCount);
}
function part2(): string {
$safeCount = 0;
foreach ($this->input as $line){
$levels = array_map('intval', explode(' ', $line));
$isSafe = false;
for ($removeLevelIndex = -1; $removeLevelIndex < count($levels); $removeLevelIndex++){
$localLevels = $levels;
if ($removeLevelIndex > -1){
array_splice($localLevels, $removeLevelIndex, 1);
}
$levelSetIsSafe = true;
$direction = null;
for ($i = 0; $i < count($localLevels) - 1; $i++){
$levelNow = $localLevels[$i];
$levelThen = $localLevels[$i + 1];
$change = abs($levelThen - $levelNow);
if ($change < 1 || $change > 3){
$levelSetIsSafe = false;
break;
}
if ($direction === null){
$direction = $levelThen > $levelNow;
}
if ($direction !== ($levelThen > $levelNow)){
$levelSetIsSafe = false;
break;
}
}
if ($levelSetIsSafe){
$isSafe = true;
break;
}
}
if ($isSafe)
$safeCount++;
}
return strval($safeCount);
}
}

@ -5,9 +5,22 @@
</head> </head>
<body> <body>
<?php
<div id="response">Response</div>
<?php
for ($i = 1; $i <= 25; $i++){
echo
"<div class='day' id='day-$i'>
<h3>Day $i</h3>
<button data-url='/run.php?day=$i&part=1'>Part 1</button>
<button data-url='/run.php?day=$i&part=2'>Part 2</button>
</div>";
}
?> ?>
<script type="application/javascript" src="main.js"></script>
</body> </body>
</html> </html>

@ -0,0 +1,11 @@
for (let i = 1; i <= 25; i++){
let buttons = document.querySelectorAll(`#day-${i} > button`);
buttons.forEach(button => {
button.addEventListener('click', async () => {
const response = await (await fetch(button.dataset['url'])).json();
document.getElementById('response').innerText = response.result;
});
});
}

@ -30,9 +30,9 @@ if ($dayNumber){
'data' => null 'data' => null
]; ];
echo json_encode($response); echo json_encode($response);
die(); die();
} }
} }
} }
echo json_encode([]);

@ -4,7 +4,10 @@ body {
background-color: rgb(30, 30, 30); background-color: rgb(30, 30, 30);
} }
* { body > * {
color: white; color: white;
}
* {
font-family: "Red Hat Mono", serif; font-family: "Red Hat Mono", serif;
} }
Loading…
Cancel
Save