Browse Source

Second part of first day

Petra Lamborn 1 year ago
parent
commit
caa357c35b
2 changed files with 25 additions and 1 deletions
  1. 7
    1
      1a.R
  2. 18
    0
      1b.R

+ 7
- 1
1a.R View File

@@ -1,8 +1,14 @@
1
+# Target: find two numbers that sum to 2020, submit them multiplied together
2
+
3
+# Get input
1 4
 input <- read.csv("input1a.csv", header=F)[[1]]
2 5
 
6
+# Find the value that would have to be present for them to add to 2020
3 7
 alt <- 2020 - input
4 8
 
9
+# Returns the three numbers
5 10
 vals <- input[input %in% alt]
6 11
 vals
12
+
7 13
 # 719796
8
-vals[1] * vals[2]
14
+prod(vals)

+ 18
- 0
1b.R View File

@@ -0,0 +1,18 @@
1
+# Target: find *three* numbers that sum to 2020, submit them multiplied together
2
+
3
+# Get input
4
+input <- read.csv("input1a.csv", header=F)[[1]]
5
+
6
+# Add every pair of numbers together
7
+i2 <- outer(input, input, FUN = "+")
8
+
9
+# Find the value that would have to be present for them to add to 2020
10
+alt <- 2020 - i2
11
+
12
+# Returns the three numbers
13
+vals <- input[input %in% alt]
14
+vals
15
+
16
+# 144554112
17
+prod(vals)
18
+