From 4d54bd32cf3c4f3aecca4b1bed22ca104ddb0c29 Mon Sep 17 00:00:00 2001 From: hrbrmstr Date: Sun, 6 Dec 2020 08:11:49 -0500 Subject: [PATCH] Day 4 js --- R/.Rhistory | 28 +++++------ R/.Rproj.user/shared/notebooks/paths | 1 + R/04.py | 33 ++++++++----- js/04.js | 93 ++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 26 deletions(-) create mode 100644 js/04.js diff --git a/R/.Rhistory b/R/.Rhistory index 9b7df25..3875426 100644 --- a/R/.Rhistory +++ b/R/.Rhistory @@ -1,17 +1,3 @@ -valid = map2(field, value, validate) -) -}) -validate <- function(field, value) { -# print(str(field)) -# print(str(value)) -return( -suppressWarnings( -switch( -field, -"byr" = stri_detect_regex(value, "^[[:digit:]]{4}$") & between(as.integer(value), 1920, 2002), -"iyr" = stri_detect_regex(value, "^[[:digit:]]{4}$") & between(as.integer(value), 2010, 2020), -"eyr" = stri_detect_regex(value, "^[[:digit:]]{4}$") & between(as.integer(value), 2020, 2030), -"hgt" = stri_detect_regex(value, "^[[:digit:]]{2,3}(cm|in)$") & validate_height(value), "hcl" = stri_detect_regex(value, "^#[a-f[:digit:]]{6}$"), "ecl" = value %in% c("amb", "blu", "brn", "gry", "grn", "hzl", "oth"), "pid" = stri_detect_regex(value, "^[[:digit:]]{9}$"), @@ -510,3 +496,17 @@ all(found %in% req_keys) & (length(found) == length(req_keys)) }) %>% sum() reticulate::repl_python() +split(input, findInterval(1:length(input), which(input == ""))) %>% +map_lgl(~{ +paste0(.x, collapse = " ") %>% +stri_match_all_regex("([[:alpha:]]{3}):([^ $]+)") %>% +pluck(1) %>% +.[,2:3] %>% +as.data.frame() %>% +set_names(c("field", "value")) %>% +filter(field != "cid") -> found +if (!(all(found$field %in% req_keys) & (length(found$field) == length(req_keys)))) return(FALSE) +all(map2_lgl(found$field, found$value, validate)) +}) %>% +sum() +reticulate::repl_python() diff --git a/R/.Rproj.user/shared/notebooks/paths b/R/.Rproj.user/shared/notebooks/paths index d00d570..a9fea99 100644 --- a/R/.Rproj.user/shared/notebooks/paths +++ b/R/.Rproj.user/shared/notebooks/paths @@ -14,6 +14,7 @@ /Users/hrbrmstr/Development/2020-code-advent/js/01.js="77D3BDB6" /Users/hrbrmstr/Development/2020-code-advent/js/02.js="BC8B36A1" /Users/hrbrmstr/Development/2020-code-advent/js/03.js="E24752E6" +/Users/hrbrmstr/Development/2020-code-advent/js/04.js="CB04A9CB" /Users/hrbrmstr/Development/2020-code-advent/scala/01/01/src/main/scala/Main.scala="8F03A1D5" /Users/hrbrmstr/Development/2020-code-advent/scala/02/02/src/main/scala/Main.scala="418A1A5F" /private/tmp/test.txt="DFC19AA4" diff --git a/R/04.py b/R/04.py index 91f575c..379562c 100644 --- a/R/04.py +++ b/R/04.py @@ -19,7 +19,7 @@ sum([ test_fields(rec) for rec in recs ]) # 04-02 ------------------------------------------------------------------- def validate_height(value): - match = re.match(r"^(?P[0-9]{2,3})(?Pcm|in)$" + match = re.match(r"^(?P[0-9]{2,3})(?Pcm|in)$", value) val = int(match.group("val")) unit = match.group("unit") if (unit == "cm"): @@ -28,15 +28,17 @@ def validate_height(value): return(59 <= val <= 76) def validate(fval): - return({ - "byr": lambda x: re.match(r"^[0-9]{4}$", fval[1]) && (1920 <=int(fval[1]) <= 2002), - "iyr": lambda x: re.match(r"^[0-9]{4}$", fval[1]) && (2010 <=int(fval[1]) <= 2020), - "eyr": lambda x: re.match(r"^[0-9]{4}$", fval[1]) && (2020 <=int(fval[1]) <= 2030), - "hgt": lambda x: re.match(r"^[0-9]{2,3}(cm|in)$", fval[1]) && validate_height(fval[1]), - "hcl": lambda x: re.match(r"#[a-f0-9]{6}", fval[1]), - "ecl": lambda x: fval[1] in set(["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]), - "hcl": lambda x: re.match(r"[0-9]{9}^$", fval[1]) - }[fval[0]](fval[1])) + is_valid = { + "byr": lambda x: re.match(r"^[0-9]{4}$", x) is not None and (1920 <= int(x) <= 2002), + "iyr": lambda x: re.match(r"^[0-9]{4}$", x) is not None and (2010 <= int(x) <= 2020), + "eyr": lambda x: re.match(r"^[0-9]{4}$", x) is not None and (2020 <= int(x) <= 2030), + "hgt": lambda x: re.match(r"^[0-9]{2,3}(cm|in)$", x) is not None and validate_height(x), + "hcl": lambda x: re.match(r"^#[a-f0-9]{6}$", x) is not None, + "ecl": lambda x: x in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"], + "pid": lambda x: re.match(r"^[0-9]{9}$", x) is not None + } + return(is_valid[fval[0]](fval[1])) + def validate_fields(rec): @@ -50,7 +52,14 @@ def validate_fields(rec): fvals = list(filter(lambda y: y[0] != 'cid', fvals)) res = sum([ validate(x) for x in fvals ]) == 7 - + return(res) - + sum([ validate_fields(rec) for rec in recs ]) + + + + + + + diff --git a/js/04.js b/js/04.js new file mode 100644 index 0000000..8485416 --- /dev/null +++ b/js/04.js @@ -0,0 +1,93 @@ +var fs = require("fs") + +Array.prototype.prepend = function() { + this.unshift(arguments["0"]) + return(this) +} + +Set.prototype.difference = function() { + return( + new Set([...this].filter(x => !arguments["0"].has(x))) + ) +} + +Number.prototype.between = function(a, b) { + min = Math.min.apply(Math, [a, b]), + max = Math.max.apply(Math, [a, b]); + return(this >= min && this <= max) +}; + +req_keys = new Set([ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid" ]) + +input = [] + +input = fs.readFileSync("../input/04-01.txt", "utf-8") +// input = fs.readFileSync("/tmp/test.txt", "utf-8") + .split("\n") + .prepend("") + .reduce((recs, line) => { + (line == "") ? recs.push([line]) : recs[recs.length - 1].push(line) + return(recs) + }, []) + .filter((x) => x.length > 1) + .map((x) => x.filter((val) => val != "") + .join(" ") + .split(" ") + .filter((x) => !RegExp('^cid').test(x)) + .reduce((kv, line) => { + [k, v] = line.split(":") + kv[k] = v + return(kv) + }, {}) + ) + +// 04-01 + +console.log( + input.map((x) => req_keys.difference(new Set(Object.keys(x)))) + .filter((x) => x.size == 0) + .length +) + +// 04-02 + +hair_colors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"] + +validate_height = function(x) { + + val = parseInt(x.replace(/(in|cm)/g, "")) + unit = x.replace(/[0-9]/g, "") + + return(unit == "in" ? val.between(59, 76) : val.between(150, 193)) + +} + +console.log( + input + .filter((x) => req_keys.difference(new Set(Object.keys(x))).size == 0) + .filter((x) => + Object.entries(x).filter((kv) => { + switch(kv[0]) { + case "byr" : res = RegExp('^[0-9]{4}$').test(kv[1]) && + parseInt(kv[1]).between(1920, 2002) + break + case "iyr" : res = RegExp('^[0-9]{4}$').test(kv[1]) && + parseInt(kv[1]).between(2010, 2020) + break + case "eyr" : res = RegExp('^[0-9]{4}$').test(kv[1]) && + parseInt(kv[1]).between(2020, 2030) + break + case "hgt" : res = RegExp('^[0-9]{2,3}(cm|in)$').test(kv[1]) && + validate_height(kv[1]) + break + case "hcl" : res = RegExp('^#[a-f0-9]{6}$').test(kv[1]) + break + case "ecl" : res = hair_colors.includes(kv[1]) + break + case "pid" : res = RegExp('^[0-9]{9}$').test(kv[1]) + break + } + return(res) + }).length == 7 + ).length +)