input<-read_lines("Day14Sample.txt")
robotframe<-as.data.frame(matrix(ncol=4,nrow=0))
for(i in 1:length(input)){
y<-as.numeric(unlist(str_extract_all(input[i],"-?\\d+")))
robotframe<-rbind(robotframe,y)}
colnames(robotframe)<-c("px","py","vx","vy")
Move everything about in the frame and then count how many are in each quadrant
safetyfactor<-function(rf,max_x,max_y,s){
midx<-max_x%/%2
midy<-max_y%/%2
rf<-rf%>%rowwise%>%
### find the final x & y position
mutate(finalx=((px+s*vx)%%max_x),finaly=((py+s*vy)%%max_y)) %>%
### find which quadrant it is in
mutate(quad=if_else(finalx<midx&&finaly<midy,1,
if_else(finalx>midx&&finaly<midy,2,
if_else(finalx<midx&&finaly>midy,3,
if_else(finalx>midx&&finaly>midy,4,0)))))
sf<-rf%>%count(quad)%>%filter(quad!=0)%>%arrange(quad)
sf<-sf$n
sf}
p1<-safetyfactor(robotframe,11,7,100)
p1
[1] 1 3 4 1
part1<-prod(p1)
part1
[1] 12
Because there is a repeat every 10403, animate and watch that long is a possibility, but that’s a lot of rendering and watching
My first idea was that the tree would be centered around the midline - so I was looking for symmetry across the quadrants - that went nowhere.
Then, I thought about looking for anomalies in the safety factor -
This slightly alters the safetyfactor function from above
safac2<-function(rf,max_x,max_y,s){
midx<-max_x%/%2
midy<-max_y%/%2
rf<-rf%>%rowwise%>%
### find the final x & y position
mutate(finalx=((px+s*vx)%%max_x),finaly=((py+s*vy)%%max_y)) %>%
select(finalx,finaly)%>%
### find which quadrant it is in
mutate(quad=if_else(finalx<midx&&finaly<midy,1,
if_else(finalx>midx&&finaly<midy,2,
if_else(finalx<midx&&finaly>midy,3,
if_else(finalx>midx&&finaly>midy,4,0)))))
sf<-rf%>%count(quad)%>%filter(quad!=0)%>%arrange(quad)
sf<-sf$n
sf<-c(sf,prod(sf))
sf}
### find tree runs this through all repeats. It also takes forever.
findtree<-function(rf,max_x,max_y){
treeframe<-as.data.frame(matrix(ncol=6,nrow=0))
for(i in 1:(max_x*max_y)){
t<-safac2(rf,max_x,max_y,i)
treeframe<-rbind(treeframe,c(i,t))}
colnames(treeframe)<-c("dex","q1","q2","q3","q4","safetyfactor")
treeframe}
Running this allowed me to look for clumps
quadsearch<-findtree(robotframe,101,103)
The plot shows that right around 7000, theres an especially large group of robots in q1.
And plotting that one specifically shows
plottree<-function(rf,max_x,max_y,s){
midx<-max_x%/%2
midy<-max_y%/%2
rf<-rf%>%rowwise%>%
### find the final x & y position
mutate(finalx=((px+s*vx)%%max_x),finaly=((py+s*vy)%%max_y)) %>%
### find which quadrant it is in
select(finalx,finaly)
rf}
sevenzerofivefive<-plottree(robotframe,101,103,7055)
second_7055<-ggplot(data=sevenzerofivefive,aes(x=finalx,y=finaly))+
geom_point()+
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position="none")+
scale_y_reverse()+
coord_fixed()
second_7055
Tree
Finally - here’s a sped up version of the 4 minutes before the tree appears.