Advent of code Day 7

Goal: Regex Headche

Ref: http://adventofcode.com/2016/day/7


In [1]:
library(stringr)

In [2]:
data <- readLines("data.txt")
head(data)


  1. "rhamaeovmbheijj[hkwbkqzlcscwjkyjulk]ajsxfuemamuqcjccbc"
  2. "gdlrknrmexvaypu[crqappbbcaplkkzb]vhvkjyadjsryysvj[nbvypeadikilcwg]jwxlimrgakadpxu[dgoanojvdvwfabtt]yqsalmulblolkgsheo"
  3. "dqpthtgufgzjojuvzvm[eejdhpcqyiydwod]iingwezvcbtowwzc[uzlxaqenhgsebqskn]wcucfmnlarrvdceuxqc[dkwcsxeitcobaylhbvc]klxammurpqgmpsxsr"
  4. "gmmfbtpprishiujnpdi[wedykxqyntvrkfdzom]uidgvubnregvorgnhm"
  5. "txxplravpgztjqcw[txgmmtlhmqpmmwp]bmhfgpmafxqwtrpr[inntmjmgqothdzfqgxq]cvtwvembpvdmcvk"
  6. "gkxjhpayoyrrpcr[mwyoahlkqyhtznyzrm]mvmurvsrgjunjjepn[mkoumuohilpcfgbmsmh]hpwggyvjkusjxcyojyr[wqxyuzbewpjzlyqmkhw]nniczueulxtdsmkniex"

In [3]:
outside <- str_match(data,".*(?:[^\\[]+|^|\\].*)(\\w)((?!\\1).)\\2\\1.*") #Long to run

In [4]:
inside <- str_match(data,".*(?:\\[)\\w*(\\w)((?!\\1).)\\2\\1\\w*\\].*") #Long to run

In [5]:
insideList <- as.data.frame(inside)[1]
outsideList <- as.data.frame(outside)[1]

In [6]:
valid <- setdiff(which(!is.na(outsideList)),which(!is.na(insideList)))
length(valid)


110

Part 2

I have decided to go with a REGEX it was a mistake I should i have done this differently. I should I just split the inside and outside, then simple regex to find patern and then use R logic to fix This is way too complex and long to run


In [7]:
partern2 <- str_match(data,"(?:\\w*(?:\\[\\w*\\])*.*(\\w)(?!\\1)(\\w)\\1\\w*(?:\\[\\w*\\]\\w*)*(?:\\[)\\w*\\2\\1\\2.*)|(?:.*(?:\\[)\\w*(\\w)((?!\\3).)\\3\\w*\\]\\w*(?:\\[\\w*\\]\\w*)*\\w*\\4\\3\\4.*)")

In [8]:
partern2List <- as.data.frame(partern2)[1]

In [9]:
valid2 <- which(!is.na(partern2List))
length(valid2)


242

In [10]:
cat("Q1 - Number of IPs that support TLS : ", length(valid), '\n')
cat("Q2 - Number of IPs that support SSL : ", length(valid2), '\n')


Q1 - Number of IPs that support TLS :  110 
Q2 - Number of IPs that support SSL :  242