Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. <?php
  2.  
  3. // get the base frequency
  4. $startingFrequency = 0;
  5.  
  6. // 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"
  7. $fContents = file_get_contents("external_contents/day_1.txt");
  8.  
  9. $reached = [$startingFrequency];
  10.  
  11. // Loop through the external contents until we find a match that happens twice
  12. for ($i = 0; $i < 10000; $i++) {
  13.     // loop through each line of the contents (delimit by new line)
  14.     foreach ((array)explode(PHP_EOL, $fContents) as $number) {
  15.         // make sure the number is numeric
  16.         if (is_numeric($number)) {
  17.             // add the number to the starting frequency
  18.             $startingFrequency += $number;
  19.             // check if the starting frequency is in the array
  20.             if (in_array($startingFrequency, $reached)) {
  21.                 echo "Duplicate frequency: $startingFrequency<br>";
  22.             } else {
  23.                 // push the frequency into the '$reached' array if it has not yet been inserted
  24.                 array_push($reached, $startingFrequency);
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30.  
  31. // return the end result
  32. echo "End result: " . $startingFrequency;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement