Browse Source

First Commit

master
Graeme Dyas 4 months ago
commit
f4bd27ebc9
  1. 22
      Day 1/Part_1.php
  2. 24
      Day 1/Part_2.php
  3. 32
      Day 2/Part_1.php
  4. 35
      Day 2/Part_2.php
  5. 27
      Day 3/Part_1.php
  6. 26
      Day 3/Part_2.php

22
Day 1/Part_1.php

@ -0,0 +1,22 @@
<?php
//Read in the set of numbers
$in = file("input.txt");
//Clean up the numbers to remove any spaces
foreach ($in as &$inx) {
$inx = trim($inx);
}
//Loop over the numbers till you find the pair then stop
foreach ($in as $a) {
foreach ($in as $b) {
if ($a + $b == 2020) {
echo $a * $b . "\n";
die();
}
}
}

24
Day 1/Part_2.php

@ -0,0 +1,24 @@
<?php
//Read in the set of numbers
$in = file("input.txt");
//Clean up the numbers to remove any spaces
foreach ($in as &$inx) {
$inx = trim($inx);
}
//Loop over the numbers till you find the pair then stop
foreach ($in as $a) {
foreach ($in as $b) {
foreach($in as $c) {
if ($a + $b + $c == 2020) {
echo $a * $b *$c . "\n";
die();
}
}
}
}

32
Day 2/Part_1.php

@ -0,0 +1,32 @@
<?php
$in = file("input.txt");
$data = array();
$count = 0;
foreach ($in as &$inx) {
$inx = trim($inx);
$inx = explode(" ", $inx);
$minmax = explode("-", $inx[0]);
$out = array("min" => (int)$minmax[0], "max" => (int)$minmax[1], "chr" => $inx[1][0], "password" => $inx[2]);
array_push($data, $out);
}
foreach ($data as $a) {
$hits = 0;
$chars = str_split($a['password']);
foreach ($chars as $c) {
if ($c == $a['chr']) {
$hits = $hits + 1;
}
}
if ($hits >= $a['min'] && $hits <= $a['max']) {
$count = $count + 1;
}
}
echo $count . "\n";

35
Day 2/Part_2.php

@ -0,0 +1,35 @@
<?php
$in = file("input.txt");
$data = array();
$count = 0;
foreach ($in as &$inx) {
$inx = trim($inx);
$inx = explode(" ", $inx);
$ab = explode("-", $inx[0]);
$out = array("a" => (int)$ab[0], "b" => (int)$ab[1], "chr" => $inx[1][0], "password" => $inx[2]);
array_push($data, $out);
}
foreach ($data as $a) {
$hits = 0;
$indexa = $a['a'] - 1;
$indexb = $a['b'] - 1;
if ($a['password'][$indexa] == $a['chr']) {
$hits = $hits + 1;
}
if ($a['password'][$indexb] == $a['chr']) {
$hits = $hits + 1;
}
if ($hits == 1) {
$count = $count + 1;
}
}
echo $count . "\n";

27
Day 3/Part_1.php

@ -0,0 +1,27 @@
<?php
$in = file("input.txt");
foreach ($in as &$inx) {
$inx = trim($inx);
$inx = str_repeat($inx, 1000);
}
function countl($row,$col,$in) {
$x = 0;
$c = 1;
$hit = 0;
foreach ($in as $line) {
if ($c % $col or $col == 1) {
if ($line[$x] == "#") {
$hit = $hit + 1;
}
$x = $x + $row;
}
$c = $c + 1;
}
return($hit);
}
echo countl(3,1,$in);

26
Day 3/Part_2.php

@ -0,0 +1,26 @@
<?php
$in = file("input.txt");
foreach ($in as &$inx) {
$inx = trim($inx);
$inx = str_repeat($inx, 1000);
}
function countl($row,$col,$in) {
$x = 0;
$c = 1;
$hit = 0;
foreach ($in as $line) {
if ($c % $col or $col == 1) {
if ($line[$x] == "#") {
$hit = $hit + 1;
}
$x = $x + $row;
}
$c = $c + 1;
}
return($hit);
}
$out = countl(1,1,$in) * countl(3,1,$in) * countl(5,1,$in) * countl(7,1,$in) * countl(1,2,$in);
print $out;
Loading…
Cancel
Save