Skip to content

Instantly share code, notes, and snippets.

@aishraj
Last active December 4, 2018 15:31
Show Gist options
  • Save aishraj/45e174542dd3fcc6bfb4689119f0696b to your computer and use it in GitHub Desktop.
Save aishraj/45e174542dd3fcc6bfb4689119f0696b to your computer and use it in GitHub Desktop.
<?hh // strict
namespace adventofcode\two;
use namespace HH\Lib\C;
<<__Entrypoint>>
async function main(): Awaitable<noreturn> {
$file_contents = await read_file("input.txt");
$parsed_items = \explode("\n", $file_contents);
$s = 0;
$results = array();
foreach ($parsed_items as $number_val) {
$item = (int)$number_val;
$s += $item;
if (C\contains_key($results, $s)) {
\var_dump($s);
break;
} else {
$results[$s] = 1;
}
}
exit(0);
}
async function read_file(string $file_name): Awaitable<string> {
$file_handle = \fopen($file_name, "r");
$result = "";
if ($file_handle) {
while (($line = \fgets($file_handle)) !== false) {
$result .= $line;
}
\fclose($file_handle);
} else {
throw new \Exception("Unable to open the file for readidng");
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment