-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.php
executable file
·27 lines (26 loc) · 1.16 KB
/
solution.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env php
<?php
include("common.php");
$input = join('', read_input());
$small = range('a','z'); $BIG = range('A','Z');
$reactionpairs = Amap(array_merge(Azip2($small,$BIG), Azip2($BIG,$small)), function($x){ return join('', $x); });
function fullReaction(string $input, array $reactionpairs): array {
$c=0; while(true){
$c++;
$oldinput = $input;
$input = str_replace($reactionpairs, '', $oldinput);
if(strlen($input) === strlen($oldinput)) break;
}
return [$c, $input];
}
$part1 = fullReaction($input, $reactionpairs);
printf("Part 1: reactions: %d | remaining units = answer = %d\n", $part1[0], strlen($part1[1]));
$part2results = [];
foreach($reactionpairs as $pair){
$cleanedinput = str_replace(line2array($pair), '', $input);
//printf("pair: %s, ilen: %d, cleanedlen: %d\n", $pair, strlen($input),strlen($cleanedinput));
[$c, $reactresult] = fullReaction($cleanedinput, $reactionpairs);
$part2results[$pair] = strlen($reactresult);
}
[$thepair, $part2] = Aminkv($part2results);
printf("Part 2: the length of the shortest polymer after removing all units of exactly one type: '%s' is: %d\n", $thepair, $part2);