Add day 1-5
This commit is contained in:
Executable
+131
@@ -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)
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|
||||
@@ -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"
|
||||
Reference in New Issue
Block a user