-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.php
executable file
·44 lines (43 loc) · 2.08 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env php
<?php
include("common.php");
$lines = read_input();
$allgroups = ['body'=>[], 'infection'=>[]]; $c=0; $cindex = '';
foreach($lines as $line){ $c++;
if(''===$line)continue;
if(':' === $line[-1]){ $cindex = ['Immune System:'=>'body', 'Infection:'=>'infection'][ $line ] ?? ''; continue; }
$allgroups[$cindex][$c] = new Group($c,$cindex,$line);
}
Group::show($allgroups);
class Group {
var $id=0;
var $kind='';
var $units=0;
var $hp=0;
var $weakto=[];
var $strongto=[];
var $adamage=0;
var $atype='';
var $initiative=0;
function __construct(int $id, string $kind, string $line){ $this->id = $id; $this->kind = $kind; $this->extractInfoFromLine($line); }
function __toString(): string {
return sprintf("Group{ id: %2d, kind: %-9s, units: %4d, hp: %5d, adamage: %4d, atype: %11s, initiative: %2d, weakto: [%-24s], strongto: [%-24s]}",
$this->id, $this->kind, $this->units, $this->hp, $this->adamage, $this->atype, $this->initiative, join(', ', $this->weakto), join(', ', $this->strongto));
}
static function show(array $allgroups){
foreach($allgroups as $gkind=>$groups){ printf("%s groups:\n", $gkind); foreach($groups as $g) printf("%s\n", $g); }
}
function extractInfoFromLine(string $line){
sscanf($line, "%d units each with %d hit points", $this->units, $this->hp);
if(strpos($line, '(')>0){
$_skip = strtok($line, '()'); $options = strtok('()'); $_skip = strtok('()');
$aoptions = explode('; ', $options);
foreach($aoptions as $a){
if(strpos($a, $x='weak to')===0) $this->weakto = explode(',',str_replace(' ','',str_replace($x,'',$a)));
if(strpos($a, $x='immune to')===0) $this->strongto = explode(',',str_replace(' ','',str_replace($x,'',$a)));
}
}
if(preg_match_all("/with an attack that does (\d+) (.+) damage at initiative (\d+)$/", $line, $b))
[ $this->adamage, $this->atype, $this->initiative ] = [ (int) $b[1][0], $b[2][0], (int) $b[3][0] ];
}
}