Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // get the base frequency
- $startingFrequency = 0;
- // get the contents given by the advent_of_code website and put it in the folder "external_contents" in the txt file called "day_1.txt"
- $fContents = file_get_contents("external_contents/day_1.txt");
- $reached = [$startingFrequency];
- // Loop through the external contents until we find a match that happens twice
- for ($i = 0; $i < 10000; $i++) {
- // loop through each line of the contents (delimit by new line)
- foreach ((array)explode(PHP_EOL, $fContents) as $number) {
- // make sure the number is numeric
- if (is_numeric($number)) {
- // add the number to the starting frequency
- $startingFrequency += $number;
- // check if the starting frequency is in the array
- if (in_array($startingFrequency, $reached)) {
- echo "Duplicate frequency: $startingFrequency<br>";
- } else {
- // push the frequency into the '$reached' array if it has not yet been inserted
- array_push($reached, $startingFrequency);
- }
- }
- }
- }
- // return the end result
- echo "End result: " . $startingFrequency;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement