-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.r
99 lines (78 loc) · 2.47 KB
/
day17.r
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
file_name <- "sample17.txt"
file_content <- readLines(file_name)
grid_dim <- length(file_content)
grid <- matrix(, nrow = grid_dim, ncol = grid_dim)
for (i in seq_along(file_content)) {
line <- file_content[i]
line_split <- strsplit(line, "")[[1]]
line_nums <- as.numeric(line_split)
grid[i,] <- line_nums
}
cur_pos <- c(1, 1)
move <- function(pos, dir) {
if (dir == 'R') {
return(c(pos[1], pos[2] + 1))
} else if (dir == 'D') {
return(c(pos[1] + 1, pos[2]))
} else if (dir == 'L') {
return(c(pos[1], pos[2] - 1))
} else { # 'U'
return(c(pos[1] - 1, pos[2]))
}
}
get_best_weight <- function(pos, path, same_dir_count, dir) {
# Base case
if (pos[1] < 1 || pos[1] > grid_dim || pos[2] < 1 || pos[2] > grid_dim || path[pos[1], pos[2]]) {
return(-1)
}
print("Now at")
print(pos)
# Try getting best weight from all possible directions
possible_directions <- c()
if (dir == 'X') {
# Special case for start
possible_directions <- c(possible_directions, c('R', 'D'))
} else if (dir == 'R') {
possible_directions <- c(possible_directions, c('U', 'R', 'D'))
} else if (dir == 'D') {
possible_directions <- c(possible_directions, c('R', 'D', 'L'))
} else if (dir == 'L') {
possible_directions <- c(possible_directions, c('D', 'L', 'U'))
} else if (dir == 'U') {
possible_directions <- c(possible_directions, c('L', 'U', 'R'))
}
weight <- -1
path[pos[1], pos[2]] = TRUE
for (next_d in possible_directions) {
if (next_d == dir) {
same_dir_count <- same_dir_count + 1
}
if (same_dir_count >= 4) {
# Can't go straight more than 3 times
next
}
if (pos[1] == grid_dim && pos[2] == grid_dim) {
# Reached the destination!
return(grid[pos[1], pos[2]])
}
next_weight <- get_best_weight(move(pos, next_d), path, same_dir_count, next_d)
if (next_weight < 0) {
# We went somewhere illegal, abandon
next
}
if (weight < 0 || next_weight < weight) {
weight <- next_weight
}
}
path[pos[1], pos[2]] = FALSE
if (weight < 0) {
return(weight)
} else {
return(grid[pos[1], pos[2]] + weight)
}
}
browser()
path <- matrix(FALSE, nrow = grid_dim, ncol = grid_dim)
weight <- get_best_weight(c(1, 1), path, 0, 'X')
print(weight)
warnings()