Advent of Code, day 25

This December I participated in Advent of Code.

Part 1 was sort of easy. Check whether a lock pin and key pin overlap in this particular combination, and go through all the possible combinations, counting. Part 2 turned out to be: Complete all the 49 other parts this December, and click this link!

Part 1:

<?php

///////////////////////////////////////////////////

$input = file_get_contents('./d25input2.txt', true);

$item = 0;
foreach(preg_split("/((\r?\n)|(\r\n?))/", $input) as $line) {
  if(strlen($line)>2) {
    $items[$item][] = str_split($line);
  } else {
    $item++;
  }
}

foreach($items as $item) {
  $tmp = implode($item[0]);
  if($tmp == "#####") {
    $locks[] = $item;
  } else {
    $keys[] = $item;
  }
}

foreach($locks as $id => $lock) {
  // each pin, 5 of them
  for($i=0;$i<5;$i++) {
    // each pin element, 7 of them
    $pinh = 0;
    for($j=1;$j<7;$j++) {
      if($lock[$j][$i] == "#") {
        $pinh++;
      }
    }
    $locksig[$id][] = $pinh;
  }
}

foreach($keys as $id => $key) {
  // each pin, 5 of them
  for($i=0;$i<5;$i++) {
    // each pin element, 7 of them
    $pinh = 0;
    for($j=5;$j>0;$j--) {
      if($key[$j][$i] == "#") {
        $pinh++;
      }
    }
    $keysig[$id][] = $pinh;
  }
}

$lockkeymatch = 0;

foreach($locksig as $lock) {
  foreach($keysig as $key) {
    $tmp = array();
    for($i=0;$i<5;$i++) {
      $tmp[] = $lock[$i] + $key[$i];
    }
    if(max($tmp) <= 5) {
      $lockkeymatch++;
    }
  }
}

print("$lockkeymatch\n");

?>

Skriv en kommentar