From 9367e34005ab3da86ea5d53f3dceb8be5527da9f Mon Sep 17 00:00:00 2001 From: rav4s Date: Fri, 4 Dec 2020 09:28:31 -0600 Subject: [PATCH] Added solution to day 4 part 1 --- day4part1.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 day4part1.py diff --git a/day4part1.py b/day4part1.py new file mode 100644 index 0000000..6fb6ef5 --- /dev/null +++ b/day4part1.py @@ -0,0 +1,51 @@ +import re + +def file_to_string(file_path): + f = open(file_path, "r") + string = f.read() + f.close() + return string + +def split_on_empty_lines(string): + return re.split(r"(?:\r?\n){2,}", string.strip()) + +def remove_newlines(splitted_string): + newlist = [] + for i in splitted_string: + i = i.replace('\r', '').replace('\n', '') + newlist.append(i) + return newlist + +def compare_j_value(j): + if j == 7: + return "Valid" + else: + return "Invalid" + +def find_attributes(newlines_removed): + newlist = [] + for i in newlines_removed: + j = 0 + if "byr:" in i: + j = j + 1 + if "iyr:" in i: + j = j + 1 + if "eyr:" in i: + j = j + 1 + if "hgt:" in i: + j = j + 1 + if "hcl:" in i: + j = j + 1 + if "ecl:" in i: + j = j + 1 + if "pid:" in i: + j = j + 1 + newlist.append(compare_j_value(j)) + return newlist + +string = file_to_string("day4input.txt") +splitted_string = split_on_empty_lines(string) +newlines_removed = remove_newlines(splitted_string) +list_of_valids_invalids = find_attributes(newlines_removed) +print(list_of_valids_invalids) +print(list_of_valids_invalids.count("Valid"))