First commit, Complete 2015 day 1

This commit is contained in:
Timothy Warren 2022-12-15 12:03:31 -05:00
commit ced75d1f4f
4 changed files with 66 additions and 0 deletions

36
2015/day1/README.md Normal file
View File

@ -0,0 +1,36 @@
# Day 1: Not Quite Lisp
## Part 1
Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th.
Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
Here's an easy puzzle to warm you up.
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time.
An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.
The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.
For example:
- `(())` and `()()` both result in floor 0.
- `(((` and `(()(()(` both result in floor 3.
- `))(((((` also results in floor 3.
- `())` and `))(` both result in floor -1 (the first basement level).
- `)))` and `)())())` both result in floor -3.
**To what floor do the instructions take Santa?**
## Part 2
Now, given the same instructions, find the **position** of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on.
For example:
* `)` causes him to enter the basement at character position 1.
* `()())` causes him to enter the basement at character position 5.
What is the position of the character that causes Santa to first enter the basement?

1
2015/day1/input.txt Normal file

File diff suppressed because one or more lines are too long

28
2015/day1/run.php Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env php
<?php declare(strict_types=1);
$inputStr = file_get_contents('input.txt');
$input = str_split($inputStr);
$actions = array_map(fn (string $item): int => $item === '(' ? 1 : -1, $input);
echo "Part 1: final floor:";
echo array_reduce($actions, fn (int $a, int $b): int => $a + $b, 0) . "\n";
function find_sum_index(array $actions, int $target = -1): int
{
$current = 0;
foreach ($actions as $idx => $val)
{
$current += $val;
if ($current === $target)
{
return $idx + 1;
}
}
}
echo "Part 2: first step in basement:";
echo find_sum_index($actions, -1) . "\n";

1
README.md Normal file
View File

@ -0,0 +1 @@
# Advent of Code