Compare commits

..

7 Commits

Author SHA1 Message Date
cf5207cca3 Skeleton for day 4 2024-12-04 12:49:04 -04:00
4396cd68fc Add some clarifying comments 2024-12-03 15:33:24 -04:00
1f22b34ae0 Remove unneeded regex options 2024-12-03 15:03:24 -04:00
e97762a3f7 Add one liner 2024-12-03 15:01:48 -04:00
fa259faa2b Remove unneeded continue logic 2024-12-03 14:58:13 -04:00
bb31161c80 No need for defs 2024-12-03 14:56:29 -04:00
cb4f3d26d0 Inline some vars 2024-12-03 14:54:54 -04:00
4 changed files with 35 additions and 41 deletions

View File

@ -1,21 +1,20 @@
from aocd import get_data from aocd import get_data
data = get_data(day=1, year=2024) data = get_data(day=1, year=2024)
# Condition data
list1 = [] list1 = []
list2 = [] list2 = []
lines = data.split("\n") lines = data.split("\n")
for line in lines: for line in lines:
nums = line.split(" ") nums = line.split(" ")
num1 = int(nums[0]) num1 = int(nums[0])
num2 = int(nums[1]) num2 = int(nums[1])
list1.append(num1) list1.append(num1)
list2.append(num2) list2.append(num2)
list1.sort() list1.sort()
list2.sort() list2.sort()
# for i in range(len(list1)): # Part 1
# print(list1[i], list2[i])
total_dist = 0 total_dist = 0
for i in range(len(list1)): for i in range(len(list1)):
dist = list2[i] - list1[i] dist = list2[i] - list1[i]
@ -24,6 +23,7 @@ for i in range(len(list1)):
print("Total distance: ", total_dist) print("Total distance: ", total_dist)
# Part 2
total_similarity = 0 total_similarity = 0
for i in range(len(list1)): for i in range(len(list1)):
num = list1[i] num = list1[i]

View File

@ -1,5 +1,7 @@
from aocd import get_data from aocd import get_data
data = get_data(day=2, year=2024) data = get_data(day=2, year=2024)
# Condition data
lines = data.split("\n") lines = data.split("\n")
reports = [] reports = []
for line in lines: for line in lines:
@ -10,6 +12,7 @@ for line in lines:
total_safe = 0 total_safe = 0
safe_nums = [1, 2, 3] safe_nums = [1, 2, 3]
# Part 1
for report in reports: for report in reports:
dists = [report[i+1] - report[i] for i in range(len(report)) if i != len(report) - 1] dists = [report[i+1] - report[i] for i in range(len(report)) if i != len(report) - 1]
# dists is either all positive or all negative (checks all increasing or all decreasing) # dists is either all positive or all negative (checks all increasing or all decreasing)
@ -20,6 +23,7 @@ for report in reports:
print("Total safe reports: ", total_safe) print("Total safe reports: ", total_safe)
# Part 2
total_safe_tolerance = 0 total_safe_tolerance = 0
for report in reports: for report in reports:
print("Checking report: ", report) print("Checking report: ", report)

View File

@ -3,38 +3,25 @@ import re
data = get_data(day=3, year=2024) data = get_data(day=3, year=2024)
def part_one(): # Part one
regex = r"mul\((\d+),(\d+)\)" matches = re.findall(r"mul\((\d+),(\d+)\)", data)
matches = re.findall(regex, data, re.MULTILINE) total = 0
for match in matches:
total += int(match[0]) * int(match[1])
print(total)
total = 0 # One-liner
for match in matches: # print(sum(int(x) * int(y) for x, y in re.findall(r"mul\((\d+),(\d+)\)", data)))
total += int(match[0]) * int(match[1])
print(total)
def part_two(): # Part two
mul_regex = r"mul\((\d+),(\d+)\)" matches = re.finditer(r"mul\((\d+),(\d+)\)", data)
do_str = "do()" total = 0
dont_str = "don't()" for match in matches:
start = match.start()
matches = re.finditer(mul_regex, data, re.MULTILINE) do_match = data.rfind("do()", 0, start)
dont_match = data.rfind("don't()", 0, start)
total = 0 if do_match == dont_match == -1: # Neither
for match in matches: total += int(match.group(1)) * int(match.group(2))
start = match.start() elif do_match > dont_match: # Do
do_match = data.rfind(do_str, 0, start) total += int(match.group(1)) * int(match.group(2))
dont_match = data.rfind(dont_str, 0, start) print(total)
if do_match == dont_match == -1: # Neither
total += int(match.group(1)) * int(match.group(2))
continue
if do_match > dont_match: # Do
total += int(match.group(1)) * int(match.group(2))
continue
else: # Don't
continue
print(total)
part_one()
part_two()

3
2024/04/solution.py Normal file
View File

@ -0,0 +1,3 @@
from aocd import get_data
data = get_data(day=4, year=2024)