Browse Source

because who needs loops?

Petra Lamborn 3 years ago
parent
commit
499d0aaa8c
2 changed files with 1031 additions and 0 deletions
  1. 31
    0
      9.R
  2. 1000
    0
      input9.txt

+ 31
- 0
9.R View File

@@ -0,0 +1,31 @@
1
+input <- as.numeric(readLines("input9.txt"))
2
+
3
+allplus <- function(vec) {
4
+    unlist(lapply(1:(length(vec) - 1), function(i) {
5
+                      vec[i] + vec[(i + 1):length(vec)]
6
+    }))
7
+}
8
+
9
+vvals <- sapply(1:length(input), function(x) {
10
+           if (x <= 25) {
11
+               return(TRUE)
12
+           }
13
+           tnum <- input[x]
14
+           l25 <- input[(x - 25):(x - 1)]
15
+           return(tnum %in% allplus(l25))
16
+           
17
+})
18
+
19
+inval <- input[!vvals][1]
20
+inval
21
+
22
+alladd <- sapply(1:(length(input) - 1), function(x) {
23
+           sapply((x + 1):length(input), function(y) {
24
+                      sel <- input[x:y]
25
+                      if (sum(sel) == inval) {
26
+                          print(sel)
27
+                          print(min(sel) + max(sel))
28
+                      }
29
+                      return(sum(sel))
30
+    })
31
+})

+ 1000
- 0
input9.txt
File diff suppressed because it is too large
View File