Solution for day 1.
This commit is contained in:
parent
44832a7aea
commit
d8f64cc14b
@ -1,3 +1,63 @@
|
||||
<?php
|
||||
|
||||
// enter the solution for day 1 here.
|
||||
// Problem: https://adventofcode.com/2015/day/1
|
||||
|
||||
/*
|
||||
* Solution 1
|
||||
*
|
||||
* Runtime: ~0.158ms
|
||||
*/
|
||||
function solution1() {
|
||||
$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";
|
||||
}
|
||||
solution1();
|
||||
|
||||
/*
|
||||
* Solution 2
|
||||
*
|
||||
* Runtime: ~0.622ms
|
||||
*/
|
||||
function solution2() {
|
||||
$start_time = microtime(true);
|
||||
$instructions = file_get_contents('./inputs/day1.txt');
|
||||
$chars = str_split($instructions);
|
||||
$floor = 0;
|
||||
foreach ($chars as $char) {
|
||||
match ($char) {
|
||||
"(" => $floor++,
|
||||
")" => $floor--,
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
echo "The answer is $floor\n";
|
||||
$end_time = microtime(true);
|
||||
$execution_time = ($end_time - $start_time);
|
||||
echo $execution_time * 1000 . "\n";
|
||||
}
|
||||
solution2();
|
||||
|
||||
/*
|
||||
* Solution 3
|
||||
*
|
||||
* Runtime: ~0
|
||||
*/
|
||||
function solution3() {
|
||||
$start_time = microtime(true);
|
||||
$instructions = file_get_contents('./inputs/day1.txt');
|
||||
$chars = str_split($instructions);
|
||||
$values = array_count_values($chars);
|
||||
$floor = $values['('] - $values[')'];
|
||||
echo "The answer is $floor\n";
|
||||
$end_time = microtime(true);
|
||||
$execution_time = ($end_time - $start_time);
|
||||
echo $execution_time * 1000 . "\n";
|
||||
}
|
||||
solution3();
|
||||
|
||||
1
2015/inputs/day1.txt
Normal file
1
2015/inputs/day1.txt
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user