-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratchcards.php
executable file
·83 lines (71 loc) · 1.87 KB
/
scratchcards.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/php
<?php
function str_squeeze($input, $charset = ' ') {
$len = strlen($input);
if($len === 0) return "";
if($len === 1) return $input;
$output = $input[0];
$prevChr = $input[0];
$prevBad = strstr($charset, $prevChr) !== false;
for($i = 1; $i < $len; ++$i) {
$c = $input[$i];
if(($prevBad) && ($prevChr === $c)) {
// quack
} else {
$output .= $c;
}
$prevChr = $c;
$prevBad = strstr($charset, $prevChr) !== false;
}
return $output;
}
function arrayOfString_to_arrayOfNumber($input) {
$output = [];
foreach($input as $key => $value) {
$output[$key] = intval($value);
}
return $output;
}
$idx = 0;
$copiesByIdx = [];
$sum = 0;
while(($line = fgets(STDIN)) !== false) {
$line = str_squeeze(trim($line));
if($line === "") continue;
$parts = explode(': ', $line);
if(count($parts) !== 2) {
fprintf(STDERR, "Failed to parse line: splitting by ': ' yielded %d parts, expected 2\n", count($parts));
die(1);
}
[$winning, $player] = explode(' | ', $parts[1]);
if(count($parts) !== 2) {
fprintf(STDERR, "Failed to parse line: splitting by ' | ' yielded %d parts, expected 2\n", count($parts));
die(1);
}
$winning = arrayOfString_to_arrayOfNumber(explode(' ', $winning));
$player = arrayOfString_to_arrayOfNumber(explode(' ', $player));
$value = 0;
$matches = 0;
foreach($player as $p) {
foreach($winning as $w) {
if($p === $w) {
$value = ($value === 0) ? 1 : ($value * 2);
$matches += 1;
}
}
}
$sum += $value;
if(!isset($copiesByIdx[$idx])) $copiesByIdx[$idx] = 1;
$copies = $copiesByIdx[$idx];
// fprintf(STDERR, "Have %d copies of card %d\n", $copies, $idx+1);
while($matches > 0) {
if(!isset($copiesByIdx[$idx + $matches])) {
$copiesByIdx[$idx + $matches] = 1;
}
$copiesByIdx[$idx + $matches] += $copies;
--$matches;
}
$idx += 1;
}
echo "Part1: ", $sum, "\n";
echo "Part2: ", array_sum($copiesByIdx), "\n";