diff --git a/day05.py b/day05.py new file mode 100644 index 0000000..a0b3939 --- /dev/null +++ b/day05.py @@ -0,0 +1,119 @@ +import re + + +class FarmMaps: + def __init__(self): + self.s2s = [] + self.s2f = [] + self.f2w = [] + self.w2l = [] + self.l2t = [] + self.t2h = [] + self.h2l = [] + + +class Seeds: + def __init__(self): + self.seeds = [] + + +def parse_input(input_list): + seeds = Seeds() + my_farm = FarmMaps() + + map_type = None + + for line in input_list: + s2s_map = line.strip().split(' ') + if re.match(r'seeds: ', line): + seeds.seeds = [int(seed) for seed in line.strip().split() if seed.isnumeric()] + # elif line and re.match(r'\d+ \d+ \d+', line): + # s2s_map = list(map(int, s2s_map)) + + if re.match(r'seed-to-soil map:', line): + map_type = 'seed-to-soil map:' + if re.match(r'soil-to-fertilizer map:', line): + map_type = 'soil-to-fertilizer map:' + if re.match(r'fertilizer-to-water map:', line): + map_type = 'fertilizer-to-water map:' + if re.match(r'water-to-light map:', line): + map_type = 'water-to-light map:' + if re.match(r'light-to-temperature map:', line): + map_type = 'light-to-temperature map:' + if re.match(r'temperature-to-humidity map:', line): + map_type = 'temperature-to-humidity map:' + if re.match(r'humidity-to-location map:', line): + map_type = 'humidity-to-location map:' + + if line and re.match(r'\d+ \d+ \d+', line): + s2s_map = list(map(int, s2s_map)) + map_range = (s2s_map[1], s2s_map[0], s2s_map[1] + s2s_map[2], s2s_map[0] + s2s_map[2]) + + if map_type == 'seed-to-soil map:': + my_farm.s2s.append(map_range) + if map_type == 'soil-to-fertilizer map:': + my_farm.s2f.append(map_range) + if map_type == 'fertilizer-to-water map:': + my_farm.f2w.append(map_range) + if map_type == 'water-to-light map:': + my_farm.w2l.append(map_range) + if map_type == 'light-to-temperature map:': + my_farm.l2t.append(map_range) + if map_type == 'temperature-to-humidity map:': + my_farm.t2h.append(map_range) + if map_type == 'humidity-to-location map:': + my_farm.h2l.append(map_range) + + return seeds, my_farm + + +def part01(input_lines): + (seeds, farm_map) = parse_input(input_lines) + lowest_location = -1 + for seed in seeds.seeds: + for s2s in farm_map.s2s: + if s2s[0] <= seed <= s2s[2]: + soil = s2s[1] + (seed - s2s[0]) + break + soil = seed + for s2f in farm_map.s2f: + if s2f[0] <= soil <= s2f[2]: + fertilizer = s2f[1] + (soil - s2f[0]) + break + fertilizer = soil + for f2w in farm_map.f2w: + if f2w[0] <= fertilizer <= f2w[2]: + water = f2w[1] + (fertilizer - s2f[0]) + break + water = fertilizer + for w2l in farm_map.w2l: + if w2l[0] <= water <= w2l[2]: + light = w2l[1] + (water - w2l[0]) + break + light = water + for l2t in farm_map.l2t: + if l2t[0] <= light <= l2t[2]: + temperature = l2t[1] + (light - l2t[0]) + break + temperature = light + for t2h in farm_map.t2h: + if t2h[0] <= temperature <= t2h[2]: + humidity = t2h[1] + (temperature - t2h[0]) + break + humidity = temperature + for h2l in farm_map.h2l: + if h2l[0] <= humidity <= h2l[2]: + location = h2l[1] + (humidity - h2l[0]) + break + location = humidity + + if lowest_location == -1: + lowest_location = location + elif location < lowest_location: + lowest_location = location + + print(lowest_location) + + +def part02(input_lines): + pass diff --git a/main.py b/main.py index c3f9e27..9e87c26 100644 --- a/main.py +++ b/main.py @@ -2,19 +2,38 @@ import day02 import day03 import day04 +import day05 + +from y2020_d02 import Y2020D02 -if __name__ == '__main__': - today = '04' - input_file = f'problem_inputs/day{today}_input.txt' - # input_file = f'problem_inputs/test_input.txt' +if __name__ == '__main__': + today = '2020' + input_file = f'problem_inputs/aoc_2020_02_input.txt' with open(input_file) as day_fp: day_input = day_fp.readlines() day_input = [f'{i}'.strip() for i in day_input] - if today == '05': - day05.part01(day_input) - day05.part02(day_input) + + aoc2020d2 = Y2020D02(day_input) + aoc2020d2.day01() + aoc2020d2.day02() + + + if today == '05' or today == '06': + # input_file = f'problem_inputs/day{today}_input.txt' + input_file = f'problem_inputs/test_input.txt' + + with open(input_file) as day_fp: + day_input = day_fp.readlines() + day_input = [f'{i}'.strip() for i in day_input] + + if today == '05': + day05.part01(day_input) + day05.part02(day_input) + if today == '06': + day05.part01(day_input) + day05.part02(day_input) if today == '04': input_file = f'problem_inputs/day{today}_input.txt' diff --git a/problem_inputs/day06.py b/problem_inputs/day06.py new file mode 100644 index 0000000..d20165d --- /dev/null +++ b/problem_inputs/day06.py @@ -0,0 +1,10 @@ +import re + + +def part01(speed_input): + times = re.split(r'\s+', speed_input[0])[1:] + distances = re.split(r'\s+', speed_input[1])[1:] + + for race in len(times): + for poweruptime in (range(1, times[race])): + time to travel = times[race] - poweruptime diff --git a/y2020_d02.py b/y2020_d02.py new file mode 100644 index 0000000..269b2c3 --- /dev/null +++ b/y2020_d02.py @@ -0,0 +1,37 @@ + +class Y2020D02: + pwd_input = [] + + def __init__(self, input_list): + for i in input_list: + (rules, pwd) = i.split(":") + (minmax, ltr) = rules.split(" ") + (min_ltr, max_ltr) = minmax.split("-") + + self.pwd_input.append((int(min_ltr), int(max_ltr), ltr.strip(), pwd.strip(), i)) + + def day01(self): + correct_passwords = 0 + for potential_word in self.pwd_input: + filtered_word_count = potential_word[3].count(potential_word[2]) + if potential_word[0] <= filtered_word_count <= potential_word[1]: + correct_passwords += 1 + + print(correct_passwords) + + def day02(self): + correct_passwords = 0 + pos1 = False + pos2 = False + for pword in self.pwd_input: + if pword[3][pword[0]-1] == pword[2]: + pos1 = True + if pword[3][pword[1]-1] == pword[2]: + pos2 = True + if pos1 ^ pos2: + correct_passwords += 1 + print(pword[0], pword[1], "s:", pword[3][pword[0]-1], "e:", pword[3][pword[1]-1], "ltr:", pword[2], pword[3]) + pos1 = False + pos2 = False + + print(correct_passwords)