50 lines
1.1 KiB
PHP
Raw Normal View History

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 22:52:14 -05:00
$start_time = microtime(true);
$instructions = file_get_contents('./inputs/day1.txt');
$up_floors = substr_count($instructions, "(");
$down_floors = substr_count($instructions, ")");
$total = $up_floors - $down_floors;
echo "The answer is $total\n";
$end_time = microtime(true);
$execution_time = ($end_time - $start_time);
echo $execution_time * 1000 . "\n";
}
/*
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() {
2024-11-23 22:52:14 -05:00
$start_time = microtime(true);
2024-11-23 23:22:26 -05:00
$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++;
}
};
echo "The position is $position\n";
2024-11-23 22:52:14 -05:00
$end_time = microtime(true);
$execution_time = ($end_time - $start_time);
echo $execution_time * 1000 . "\n";
}