2024-11-23 22:16:04 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-11-23 22:52:14 -05:00
|
|
|
// Problem: https://adventofcode.com/2015/day/1
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Solution 1
|
|
|
|
|
*
|
|
|
|
|
* Runtime: ~0.158ms
|
|
|
|
|
*/
|
2024-11-23 23:22:26 -05:00
|
|
|
function day1solution1() {
|
2024-11-23 23:28:18 -05:00
|
|
|
$instructions = file_get_contents('./2015/inputs/day1.txt');
|
2024-11-23 22:52:14 -05:00
|
|
|
$up_floors = substr_count($instructions, "(");
|
|
|
|
|
$down_floors = substr_count($instructions, ")");
|
|
|
|
|
$total = $up_floors - $down_floors;
|
2024-11-24 10:03:05 -05:00
|
|
|
|
|
|
|
|
return $total;
|
2024-11-23 22:52:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2024-11-23 23:22:26 -05:00
|
|
|
* Solution 1b
|
2024-11-23 22:52:14 -05:00
|
|
|
*
|
2024-11-23 23:22:26 -05:00
|
|
|
* Runtime: ~.33ms
|
2024-11-23 22:52:14 -05:00
|
|
|
*/
|
2024-11-23 23:22:26 -05:00
|
|
|
function day1solution1b() {
|
|
|
|
|
$instructions = file_get_contents('./2015/inputs/day1.txt');
|
2024-11-23 22:52:14 -05:00
|
|
|
$chars = str_split($instructions);
|
2024-11-23 23:22:26 -05:00
|
|
|
$position = 1;
|
2024-11-23 22:52:14 -05:00
|
|
|
$floor = 0;
|
|
|
|
|
foreach ($chars as $char) {
|
|
|
|
|
match ($char) {
|
|
|
|
|
"(" => $floor++,
|
|
|
|
|
")" => $floor--,
|
|
|
|
|
};
|
2024-11-23 23:22:26 -05:00
|
|
|
if ($floor == -1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$position++;
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-11-24 10:03:05 -05:00
|
|
|
|
|
|
|
|
return $position;
|
2024-11-23 22:52:14 -05:00
|
|
|
}
|