Add Python day 1, 2, and 3
parent
993c9af394
commit
f4a6aae550
|
@ -0,0 +1,6 @@
|
||||||
|
import src.day_01.part_1
|
||||||
|
import src.day_01.part_2
|
||||||
|
import src.day_02.part_1
|
||||||
|
import src.day_02.part_2
|
||||||
|
import src.day_03.part_1
|
||||||
|
import src.day_03.part_2
|
|
@ -0,0 +1,14 @@
|
||||||
|
lines = [ line.strip().split(" ") for line in open('inputs/01.txt') ]
|
||||||
|
|
||||||
|
left = [ int(line[0]) for line in lines ]
|
||||||
|
right = [ int(line[1]) for line in lines ]
|
||||||
|
|
||||||
|
left.sort()
|
||||||
|
right.sort()
|
||||||
|
|
||||||
|
s = 0
|
||||||
|
|
||||||
|
for l, r in zip(left, right):
|
||||||
|
s += abs(l - r)
|
||||||
|
|
||||||
|
print(f"Day 01 part 1: {s}")
|
|
@ -0,0 +1,24 @@
|
||||||
|
lines = [ line.strip().split(" ") for line in open('inputs/01.txt') ]
|
||||||
|
|
||||||
|
left = [ int(line[0]) for line in lines ]
|
||||||
|
right = [ int(line[1]) for line in lines ]
|
||||||
|
|
||||||
|
left.sort()
|
||||||
|
right.sort()
|
||||||
|
|
||||||
|
s = 0
|
||||||
|
|
||||||
|
i, j = 0, 0
|
||||||
|
|
||||||
|
while i < len(left) and j < len(right):
|
||||||
|
l, r = left[i], right[j]
|
||||||
|
|
||||||
|
if l < r:
|
||||||
|
i += 1
|
||||||
|
elif l > r:
|
||||||
|
j += 1
|
||||||
|
else:
|
||||||
|
j += 1
|
||||||
|
s += l
|
||||||
|
|
||||||
|
print(f"Day 01 part 2: {s}")
|
|
@ -0,0 +1,23 @@
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
lines = [ [ int(l) for l in line.strip().split(" ") ] for line in open('inputs/02.txt') ]
|
||||||
|
|
||||||
|
s = 0
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
increasing, decreasing, max_diff = 0, 0, 0
|
||||||
|
|
||||||
|
for a, b in itertools.pairwise(line):
|
||||||
|
max_diff = max(max_diff, abs(a - b))
|
||||||
|
|
||||||
|
if a < b:
|
||||||
|
increasing += 1
|
||||||
|
elif a > b:
|
||||||
|
decreasing += 1
|
||||||
|
|
||||||
|
single_direction = (increasing * decreasing == 0) and (increasing + decreasing == len(line) - 1)
|
||||||
|
|
||||||
|
if single_direction and max_diff <= 3:
|
||||||
|
s += 1
|
||||||
|
|
||||||
|
print(f"Day 02 part 1: {s}")
|
|
@ -0,0 +1,32 @@
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
def is_safe_line(line) -> bool:
|
||||||
|
increasing, decreasing, max_diff = 0, 0, 0
|
||||||
|
|
||||||
|
for a, b in itertools.pairwise(line):
|
||||||
|
max_diff = max(max_diff, abs(a - b))
|
||||||
|
|
||||||
|
if a < b:
|
||||||
|
increasing += 1
|
||||||
|
elif a > b:
|
||||||
|
decreasing += 1
|
||||||
|
|
||||||
|
single_direction = (increasing * decreasing == 0) and (increasing + decreasing == len(line) - 1)
|
||||||
|
|
||||||
|
return single_direction and max_diff <= 3
|
||||||
|
|
||||||
|
|
||||||
|
lines = [ [ int(l) for l in line.strip().split(" ") ] for line in open('inputs/02.txt') ]
|
||||||
|
|
||||||
|
s = 0
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
if is_safe_line(line):
|
||||||
|
s += 1
|
||||||
|
else:
|
||||||
|
for i in range(len(line)):
|
||||||
|
if is_safe_line(line[:i] + line[i+1:]):
|
||||||
|
s += 1
|
||||||
|
break
|
||||||
|
|
||||||
|
print(f"Day 02 part 2: {s}")
|
|
@ -0,0 +1,9 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
lines = "".join(line for line in open('inputs/03.txt'))
|
||||||
|
total = 0
|
||||||
|
|
||||||
|
for a, b in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)", lines):
|
||||||
|
total += int(a) * int(b)
|
||||||
|
|
||||||
|
print(f"Day 03 part 1: {total}")
|
|
@ -0,0 +1,24 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
def find_muls(text : str) -> int:
|
||||||
|
tot = 0
|
||||||
|
for a, b in re.findall(r"mul\((\d{1,3}),(\d{1,3})\)", text):
|
||||||
|
tot += int(a) * int(b)
|
||||||
|
return tot
|
||||||
|
|
||||||
|
lines = "".join(line for line in open('inputs/03.txt'))
|
||||||
|
total = 0
|
||||||
|
|
||||||
|
if "don't()" in lines:
|
||||||
|
parts = lines.split("don't()")
|
||||||
|
|
||||||
|
# By default, first is valid
|
||||||
|
total += find_muls(parts[0])
|
||||||
|
|
||||||
|
for part in parts[1:]:
|
||||||
|
for valid_part in part.split("do()")[1:]:
|
||||||
|
total += find_muls(valid_part)
|
||||||
|
else:
|
||||||
|
total = find_muls(lines)
|
||||||
|
|
||||||
|
print(f"Day 03 part 2: {total}")
|
Loading…
Reference in New Issue