59 lines
1.1 KiB
Python
59 lines
1.1 KiB
Python
|
day = "day2"
|
||
|
file = f"inputs/{day}.txt"
|
||
|
|
||
|
|
||
|
|
||
|
def part_one(file):
|
||
|
result = 0
|
||
|
input = open(file, "r")
|
||
|
input = input.readlines()
|
||
|
|
||
|
for line in input:
|
||
|
if(line_check(line)):
|
||
|
result += 1
|
||
|
|
||
|
|
||
|
print(f"Part 1: {result}")
|
||
|
|
||
|
|
||
|
def line_check(line):
|
||
|
|
||
|
string = line.split(' ')[2]
|
||
|
letter = line.split(" ")[1][0]
|
||
|
start = int(line.split(" ")[0].split("-")[0])
|
||
|
end = int(line.split(" ")[0].split("-")[1])
|
||
|
|
||
|
if(string.count(letter) >= start and string.count(letter) <= end):
|
||
|
return True
|
||
|
|
||
|
|
||
|
part_one(file)
|
||
|
|
||
|
|
||
|
def part_two(file):
|
||
|
result = 0
|
||
|
input = open(file, "r")
|
||
|
input = input.readlines()
|
||
|
|
||
|
for line in input:
|
||
|
if(line_check_part2(line)):
|
||
|
result += 1
|
||
|
|
||
|
print(f"Part 2: {result}")
|
||
|
|
||
|
|
||
|
def line_check_part2(line):
|
||
|
|
||
|
string = line.split(' ')[2]
|
||
|
letter = line.split(" ")[1][0]
|
||
|
start = int(line.split(" ")[0].split("-")[0])
|
||
|
end = int(line.split(" ")[0].split("-")[1])
|
||
|
|
||
|
if(string[start-1] == letter and string[end-1] == letter):
|
||
|
return False
|
||
|
elif(string[start-1] == letter or string[end-1] == letter):
|
||
|
return True
|
||
|
|
||
|
|
||
|
|
||
|
part_two(file)
|