Browse Source

Day 9 Done

master
Graeme Dyas 4 months ago
parent
commit
de64b0bc4f
  1. 11
      Day 9/Part_1.php
  2. 67
      Day 9/Part_2.php
  3. 2
      README.md

11
Day 9/Part_1.php

@ -2,6 +2,8 @@
$in = file("input.txt");
$ans = 0;
foreach ($in as &$inv) {
$inv = (int)trim($inv);
}
@ -33,8 +35,11 @@ while (true) {
$v = get_preamble($in,25);
$v = get_sums($v);
if (!in_array($in[25],$v)) {
print $in[25];
die();
$ans = $in[25];
break;
}
array_shift($in);
}
}
print $ans;

67
Day 9/Part_2.php

@ -0,0 +1,67 @@
<?php
$in = file("input.txt");
$in2 = $in;
$ans = 0;
$pre = 25;
foreach ($in as &$inv) {
$inv = (int)trim($inv);
}
$in2 = $in;
function get_preamble($data, $size)
{
$p = array_slice($data, 0, $size);
return $p;
}
function get_sums($data)
{
$out = array();
foreach ($data as $pk => $pv) {
foreach ($data as $pk2 => $pv2) {
if ($pk !== $pk2) {
array_push($out, $pv + $pv2);
}
}
}
$out = array_unique($out);
sort($out);
return $out;
}
while (true) {
$v = get_preamble($in, $pre);
$v = get_sums($v);
if (!in_array($in[$pre], $v)) {
$ans = $in[$pre];
break;
}
array_shift($in);
}
while (true) {
$in3 = $in2;
while (count($in3) > 0) {
$acc = array_sum($in3);
if ($acc == $ans) {
sort($in3);
$b = array_shift($in3) + array_pop($in3);
echo $b;
die();
}
array_pop($in3);
}
array_shift($in2);
if (count($in2) == 0) {
break;
}
}

2
README.md

@ -9,4 +9,4 @@
|6|:heavy_check_mark:|:heavy_check_mark:|
|7|:heavy_check_mark:|:x:|
|8|:heavy_check_mark:|:heavy_check_mark:|
|9|:heavy_check_mark:|:x:|
|9|:heavy_check_mark:|:heavy_check_mark:|
Loading…
Cancel
Save