Compare commits
7 Commits
f92209c366
...
master
Author | SHA1 | Date | |
---|---|---|---|
cf5207cca3 | |||
4396cd68fc | |||
1f22b34ae0 | |||
e97762a3f7 | |||
fa259faa2b | |||
bb31161c80 | |||
cb4f3d26d0 |
@ -1,6 +1,7 @@
|
|||||||
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")
|
||||||
@ -13,9 +14,7 @@ for line in lines:
|
|||||||
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]
|
||||||
|
@ -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)
|
||||||
|
@ -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
|
total = 0
|
||||||
for match in matches:
|
for match in matches:
|
||||||
total += int(match[0]) * int(match[1])
|
total += int(match[0]) * int(match[1])
|
||||||
print(total)
|
print(total)
|
||||||
|
|
||||||
def part_two():
|
# One-liner
|
||||||
mul_regex = r"mul\((\d+),(\d+)\)"
|
# print(sum(int(x) * int(y) for x, y in re.findall(r"mul\((\d+),(\d+)\)", data)))
|
||||||
do_str = "do()"
|
|
||||||
dont_str = "don't()"
|
|
||||||
|
|
||||||
matches = re.finditer(mul_regex, data, re.MULTILINE)
|
|
||||||
|
|
||||||
|
# Part two
|
||||||
|
matches = re.finditer(r"mul\((\d+),(\d+)\)", data)
|
||||||
total = 0
|
total = 0
|
||||||
for match in matches:
|
for match in matches:
|
||||||
start = match.start()
|
start = match.start()
|
||||||
do_match = data.rfind(do_str, 0, start)
|
do_match = data.rfind("do()", 0, start)
|
||||||
dont_match = data.rfind(dont_str, 0, start)
|
dont_match = data.rfind("don't()", 0, start)
|
||||||
|
|
||||||
if do_match == dont_match == -1: # Neither
|
if do_match == dont_match == -1: # Neither
|
||||||
total += int(match.group(1)) * int(match.group(2))
|
total += int(match.group(1)) * int(match.group(2))
|
||||||
continue
|
elif do_match > dont_match: # Do
|
||||||
if do_match > dont_match: # Do
|
|
||||||
total += int(match.group(1)) * int(match.group(2))
|
total += int(match.group(1)) * int(match.group(2))
|
||||||
continue
|
|
||||||
else: # Don't
|
|
||||||
continue
|
|
||||||
|
|
||||||
print(total)
|
print(total)
|
||||||
|
|
||||||
part_one()
|
|
||||||
part_two()
|
|
3
2024/04/solution.py
Normal file
3
2024/04/solution.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from aocd import get_data
|
||||||
|
data = get_data(day=4, year=2024)
|
||||||
|
|
Reference in New Issue
Block a user