Skip to content

Commit 5d23d35

Browse files
committed
day11
1 parent 05ffda5 commit 5d23d35

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

2024/day11.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
input <- scan(read.input(11))
2+
options(digits=22)
3+
stones <- input
4+
#stones <- c(125,17)
5+
for(t in 1:25){
6+
i <- 1
7+
while(i<=length(stones)){
8+
if(stones[i]==0){
9+
stones[i] <- 1
10+
i <- i+1
11+
}else if(!nchar(stones[i])%%2){
12+
n <- nchar(stones[i])/2
13+
new <- as.integer(c(substr(stones[i],1,n),
14+
substr(stones[i],n+1,nchar(stones[i]))))
15+
if(i==1){
16+
stones <- c(new,stones[-i])
17+
}else if(i==length(stones)){
18+
stones <- c(stones[-i], new)
19+
}else{
20+
stones <- c(stones[1:(i-1)],new,stones[(i+1):length(stones)])
21+
}
22+
i <- i+2
23+
}else{
24+
stones[i] <- stones[i]*2024
25+
i <- i+1
26+
}
27+
}
28+
cat(t,":",length(stones),"\n")
29+
}
30+
length(stones)
31+
#185894
32+
33+
blink <- \(x){
34+
if(x==0){
35+
return(1)
36+
}else if(!nchar(x)%%2){
37+
n <- nchar(x)/2
38+
a <- as.integer(substr(x,1,n))
39+
b <- as.integer(substr(x,n+1,nchar(x)))
40+
return(c(a,b))
41+
}else{
42+
return(x*2024)
43+
}
44+
}
45+
46+
stones <- input
47+
memo <- cbind(stones,1)
48+
49+
memorize <- \(x,n){
50+
if(!x%in%memo2[,1]){
51+
memo2 <<- rbind(memo2,c(x,n))
52+
}else{
53+
memo2[memo2[,1]==x,2] <<- memo2[memo2[,1]==x,2] + n
54+
}
55+
}
56+
57+
for(t in 1:75){
58+
memo2 <- matrix(ncol=2,nrow=0)
59+
for(i in memo[,1]){
60+
for(j in blink(i)){
61+
memorize(j,memo[memo[,1]==i,2])
62+
}
63+
}
64+
memo <- memo2
65+
cat(t,"\r")
66+
}
67+
#221632504974231

2024/screenshot.png

4 KB
Loading

Other/2016.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ while(length(res)<8){
157157
res <- c(res,substr(md,6,6))
158158
}
159159
i <- i +1
160-
cat(res,"\r")
160+
if(!i%%10000)cat(res,"\r")
161161
}
162-
162+
cat(res,sep="")
163163

164164
# Day 6
165165
## Part 1

0 commit comments

Comments
 (0)