Browse Source

Day 13

(had to sleep on part 2)
Petra Lamborn 3 years ago
parent
commit
b24c387b42
2 changed files with 37 additions and 0 deletions
  1. 35
    0
      13.R
  2. 2
    0
      input13.txt

+ 35
- 0
13.R View File

@@ -0,0 +1,35 @@
1
+library(stringr)
2
+input <- readLines("input13.txt")
3
+v <- as.numeric(input[1])
4
+vals <- unlist(str_split(input[2], ","))
5
+bnums <- as.numeric(vals[vals != "x"])
6
+times <- bnums - (v %% bnums)
7
+bindex <- which.min(times)
8
+
9
+bnums[bindex] * times[bindex]
10
+
11
+
12
+
13
+vbt <- which(vals != "x") - 1
14
+
15
+target.mod <- (bnums - vbt) %% bnums
16
+
17
+
18
+f2 <- sapply(1:bnums[2], function(x) {
19
+           c(x*bnums[1], ((x*bnums[1]) %% bnums[2]) == target.mod[2])
20
+})
21
+
22
+val.2 <- f2[1,f2[2,] == 1]
23
+
24
+
25
+tmp.v <- 0
26
+sapply(2:length(bnums), function(x) {
27
+           ft <- sapply(1:bnums[x], function(y) {
28
+                            cand <- tmp.v + y*prod(bnums[1:(x - 1)])
29
+                            c(cand, (cand %% bnums[x]) == target.mod[x])
30
+    })
31
+           tmp.v <<- ft[1, ft[2,] == 1]
32
+           tmp.v
33
+})
34
+
35
+print(tmp.v, digits = 15)

+ 2
- 0
input13.txt View File

@@ -0,0 +1,2 @@
1
+1000067
2
+17,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,439,x,29,x,x,x,x,x,x,x,x,x,x,13,x,x,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,787,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,19