Add day 1-5

This commit is contained in:
2024-12-18 23:45:11 -07:00
commit ebd8d45970
61 changed files with 932 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
*/*/input.txt
View File
View File
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
data = f.readlines()
l = []
r = []
for line in data:
split = line.split(" ")
l.append(int(split[0]))
r.append(int(split[-1].strip()))
if len(l) != len(r):
raise Exception("lists must be the same length")
l_sort = sorted(l)
r_sort = sorted(r)
distance = 0
for i in range(len(l)):
print(f"l: {l_sort[i]}, r: {r_sort[i]}")
v = l_sort[i] - r_sort[i]
d = abs(v)
print(f"v: {v}, d: {d}")
distance = distance + d
print("")
print(distance)
+6
View File
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+30
View File
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
data = f.readlines()
l = []
r = []
for line in data:
split = line.split(" ")
l.append(int(split[0]))
r.append(int(split[-1].strip()))
if len(l) != len(r):
raise Exception("lists must be the same length")
total = 0
for e in l:
count = r.count(e)
result = e * count
print(f"count: {count}, result: {result}")
total += result
print("")
print(total)
+6
View File
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
filename = "example.txt"
filename = "input.txt"
def valid_deltas(report):
for x in range(len(report) - 1):
if not (1 <= abs(report[x] - report[x + 1]) <= 3):
return False
return True
def is_ascending(report):
x = [
report[i] < report[i + 1]
for i in range((len(report) - 1))
]
print(x)
return all(x)
def is_decending(report):
return is_ascending(list(reversed(report)))
reports = []
with open(filename) as f:
for line in f.readlines():
reports.append([int(x) for x in line.strip().split(" ")])
valid_reports = 0
for report in reports:
print(", ".join([str(x) for x in report]))
ascending = is_ascending(report)
decending = is_decending(report)
deltas = valid_deltas(report)
print(f"ascending: {ascending}")
print(f"decending: {decending}")
print(f"deltas: {deltas}")
valid = (ascending or decending) and deltas
print(f"valid: {valid}")
print()
print()
if valid:
valid_reports += 1
print(f"valid_reports: {valid_reports}")
+6
View File
@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+59
View File
@@ -0,0 +1,59 @@
#!/usr/bin/env python3
filename = "example.txt"
filename = "input.txt"
def print_report(report):
print(", ".join([str(x) for x in report]))
def valid_deltas(report):
for x in range(len(report) - 1):
if not (1 <= abs(report[x] - report[x + 1]) <= 3):
return False
return True
def is_ascending(report):
x = [
report[i] < report[i + 1]
for i in range((len(report) - 1))
]
print(x)
return all(x)
def is_decending(report):
return is_ascending(list(reversed(report)))
reports = []
with open(filename) as f:
for line in f.readlines():
reports.append([int(x) for x in line.strip().split(" ")])
valid_reports = 0
for report in reports:
ascending = is_ascending(report)
decending = is_decending(report)
deltas = valid_deltas(report)
valid = (ascending or decending) and deltas
if valid:
valid_reports += 1
continue
for i in range(len(report)):
x = report.copy()
x.pop(i)
ascending = is_ascending(x)
decending = is_decending(x)
deltas = valid_deltas(x)
valid = (ascending or decending) and deltas
if valid:
valid_reports += 1
break
print(f"valid_reports: {valid_reports}")
+6
View File
@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+61
View File
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
input = f.read().strip()
accumulator = 0
x = 0
i = 0
while True:
print("\n\n")
a = input[i:].find("mul(")
a_mod = a + i
b = input[a_mod:].find(",")
b_mod = b + a_mod
c = input[a_mod:].find(")")
c_mod = c + a_mod
if a == -1:
break
if b == -1:
break
if c == -1:
break
a = a_mod
b = b_mod
c = c_mod
print(input[i:])
print(input[a:])
print(input[b:])
print(input[c:])
print(a, b, c)
print(input[a+4:b])
print(input[b+1:c])
try:
val0 = int(input[a+4:b])
val1 = int(input[b+1:c])
except ValueError:
i = a + 4
#if x == 2:
# break
#x += 1
continue
print(f"{val0=}")
print(f"{val1=}")
accumulator += val0 * val1
i = c + 1
print(accumulator)
+1
View File
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+131
View File
@@ -0,0 +1,131 @@
#!/usr/bin/env python3
import dataclasses
import heapq
import re
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
input = f.read().strip()
mul_re = r"mul\((\d+),(\d+)\)"
do_re = r"do\(\)"
dont_re = r"don't\(\)"
@dataclasses.dataclass
class MulCmd():
l: int
r: int
@dataclasses.dataclass
class EnableCmd():
on: bool
commands = []
for mul in re.finditer(mul_re, input):
heapq.heappush(
commands,
(
mul.span(1)[0],
MulCmd(
l = int(input[mul.span(1)[0]:mul.span(1)[1]]),
r = int(input[mul.span(2)[0]:mul.span(2)[1]]),
),
)
)
for do in re.finditer(do_re, input):
heapq.heappush(
commands,
(
do.span(0)[0],
EnableCmd(on=True)
)
)
for dont in re.finditer(dont_re, input):
heapq.heappush(
commands,
(
dont.span(0)[0],
EnableCmd(on=False)
)
)
accumulator = 0
enable = True
while commands:
#for index, cmd in commands:
(index, cmd) = heapq.heappop(commands)
if isinstance(cmd, MulCmd):
print(f"mul: {cmd.l} * {cmd.r}")
if enable:
accumulator += cmd.l * cmd.r
elif isinstance(cmd, EnableCmd):
print(f"enable: {cmd.on}")
enable = cmd.on
print(accumulator)
#for mul in muls:
# print(input[mul.span(0)[0]:mul.span(0)[1]])
# print(input[mul.span(1)[0]:mul.span(1)[1]])
# print(input[mul.span(2)[0]:mul.span(2)[1]])
#print(mul.span(0))
#print(mul.span(1))
# accumulator += int(mul[0]) * int(mul[1])
#
#print(accumulator)
#i = 0
#while True:
#
#
#
# print("\n\n")
# a = input[i:].find("mul(")
# a_mod = a + i
# b = input[a_mod:].find(",")
# b_mod = b + a_mod
# c = input[a_mod:].find(")")
# c_mod = c + a_mod
# if a == -1:
# break
# if b == -1:
# break
# if c == -1:
# break
#
# a = a_mod
# b = b_mod
# c = c_mod
# print(input[i:])
# print(input[a:])
# print(input[b:])
# print(input[c:])
#
# print(a, b, c)
#
#
#
# print(input[a+4:b])
# print(input[b+1:c])
# try:
# val0 = int(input[a+4:b])
# val1 = int(input[b+1:c])
# except ValueError:
# i = a + 4
# continue
# print(f"{val0=}")
# print(f"{val1=}")
#
# accumulator += val0 * val1
#
# i = c + 1
#
#print(accumulator)
+1
View File
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+124
View File
@@ -0,0 +1,124 @@
#!/usr/bin/env python3
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
input = f.readlines()
board = [
[letter for letter in line.strip()]
for line in input
]
y_dim = len(board)
x_dim = len(board[0])
valids = []
do_print = False
def print_board(b, indexes_to_print=[]):
if not do_print:
return
for y in range(y_dim):
for x in range(x_dim):
if indexes_to_print:
if (y, x) in indexes_to_print:
print(b[y][x], end="")
else:
print(".", end="")
else:
print(b[y][x], end="")
print()
print()
print_board(board)
def is_xmas(*args):
a = board[args[0][0]][args[0][1]]
b = board[args[1][0]][args[1][1]]
c = board[args[2][0]][args[2][1]]
d = board[args[3][0]][args[3][1]]
if f"{a}{b}{c}{d}" == "XMAS":
print_board(board, args)
valids.append(args)
return True
return False
count = 0
for y in range(y_dim):
for x in range(x_dim):
# left (backwards)
if x > 2:
count = count + is_xmas(
(y, x),
(y, x - 1),
(y, x - 2),
(y, x - 3),
)
# right
if x_dim - x > 3:
count = count + is_xmas(
(y, x),
(y, x + 1),
(y, x + 2),
(y, x + 3),
)
# up
if y > 2:
count = count + is_xmas(
(y, x),
(y - 1, x),
(y - 2, x),
(y - 3, x),
)
# down
if y_dim - y > 3:
count = count + is_xmas(
(y, x),
(y + 1, x),
(y + 2, x),
(y + 3, x),
)
# diagonal up left
if x > 2 and y > 2:
count = count + is_xmas(
(y, x),
(y - 1, x - 1),
(y - 2, x - 2),
(y - 3, x - 3),
)
# diagonal up right
if x_dim - x > 3 and y > 2:
count = count + is_xmas(
(y, x),
(y - 1, x + 1),
(y - 2, x + 2),
(y - 3, x + 3),
)
# diagonal down left
if x > 2 and y_dim - y > 3:
count = count + is_xmas(
(y, x),
(y + 1, x - 1),
(y + 2, x - 2),
(y + 3, x - 3),
)
# diagonal down right
if x_dim - x > 3 and y_dim - y > 3:
count = count + is_xmas(
(y, x),
(y + 1, x + 1),
(y + 2, x + 2),
(y + 3, x + 3),
)
print_board(board, [x for xs in valids for x in xs])
print(count)
+10
View File
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+87
View File
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
input = f.readlines()
board = [
[letter for letter in line.strip()]
for line in input
]
y_dim = len(board)
x_dim = len(board[0])
valids = []
do_print = False
def print_board(b, indexes_to_print=[]):
if not do_print:
return
for y in range(y_dim):
for x in range(x_dim):
if indexes_to_print:
if (y, x) in indexes_to_print:
print(b[y][x], end="")
else:
print(".", end="")
else:
print(b[y][x], end="")
print()
print()
print_board(board)
def is_x_mas(center):
vals = ["L", "M", "N", "O"]
vals[0] = board[center[0] - 1][center[1] - 1]
vals[1] = board[center[0] - 1][center[1] + 1]
vals[2] = board[center[0] + 1][center[1] - 1]
vals[3] = board[center[0] + 1][center[1] + 1]
vals = "".join(vals)
x_mases = [
"MMSS",
"MSMS",
"SMSM",
"SSMM",
]
print(f"{center}: {vals}")
if vals in x_mases:
poses = [
center,
(center[0] - 1, center[1] - 1),
(center[0] - 1, center[1] + 1),
(center[0] + 1, center[1] - 1),
(center[0] + 1, center[1] + 1),
]
print_board(
board,
poses
)
valids.append(poses)
return True
return False
count = 0
for y in range(1, y_dim - 1):
for x in range(1, x_dim - 1):
if board[y][x] != "A":
continue
count = count + is_x_mas((y, x))
print_board(board, [x for xs in valids for x in xs])
print(count)
+10
View File
@@ -0,0 +1,10 @@
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
import collections
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
input = f.readlines()
input = [line.strip() for line in input]
rs = [
line.split("|") for line in input if "|" in line
]
rules = collections.defaultdict(list)
for r in rs:
rules[r[0]].append(r[1])
print(rules)
print_orders = [
line.split(",") for line in input if "|" not in line and len(line) > 0
]
count = 0
for print_order in print_orders:
bad = False
for i, x in enumerate(print_order):
for v in rules[x]:
if v in print_order:
if print_order.index(v) <= i:
bad = True
if bad:
print(f"{print_order} is bad")
continue
print(f"{print_order} is good")
count = count + int(print_order[len(print_order) // 2])
print(count)
+28
View File
@@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File
View File
View File
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
import collections
filename = "example.txt"
filename = "input.txt"
with open(filename) as f:
input = f.readlines()
input = [line.strip() for line in input]
rs = [
line.split("|") for line in input if "|" in line
]
rules = collections.defaultdict(list)
for r in rs:
rules[r[0]].append(r[1])
print(rules)
print_orders = [
line.split(",") for line in input if "|" not in line and len(line) > 0
]
count = 0
def validate_rule(order):
for i, x in enumerate(print_order):
for v in rules[x]:
if v in print_order:
if print_order.index(v) <= i:
return False
return True
def fix_rule(order):
to_fix = []
for i, x in enumerate(print_order):
for v in rules[x]:
if v in print_order:
if print_order.index(v) <= i:
order.insert(print_order.index(v), x)
order.pop(i + 1)
return order
for print_order in print_orders:
if not validate_rule(print_order):
print(f"{print_order} is bad")
#count = count + fix_rule(print_order)
while True:
new = fix_rule(print_order)
if validate_rule(new):
print_order = new
break
else:
continue
print(f"{print_order} is good")
count = count + int(print_order[len(print_order) // 2])
print(count)
+28
View File
@@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47
+14
View File
@@ -0,0 +1,14 @@
[tool.poetry]
name = "aoc"
version = "0.1.0"
description = ""
authors = ["restitux <restitux@ohea.xyz>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
View File