This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticket-translation.php
153 lines (140 loc) · 4.02 KB
/
ticket-translation.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
147
148
149
150
151
152
153
<?php
$filename = "input.txt";
function read_input() {
global $filename;
$uncutsections = explode("\r\n\r\n", file_get_contents($filename));
$sections = [];
foreach ($uncutsections as $section) {
$sections[] = explode("\r\n", $section);
}
$rules = new \Ds\Map();
foreach ($sections[0] as $rule) {
$rule = explode(": ", $rule);
$intervals = explode(" or ", $rule[1]);
$vals = [];
foreach ($intervals as $interval) {
array_push($vals, ...explode("-", $interval));
}
$values = [];
foreach (range($vals[0], $vals[1]) as $num) {
$values[] = $num;
}
foreach (range($vals[2], $vals[3]) as $num) {
$values[] = $num;
}
$rules->put($rule[0], $values);
}
$myticket = array_map("intval", explode(",", $sections[1][1]));
$nearby = [];
foreach ($sections[2] as $ticket) {
$nearby[] = array_map("intval", explode(",", $ticket));
}
$data = new \Ds\Map();
$data->put("rules", $rules);
$data->put("myticket", $myticket);
$data->put("nearby", $nearby);
return $data;
}
function check_rule($rule, $number) {
foreach ($rule as $num) {
if ($number == $num) {
return true;
}
}
return false;
}
function check_rules($rules, $number) {
foreach ($rules as $rule => $values) {
if (check_rule($values, $number)) {
return true;
}
}
return false;
}
function solve_part_one() {
$data = read_input();
$errorrate = 0;
foreach ($data->get("nearby") as $ticket) {
foreach ($ticket as $number) {
if (!check_rules($data->get("rules"), $number)) {
$errorrate += $number;
}
}
}
return $errorrate;
}
function valid($rules, $ticket) {
foreach ($ticket as $val) {
if (!check_rules($rules, $val)) {
return false;
}
}
return true;
}
function valid_tickets($data) {
$valid = [$data->get("myticket")];
foreach ($data->get("nearby") as $ticket) {
if (valid($data->get("rules"), $ticket)) {
$valid[] = $ticket;
}
}
return $valid;
}
function possible_fields($rules, $tickets) {
$fieldtable = new \Ds\Map();
$size = count($rules);
foreach ($rules as $rule => $values) {
$possible = [];
for ($i = 0; $i < $size; $i++) {
$valid = true;
foreach ($tickets as $ticket) {
if (!check_rule($values, $ticket[$i])) {
$valid = false;
break;
}
}
if ($valid) {
$possible[] = $i;
}
}
$fieldtable->put($rule, $possible);
}
return $fieldtable;
}
function figure_it_out($possible_positions) {
$positions = new \Ds\Map();
$keys = $possible_positions->keys();
while (!$keys->isEmpty()) {
foreach ($keys as $key) {
if (count($possible_positions->get($key)) == 1) {
$val = current($possible_positions->get($key));
$positions->put($key, $val);
$keys->remove($key);
foreach ($keys as $pos) {
$list = $possible_positions->get($pos);
if (($index = array_search($val, $list)) !== false) {
unset($list[$index]);
$possible_positions->put($pos, $list);
}
}
continue;
}
}
}
return $positions;
}
function solve_part_two() {
$data = read_input();
$validtickets = valid_tickets($data);
$possible = possible_fields($data->get("rules"), $validtickets);
$positions = figure_it_out($possible);
$result = 1;
foreach ($positions as $rule => $value) {
if (strpos($rule, "departure") !== false) {
$result *= $data->get("myticket")[$value];
}
}
return $result;
}
echo solve_part_one() . PHP_EOL;
echo solve_part_two() . PHP_EOL;