Skip to content

Commit

Permalink
old work on 2023, and added new solution for d02 of 2020 because why …
Browse files Browse the repository at this point in the history
…be consistent.
  • Loading branch information
aws-donco committed Oct 15, 2024
1 parent 53fad1b commit cdab670
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 7 deletions.
119 changes: 119 additions & 0 deletions day05.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 26 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions problem_inputs/day06.py
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions y2020_d02.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit cdab670

Please sign in to comment.