-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.php
More file actions
68 lines (52 loc) · 1.38 KB
/
Copy path18.php
File metadata and controls
68 lines (52 loc) · 1.38 KB
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
<?php
// $input = file("inputs/18-test.txt");
$input = file("inputs/18.txt");
$area = 1;
$xPos = 0;
foreach($input as $line){
$line = trim($line);
[$dir, $steps, $color] = explode(" ", $line);
if($dir == "R"){
$xPos += $steps;
}else if($dir == "L"){
$xPos -= $steps;
}
if($dir == "D"){
$area += $xPos * $steps;
}else if($dir == "U"){
$area += -$xPos * $steps;
}
$area += abs($steps) / 2;
// echo $area . " " . $xPos . " " . $steps . "\n";
}
var_dump($area);
$dirMap = [
"0" => "R",
"1" => "D",
"2" => "L",
"3" => "U",
];
$area = 1;
$xPos = 0;
foreach($input as $line){
$line = trim($line);
[$dir, $steps, $color] = explode(" ", $line);
$dir = $dirMap[$color[-2]];
$steps = hexdec(substr($color, 2, 5));
// var_dump(substr($color, 2, 5));
// exit();
if($dir == "R"){
$xPos += $steps;
}else if($dir == "L"){
$xPos -= $steps;
}
if($dir == "D"){
$area += $xPos * $steps;
}else if($dir == "U"){
$area += -$xPos * $steps;
}
$area += abs($steps) / 2;
// echo $area . " " . $xPos . " " . $steps . "\n";
}
var_dump($area);
?>