-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrekombat.php
executable file
·72 lines (59 loc) · 1.49 KB
/
rekombat.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
#!/usr/bin/php
<?php
function read_input() {
return array_map(
function($a) {
$a = explode("\n", trim($a));
array_shift($a);
return $a;
},
explode("\n\n", file_get_contents("/dev/stdin"))
);
}
function calc_score($a) {
$sum = 0;
$count = count($a);
for($i = 0; $i < $count; ++$i) {
$sum += ($count - $i) * $a[$i];
}
return $sum;
}
function serialize_round($p1, $p2) {
return implode(',', $p1) . ';' . implode(',', $p2);
}
function new_game(&$p1, &$p2) {
$history = [];
while(!empty($p1) && !empty($p2)) {
$current = serialize_round($p1, $p2);
if(in_array($current, $history)) return 1;
$history[] = $current;
$p1_top = array_shift($p1);
$p1_enough = (count($p1) >= $p1_top);
$p2_top = array_shift($p2);
$p2_enough = (count($p2) >= $p2_top);
if($p1_enough && $p2_enough) {
$p1_sub = array_slice($p1, 0, $p1_top);
$p2_sub = array_slice($p2, 0, $p2_top);
if(new_game($p1_sub, $p2_sub) === 1) {
array_push($p1, $p1_top, $p2_top);
} else {
array_push($p2, $p2_top, $p1_top);
}
} else {
if($p1_top > $p2_top) {
array_push($p1, $p1_top, $p2_top);
} else if($p2_top > $p1_top) {
array_push($p2, $p2_top, $p1_top);
} else {
echo "Draw! No idea how to proceed.\n";
exit(1);
}
}
}
return empty($p2) ? 1 : 2;
}
list($p1, $p2) = read_input();
$winner = new_game($p1, $p2);
$windeck = ($winner === 1) ? $p1 : $p2;
echo "Winner(", $winner, "): ", implode(', ', $windeck), "\n";
echo "Part2: ", calc_score($windeck), "\n";