Skip to content

Instantly share code, notes, and snippets.

@ozjimbob
Created December 15, 2022 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozjimbob/6b785293a58c2aacdbc07cc4cc4bda27 to your computer and use it in GitHub Desktop.
Save ozjimbob/6b785293a58c2aacdbc07cc4cc4bda27 to your computer and use it in GitHub Desktop.
# Day 15
library(sf)
#Sensor at x=1943362, y=12808: closest beacon is at x=1861152, y=-42022
data <- readLines("data_15.txt")
ll = list()
# for each data line, get coordinates, draw a diamond based on manhattan distance
for (i in seq_along(data)) {
d2 = strsplit(data[[i]], "[=,:]")[[1]]
st = as.numeric(c(d2[2], d2[4]))
en = as.numeric(c(d2[6], d2[8]))
dx = abs(st[1] - en[1])
dy = abs(st[2] - en[2])
dst = dx + dy
pol <- list(rbind(
c(st[1], st[2] + dst),
c(st[1] + dst, st[2]),
c(st[1], st[2] - dst),
c(st[1] - dst, st[2]),
c(st[1], st[2] + dst)
))
ll[[i]] = st_polygon(pol)
}
# Make a multipolygon of all these
plist <- st_multipolygon(ll)
# Make a line at 2000000
llist <- list(rbind(c(-10000000, 2000000), c(10000000, 2000000)))[[1]]
lin <- st_linestring(llist)
# Check - plot polygons and lines
plot(plist)
plot(lin, add = TRUE)
# Merge all polygons into a single chunky shape
jj = st_union(plist, by_feature = TRUE)
# Intersect the line, calculate its remaning length inside the shape
kk = st_intersection(lin, jj)
st_length(kk)
# Crop to the 0-4000000 bounding box
extt <- list(rbind(c(0, 0), c(0, 4000000),
c(4000000, 4000000), c(4000000, 0),
c(0, 0)))
extt <- st_polygon(extt)
exi <- st_intersection(jj, extt)
# What is left?
exi
## At this point I'm just gonna manually type in the coordinates I can see, for the centre point of little polygon that remains:
gg = 3012821 * 4000000 + 3042458
sprintf("%f", gg)
#12051291042458 too high
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment