Skip to content

Commit 68fa01b

Browse files
authored
Year 2021 Day 02 solutions (#8)
- Solve both parts of day 2.
1 parent e7da61c commit 68fa01b

File tree

6 files changed

+1225
-0
lines changed

6 files changed

+1225
-0
lines changed

2021/day-02.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use adventofcode\Year2021\Navigation;
6+
7+
/**
8+
* --- Day 2: Dive! ---
9+
* Now, you need to figure out how to pilot this thing.
10+
*
11+
* It seems like the submarine can take a series of commands like forward 1, down 2, or up 3:
12+
*
13+
* forward X increases the horizontal position by X units.
14+
* down X increases the depth by X units.
15+
* up X decreases the depth by X units.
16+
*
17+
* Note that since you're on a submarine, down and up affect your depth, and
18+
* so they have the opposite result of what you might expect.
19+
*
20+
* The submarine seems to already have a planned course (your puzzle input).
21+
* You should probably figure out where it's going. For example:
22+
*
23+
* forward 5
24+
* down 5
25+
* forward 8
26+
* up 3
27+
* down 8
28+
* forward 2
29+
*
30+
* Your horizontal position and depth both start at 0. The steps above would then modify them as follows:
31+
*
32+
* forward 5 adds 5 to your horizontal position, a total of 5.
33+
* down 5 adds 5 to your depth, resulting in a value of 5.
34+
* forward 8 adds 8 to your horizontal position, a total of 13.
35+
* up 3 decreases your depth by 3, resulting in a value of 2.
36+
* down 8 adds 8 to your depth, resulting in a value of 10.
37+
* forward 2 adds 2 to your horizontal position, a total of 15.
38+
*
39+
* After following these instructions, you would have a horizontal position of
40+
* 15 and a depth of 10. (Multiplying these together produces 150.)
41+
*
42+
* Calculate the horizontal position and depth you would have after following
43+
* the planned course. What do you get if you multiply your final horizontal
44+
* position by your final depth?
45+
*/
46+
47+
$navigation = new Navigation();
48+
49+
$directions = file(__DIR__ . '/inputs/day-02.input', FILE_IGNORE_NEW_LINES);
50+
$position = $navigation->findPosition($directions);
51+
$product = $position['horizontal'] * $position['depth'];
52+
print(
53+
'Your position is ' . $position['horizontal'] . ' horizontal and '
54+
. $position['depth'] . ' depth for a product of ' . $product . ".\n"
55+
);
56+
57+
/**
58+
* --- Part Two ---
59+
* Based on your calculations, the planned course doesn't seem to make any
60+
* sense. You find the submarine manual and discover that the process is actually slightly more complicated.
61+
*
62+
* In addition to horizontal position and depth, you'll also need to track a
63+
* third value, aim, which also starts at 0. The commands also mean something
64+
* entirely different than you first thought:
65+
*
66+
* - down X increases your aim by X units.
67+
* - up X decreases your aim by X units.
68+
* - forward X does two things:
69+
* - It increases your horizontal position by X units.
70+
* - It increases your depth by your aim multiplied by X.
71+
*
72+
* Again note that since you're on a submarine, down and up do the opposite of
73+
* what you might expect: "down" means aiming in the positive direction.
74+
*
75+
* Now, the above example does something different:
76+
*
77+
* - forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
78+
* - down 5 adds 5 to your aim, resulting in a value of 5.
79+
* - forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
80+
* - up 3 decreases your aim by 3, resulting in a value of 2.
81+
* - down 8 adds 8 to your aim, resulting in a value of 10.
82+
* - forward 2 adds 2 to your horizontal position, a total of 15. Because
83+
* your aim is 10, your depth increases by 2*10=20 to a total of 60.
84+
*
85+
* After following these new instructions, you would have a horizontal
86+
* position of 15 and a depth of 60. (Multiplying these produces 900.)
87+
*
88+
* Using this new interpretation of the commands, calculate the horizontal
89+
* position and depth you would have after following the planned course. What
90+
* do you get if you multiply your final horizontal position by your final depth?
91+
*/
92+
93+
$position = $navigation->findPositionByAim($directions);
94+
$product = $position['horizontal'] * $position['depth'];
95+
print(
96+
'Your position via aim approach is ' . $position['horizontal'] . ' horizontal and '
97+
. $position['depth'] . ' depth for a product of ' . $product . ".\n"
98+
);

2021/inputs/day-02-sample.input

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
forward 5
2+
down 5
3+
forward 8
4+
up 3
5+
down 8
6+
forward 2

0 commit comments

Comments
 (0)