-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent1.R
95 lines (58 loc) · 1.85 KB
/
advent1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
library(stringr)
# Read input
adv1 = readLines("input/1_2023")
# Loop over each line of text
key = c()
key = sapply(adv1, function(i){
# convert to lowercase
word = str_to_lower(i)
# unlist to extract characters as a vector
chars <- unlist(str_split(word, ""))
# keep only numbers
nums <- chars[!chars %in% letters]
# double to a single-digit (if necessary)
if(length(nums) == 1) {
nums = as.numeric(str_c(nums, nums))
key = c(key, nums)} else {
# extract (combine) the 1st and last numbers
key <- c(key, as.numeric(str_c(nums[1],tail(nums, n = 1))))
}
}, USE.NAMES = F)
# Sum each element
sum(key)
# Part 2 -----------------------------------
num_byletters <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
word = "two1nine"
# convert to lowercase
word = str_to_lower(word)
# remove NA
tmp = str_extract(word, num_byletters)[!is.na(str_extract(word, num_byletters))]
key = c()
# antes de la 1a palabra
k = head(tmp, n= 1)
chars = str_split(str_split(word, k)[[1]][1],"")[[1]][1]
if(nchar(chars) == 0 | is.na(chars)) {
nums_tmp = k
} else {nums_tmp = chars}
# convertir a numero
print(nums_tmp)
if(nchar(nums_tmp) == 1){nums_tmp = as.numeric(nums_tmp) }else{nums_tmp = match(nums_tmp, num_byletters)}
key_tmp = nums_tmp
# despues de ultima palabra
k = tail(tmp, n= 1)
# si hay numero : lo retengo
chars <-unlist(str_split(str_split(word, k)[[1]][2], "")[[1]])
nums <- chars[!chars %in% letters]
if(length(nums) == 0) {nums_tmp = k} else {nums_tmp = tail(nums, n = 1)}
# convertir a numero
print(nums_tmp)
if(nchar(nums_tmp) == 1){nums_tmp = as.numeric(nums_tmp) }else{nums_tmp = match(nums_tmp, num_byletters)}
nums_tmp
key_tmp = as.numeric(str_c(key_tmp, nums_tmp))
key = c(key, key_tmp)
key
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen