Compare commits

..

4 Commits

Author SHA1 Message Date
PedroEdiaz
ca626276ef Minor changes 2025-12-01 21:43:05 -06:00
PedroEdiaz
915065d2e6 2025: 01 2025-12-01 16:55:48 -06:00
PedroEdiaz
62bdc3c593 Opt: 01, 17 2025-10-31 15:57:08 -06:00
PedroEdiaz
0b4bd32e3e Add: 07->08 2025-10-31 15:56:51 -06:00
8 changed files with 333 additions and 20 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
**.txt

122
2015/07.py Normal file
View File

@@ -0,0 +1,122 @@
import re
# Fill Input
try:
with open("07.txt", 'r', encoding='utf-8') as file:
input = file.read()
except Exception as e:
print(f"An error occurred: {e}")
# Result
def is_number(s:str):
return ord('0')<= ord(s[0]) and ord(s[0]) <=ord('9')
dict = {}
for line in input.split('\n'):
if re_match := re.search(r"([a-z]+|[0-9]+) ([A-Z]+) ([a-z]+|[0-9]+) -> ([a-z]+)", line):
v1 = re_match.group(1)
op = re_match.group(2)
v2 = re_match.group(3)
rs = re_match.group(4)
dict[rs] = [op,v1,v2]
continue
if re_match := re.search(r"(NOT) ([a-z]+) -> ([a-z]+)", line):
op = re_match.group(1)
v1 = re_match.group(2)
rs = re_match.group(3)
dict[rs] = [op,v1,None]
continue
if re_match := re.search(r"([a-z]+|[0-9]+) -> ([a-z]+)", line):
v1 = re_match.group(1)
rs = re_match.group(2)
dict[rs] = [None,v1,None]
continue
# Part B
#dict["b"]=[None,..., None]
to_dict = {}
while len(dict):
for key in dict:
v = dict[key]
if not v[0] and (is_number(v[1]) or v[1] in to_dict):
to_dict[key] = v
if (is_number(v[1]) or v[1] in to_dict ) and (not v[2] or is_number(v[2]) or v[2] in to_dict):
to_dict[key] = v
for key in to_dict:
if key in dict:
dict.pop(key)
for key in to_dict:
v = to_dict[key]
if not v[0] and is_number(v[1]):
to_dict[key] = int(v[1])
continue
if not v[0] and v[1] in to_dict:
to_dict[key] = to_dict[v[1]]
continue
if v[0] == "NOT":
to_dict[key] = ~to_dict[v[1]]
continue
if (v[1] in to_dict) and is_number(v[2]):
match v[0]:
case "LSHIFT":
to_dict[key] = to_dict[v[1]] << int(v[2])
continue
case "RSHIFT":
to_dict[key] = to_dict[v[1]] >> int(v[2])
continue
case "AND":
to_dict[key] = to_dict[v[1]] & int(v[2])
continue
case "OR":
to_dict[key] = to_dict[v[1]] | int(v[2])
continue
if is_number(v[1]) and (v[2] in to_dict):
match v[0]:
case "LSHIFT":
to_dict[key] = int(v[1]) << to_dict[v[2]]
continue
case "RSHIFT":
to_dict[key] = int(v[1]) >> to_dict[v[2]]
continue
case "AND":
to_dict[key] = int(v[1]) & to_dict[v[2]]
continue
case "OR":
to_dict[key] = int(v[1]) | to_dict[v[2]]
continue
if (v[1] in to_dict) and (v[2] in to_dict):
match v[0]:
case "LSHIFT":
to_dict[key] = to_dict[v[1]] << to_dict[v[2]]
continue
case "RSHIFT":
to_dict[key] = to_dict[v[1]] >> to_dict[v[2]]
continue
case "AND":
to_dict[key] = to_dict[v[1]] & to_dict[v[2]]
continue
case "OR":
to_dict[key] = to_dict[v[1]] | to_dict[v[2]]
continue
print(to_dict["a"])

35
2015/08.py Normal file
View File

@@ -0,0 +1,35 @@
# Fill Input
try:
with open("08.txt", 'r', encoding='utf-8') as file:
input = file.read()
except Exception as e:
print(f"An error occurred: {e}")
# Result
res1, res2 = 0, 0
for line in input.split('\n'):
#for line in ["\"\"", "\"abc\"", "\"aaa\\\"aaa\"", "\"\\x27\""]:
#res2 = 0
it = iter(line)
while c:=next(it, None):
match c:
case '\"':
res1+=1
res2+= 1
pass
case '\\':
res1+=1
res2+=1
match c:= next(it):
case 'x':
next(it)
next(it)
res1+=2
case '\"':
res2+=1
case _:
pass
res2+=2
print(res1, res2+len(line))
print(res1, res2)

58
2015/09.py Normal file
View File

@@ -0,0 +1,58 @@
import re
# Fill Input
try:
with open("09.txt", 'r', encoding='utf-8') as file:
input = file.read()
except Exception as e:
print(f"An error occurred: {e}")
# Result
def perm(X, visited = []):
if len(visited) == len(X):
return [visited]
res = []
for a in X:
if a not in visited:
for i in perm(X, [*visited, a]):
res.append(i)
return res;
path = {}
for line in input.split('\n'):
if not (re_match := re.search(r"(\w+) to (\w+) = (\d+)", line)):
break
f = re_match.group(1)
t = re_match.group(2)
d = int(re_match.group(3))
if f in path:
path[f][t] = d
else:
path[f] = {t:d}
if t in path:
path[t][f] = d
else:
path[t] = {f:d}
res = []
for p in perm(list(path)):
sum = 0;
last = None
for c in p:
if not last:
last = c
continue
sum += path[last][c]
last = c
res.append(sum)
print(min(res), max(res))

View File

@@ -7,36 +7,41 @@ except Exception as e:
print(f"An error occurred: {e}") print(f"An error occurred: {e}")
# Reverse sort input # Reverse sort input
input= [ int(x) for x in input.split()]
l = len(input)
res1, res2, min_popcount = 0, 0, l
for i in range(1<<l):
""" """
Let: Let:
f: i -> [input[j] for j in range(len(X)) if i & 1<<j ] f: i -> X[j] for j in range(len(X)) if i & 1<<j ]
Then: Then:
f is bijection between range(2**len(X)) and the subsets of X. f is bijection between range(2**len(X)) and the P(X).
Futhermore: Futhermore:
popcount(i) == len(f(i)) popcount(i) == len(f(i))
""" """
def powerset(X):
return [[ X[j] for j in range(len(X)) if i & 1<<j ] for i in range(1<<len(X))]
if sum([input[j] for j in range(l) if i & 1<<j ]) != 150:
input= [ int(x) for x in input.split()]
res1, res2, min_l = 0, 0, len(input)
for s in powerset(input):
if sum(s) != 150:
continue continue
res1 += 1 res1 += 1
popcount = i.bit_count() l = len(s)
if popcount < min_popcount:
min_popcount = popcount if l < min_l:
min_l = l
res2 = 0; res2 = 0;
if popcount == min_popcount: if l == min_l:
res2 += 1; res2 += 1;
print(res1, res2) print(res1, res2)
# One Liner # One Liner
print( sum([ sum( [input[j] for j in range(l) if i & 1<<j ]) == 150 for i in range(1<<l)] )) #print( sum([ sum( [input[j] for j in range(len(input)) if i & 1<<j ]) == 150 for i in range(1<<len(input))] ))

75
2025/01/main.go Normal file
View File

@@ -0,0 +1,75 @@
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
)
func main() {
sum := 50
res1 := 0
res2 := 0
file, err := os.Open("./input.txt")
//file, err := os.Open("./test.txt")
if err != nil {
return
}
defer file.Close()
re := regexp.MustCompile("(L|R)(\\w+)")
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Since re : "(L|R)(\w+)", we can parse it as the following i
i := 0
{
m := re.FindStringSubmatch(scanner.Text())
i, _ = strconv.Atoi(m[2])
if m[1] == "L" {
i *= -1
}
}
// Handle sum +=i as mod(100) and work w/res2
{
// Avoid counting rebundant match
if sum == 0 && i < 0 {
res2 -= 1
}
sum += i
for sum < 0 {
sum += 100
res2 += 1
}
for sum > 100 {
sum -= 100
res2 += 1
}
if sum == 100 {
sum -= 100
}
}
// work w/res1
if sum == 0 {
res1 += 1
}
}
if err := scanner.Err(); err != nil {
return
}
fmt.Println(res1, res1+res2)
}

10
2025/01/test.txt Normal file
View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

7
2025/Makefile Normal file
View File

@@ -0,0 +1,7 @@
INPUT = \
01/input.txt
.SUFFIXES: .txt
$(INPUT):
curl "https://adventofcode.com/2025/day/`echo $@|sed -r 's,^0*(\w+/input).txt$$,\1,'`" -H 'Cookie: session=$(SESSION)' > $@