-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththermalvents.r
More file actions
66 lines (55 loc) · 1.91 KB
/
thermalvents.r
File metadata and controls
66 lines (55 loc) · 1.91 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
# ___ __ ___
# | \ __ _ _ _ / \| __|
# | |) / _` | || | | () |__ \
# |___/\__,_|\_, | \__/|___/
# |__/
#
# "Hydrothermal Venture"
#
# Challenge:
# Given a list of vectors (like, two coord pairs) figure out which represent
# straight lines through a field and calc the number of points with a value > 2.
# Make a field. The sample is 10x10, the production data are 1000x1000
size <- 1000
field <- matrix(data = 0, nrow = size, ncol = size)
# Check two coord pairs and see if they're a horiz or vert line?
straightLine <- function(a, b) {
if (a[1] == b[1] || a[2] == b[2]) {
TRUE
} else {
FALSE
}
}
for (line in scan("input.txt", what = "raw", sep = "\n")) {
# Split the line up into numbers and pair them into coordinates.
x <- unlist(strsplit(line, "(,| -> )", perl = TRUE))
# Rotate the incoming coord pairs and add 1 since R is 1-indexed.
a <- c(strtoi(x[2]) + 1, strtoi(x[1]) + 1)
b <- c(strtoi(x[4]) + 1, strtoi(x[3]) + 1)
# For Part 1, we're told to only look at straight lines.
if (straightLine(a, b) == TRUE) {
# Make a fresh matrix of zeroes, and put a 1 where this line passes
updates <- matrix(data = 0, nrow = size, ncol = size)
updates[a[1]:b[1],a[2]:b[2]] <- 1
# Add the updates to the field that exists.
field <- field + updates
} else {
# ___ _ ___
# | _ \__ _ _ _| |_ |_ )
# | _/ _` | '_| _| / /
# |_| \__,_|_| \__| /___|
#
# Also diagonal lines, lol.
updates <- matrix(data = 0, nrow = size, ncol = size)
# Assign 1 to the diagonal described by the line
diag(updates[a[1]:b[1],a[2]:b[2]]) <- 1
# Like above, add to field and move on.
field <- field + updates
}
}
hotspots <- sum(field > 1, na.rm = TRUE)
sprintf("There are %d hotspots in the field.", hotspots)
# Part One:
# [1] "There are 5145 hotspots in the field."
# Part Two:
# [1] "There are 16518 hotspots in the field."