Browse Source

Added 7/s

master
Graeme Dyas 8 months ago
parent
commit
67621b500b
  1. 56
      Day 7/Part_1.php
  2. 64
      Day 7/Part_2.php
  3. 37
      Day 8/Part_1.php
  4. 78
      Day 8/Part_2.php
  5. 11
      README.md

56
Day 7/Part_1.php

@ -0,0 +1,56 @@
<?php
# 55.25.68
$in = file("input.txt");
function formatbags($in)
{
$bags = array();
foreach ($in as &$rule) {
$rule = explode(" ", str_replace(".", "", str_replace(",", "", trim($rule))));
$bag = array_shift($rule) . " " . array_shift($rule);
array_shift($rule);
array_shift($rule);
$bags[$bag] = array();
while (count($rule) > 0) {
if ($rule[0] == "no") {
break;
} else {
$num = array_shift($rule);
$type = array_shift($rule) . " " . array_shift($rule);
array_shift($rule);
$bags[$bag][$type] = $num;
}
}
}
return $bags;
}
function updatec($data, $bag, &$answer)
{
foreach ($data as $dk => $dv) {
if (array_key_exists($bag, $dv)) {
array_push($answer, $dk);
}
}
}
$data = formatbags($in);
$ac = 0;
$answer = array();
foreach ($data as $dk => $dv) {
if (array_key_exists("shiny gold", $dv)) {
array_push($answer, $dk);
}
}
while ($ac !== count($answer)) {
$ac = count($answer);
$answer = array_unique($answer);
foreach ($answer as $bag) {
updatec($data, $bag, $answer);
}
}
$answer = array_unique($answer);
echo count($answer). "\n";

64
Day 7/Part_2.php

@ -0,0 +1,64 @@
<?php
$in = file("input.txt");
function formatbags($in)
{
$bags = array();
foreach ($in as &$rule) {
$rule = explode(" ", str_replace(".", "", str_replace(",", "", trim($rule))));
$bag = array_shift($rule) . " " . array_shift($rule);
array_shift($rule);
array_shift($rule);
$bags[$bag] = array();
while (count($rule) > 0) {
if ($rule[0] == "no") {
break;
} else {
$num = array_shift($rule);
$type = array_shift($rule) . " " . array_shift($rule);
array_shift($rule);
$bags[$bag][$type] = $num;
}
}
}
return $bags;
}
function bc2($bag, $count, $data, &$s)
{
$c2 = 0;
foreach ($data[$bag] as $sk => $sv) {
$c2 = $c2 + $sv * $count;
if (array_key_exists($sk, $s)) {
$s[$sk] = $s[$sk] + ($sv * $count);
}else{
$s[$sk] = $sv * $count;
}
}
print $c2 . "\n";
return $c2;
}
$data = formatbags($in);
$c = 0;
$s = array("shiny gold" => 1);
while (count($s) > 0) {
foreach ($s as $bag => $no) {
$c = $c + bc2($bag,$no,$data,$s);
}
array_shift($s);
}
echo $c;

37
Day 8/Part_1.php

@ -0,0 +1,37 @@
<?php
$in = file("input.txt");
foreach ($in as &$code) {
$code = explode(" ", trim($code));
$code[1] = (int)$code[1];
$code[2] = 0;
}
$x = 0;
$acc = 0;
while ($x < count($in)) {
$in[$x][2]++;
if ($in[$x][2] > 1) {
print $acc . "\n";
die();
}
switch ($in[$x][0]) {
case "acc";
$acc = $acc + $in[$x][1];
$x++;
break;
case "jmp";
$x = $x + $in[$x][1];
break;
case "nop";
$x++;
break;
}
}

78
Day 8/Part_2.php

@ -0,0 +1,78 @@
<?php
$in = file("input.txt");
foreach ($in as &$code) {
$code = explode(" ", trim($code));
$code[1] = (int)$code[1];
$code[2] = 0;
if ($code[0] == "nop" or $code[0] == "jmp") {
$code[3] = 0;
} else {
$code[3] = 2;
}
}
function swap(&$code)
{
foreach ($code as &$ins) {
$ins[2] = 0;
if ($ins[3] == 1) {
if ($ins[0] == "nop") {
$ins[0] = "jmp";
$ins[3] = 2;
} else {
$ins[0] = "nop";
$ins[3] = 2;
}
}
}
foreach ($code as &$ins) {
if ($ins[3] == 0) {
if ($ins[0] == "nop") {
$ins[0] = "jmp";
$ins[3] = 1;
break;
} else {
$ins[0] = "nop";
$ins[3] = 1;
break;
}
}
}
}
$x = 0;
$acc = 0;
while ($x < count($in)) {
$in[$x][2]++;
if ($in[$x][2] > 1) {
swap($in);
$x = 0;
$acc = 0;
}
switch ($in[$x][0]) {
case "acc";
$acc = $acc + $in[$x][1];
$x++;
break;
case "jmp";
$x = $x + $in[$x][1];
break;
case "nop";
$x++;
break;
}
}
if ($x == count($in)) {
print $acc . "\n";
print "done";
}

11
README.md

@ -0,0 +1,11 @@
|Day|Part 1|Part 2|
|--|--|--|
|1|:heavy_check_mark:|:heavy_check_mark:|
|2|:heavy_check_mark:|:heavy_check_mark:|
|3|:heavy_check_mark:|:heavy_check_mark:|
|4|:heavy_check_mark:|:heavy_check_mark:|
|5|:heavy_check_mark:|:heavy_check_mark:|
|6|:heavy_check_mark:|:heavy_check_mark:|
|7|:heavy_check_mark:|:x:|
|8|:heavy_check_mark:|:heavy_check_mark:|
Loading…
Cancel
Save