-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtickets.php
executable file
·146 lines (113 loc) · 2.56 KB
/
tickets.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/php
<?php
class Range {
public $min;
public $max;
public function __construct($min, $max) {
$this->min = (int)$min;
$this->max = (int)$max;
}
public function test($value) {
if($value < $this->min) return false;
if($value > $this->max) return false;
return true;
}
}
class Field {
public $index;
public $name;
public $ranges;
public function __construct($name, $ranges) {
$this->index = null;
$this->name = $name;
$this->ranges = $ranges;
}
public function test($value) {
$value = (int)$value;
foreach($this->ranges as $r) {
if($r->test($value)) return true;
}
return false;
}
}
$lines = explode("\n", file_get_contents("/dev/stdin"));
// -- ranges
$fields = [];
$l = 0;
for(; $lines[$l] !== ""; ++$l) {
list($name, $ranges) = explode(": ", $lines[$l]);
$ranges = array_map(
function($x) {
$x = explode("-", $x);
return new Range($x[0], $x[1]);
},
explode(" or ", $ranges)
);
$fields[] = new Field($name, $ranges);
}
$field_count = count($fields);
// -- our ticket
$l += 2;
$our_ticket = array_map('intval', explode(",", $lines[$l]));
$l += 3;
// -- nearby tickets
$tickets = [$our_ticket];
$error_rate = 0;
$count = count($lines) - 1;
for(; $l < $count; ++$l) {
$ticket_data = array_map('intval', explode(",", $lines[$l]));
$ticket_valid = true;
foreach($ticket_data as $td) {
foreach($fields as $f) {
if($f->test($td)) {
continue 2;
}
}
$ticket_valid = false;
$error_rate += $td;
}
if($ticket_valid) {
$tickets[] = $ticket_data;
} else {
}
}
echo "Part1: ", $error_rate, "\n";
// -- part 2
$unknown_fields = [];
foreach($fields as $index => $f) $unknown_fields[] = $index;
$known_columns = [];
while(!empty($unknown_fields)) {
foreach($unknown_fields as $f) {
$possible = [];
for($col = 0; $col < $field_count; ++$col) {
if(isset($known_columns[$col])) continue;
foreach($tickets as $t) {
if(!$fields[$f]->test($t[$col])) {
continue 2;
}
}
$possible[] = $col;
}
if(count($possible) === 1) {
$col = $possible[0];
echo "--> Column ", $col, " is field \"", $fields[$f]->name, "\"\n";
$fields[$f]->index = $col;
unset($unknown_fields[$f]);
$known_columns[$col] = true;
continue 2;
}
}
echo "Unable to match any more fields with columns\n";
exit(1);
}
echo "--> Our ticket:\n";
foreach($fields as $f) {
echo "--> * ", $f->name, ": ", $our_ticket[$f->index], "\n";
}
$our_score = 1;
foreach($fields as $f) {
if(substr($f->name, 0, 9) === "departure") {
$our_score *= $our_ticket[$f->index];
}
}
echo "Part2: ", $our_score, "\n";