AOC_2022/3/tuer3.php

47 lines
2.0 KiB
PHP
Raw Normal View History

2022-12-04 01:36:17 +01:00
<?php
$input; $first_comp = array(); $second_comp = array();
$keys = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$input_file = fopen("./input.txt", "r");
$input = fread($input_file, filesize("./input.txt"));
$lines_arr = explode("\n", $input);
$sum = 0; $sum2=0;
for ($i=0; $i<count($lines_arr); $i++) {
$lines_arr[$i] = trim($lines_arr[$i]);
$tmp_len = strlen($lines_arr[$i]);
array_push($first_comp, substr($lines_arr[$i], 0, $tmp_len / 2));
array_push($second_comp, substr($lines_arr[$i], ($tmp_len / 2), ($tmp_len / 2)));
$first_substr_arr = str_split($first_comp[$i], 1);
$second_substr_arr = str_split($second_comp[$i], 1);
$equal_char;
for($k=0; $k<count($first_substr_arr); $k++) {
for($j=0; $j<count($second_substr_arr); $j++) {
if ($first_substr_arr[$k] == $second_substr_arr[$j]) $equal_char = $second_substr_arr[$j];
}
}
$sum += array_search($equal_char, $keys, true) + 1;
if($i % 3 == 2 && $i>0) {
$bingo = str_split($lines_arr[$i - 2], 1);
$bongo = str_split($lines_arr[$i - 1], 1);
$bumms = str_split($lines_arr[$i - 0], 1);
$bimmler;
for($k=0; $k<count($bingo); $k++) {
for($j=0; $j<count($bongo); $j++) {
for($l=0; $l<count($bumms); $l++) {
if ($bingo[$k] == $bongo[$j] && $bingo[$k] == $bumms[$l] && $bongo[$j] == $bumms[$l]) $bimmler = $bumms[$l];
}
}
}
var_dump($bimmler);
$sum2 += array_search($bimmler, $keys, true) + 1;
}
}
print_r("<b>Erste Summe: </b>".$sum."<br>");
print_r("<b>Zweite Summe: </b>".$sum2);