Mina lösningar för Advent of Code 2025, med visualiseringar
Dag 1
Lösning
with open("input.txt", "r") as f:
lines = [l.strip() for l in f.readlines()]
def rotate(i, d, v):
c1 = 0
c2 = 0
while v != 0:
if d == 'L':
i -= 1
v -= 1
if i == -1: i = 99
if v == 0 and i == 0:
c1 += 1
break
if v != 0 and i == 0: c2 += 1
if d == 'R':
i += 1
v -= 1
if i == 100: i = 0
if v == 0 and i == 0:
c1 += 1
break
if v != 0 and i == 0: c2 += 1
return i, c1, c2
def solve1():
i = 50
c = 0
for l in lines:
d = l[0]
v = int(l[1:])
i, c1, c2 = rotate(i, d, v)
c += c1
return c
def solve2():
i = 50
c = 0
for l in lines:
d = l[0]
v = int(l[1:])
i, c1, c2 = rotate(i, d, v)
c += c1 + c2
return c
print(solve1())
print(solve2())
Visualisering
0
10
20
30
40
50
60
70
80
90
50
Input
Steg 0/10
L68← 68
L30← 30
R48→ 48
L5← 5
R60→ 60
L55← 55
L1← 1
L99← 99
R14→ 14
L82← 82
Dag 2
Lösning
with open("input.txt", "r") as f:
data = f.read()
def solve1():
s = 0
for r in data.strip().split(","):
r1, r2 = r.split("-")
for i in range(int(r1), int(r2) + 1):
if len(str(i)) % 2 != 0:
continue
i1, i2 = str(i)[:int(len(str(i)) / 2)], str(i)[int(len(str(i)) / 2):]
if i1 == i2:
s += i
return s
def solve2():
s = 0
for r in data.strip().split(","):
r1, r2 = r.split("-")
for i in range(int(r1), int(r2) + 1):
is1 = str(i)
j = 1
while j <= int(len(is1) / 2):
p = is1[:j]
t = int(len(is1) / j)
is2 = p*t
if is1 == is2:
s += i
break
j += 1
return s
print(solve1())
print(solve2())
Visualisering
Del 1
Input
1188511885
11885
vs
11885
Del 2
Input
824824824
824824824
Dag 3
Lösning
with open("input.txt", "r") as f:
banks = [l.strip() for l in f.readlines()]
def scan(banks, n):
r = []
for b in banks:
sp = 0
bp = len(b) - n + 1
pos_arr = []
for i in range(n):
m = sp
for j in range(sp, bp):
if b[j] > b[m]: m = j
pos_arr.append(m)
bp += 1
sp = m + 1
r.append(''.join([str(b[v]) for v in pos_arr]))
return sum([int(v) for v in r])
def solve1():
return scan(banks, 2)
def solve2():
return scan(banks, 12)
print(solve1())
print(solve2())
Visualisering
N=12
4
3
4
6
3
4
3
2
3
5
1
4
9
4
5
6
5
4
3
4
4
5
2
3
3
3
5
3
5
3
4
2
4
4
5
3
3
3
3
3
3
3
3
3
4
3
4
3
3
2
5
9
3
3
3
3
2
6
3
3
7
3
3
4
3
3
4
3
3
3
4
3
8
3
3
2
5
3
3
3
4
3
4
5
2
4
3
3
2
2
3
3
5
2
4
4
3
3
2
4
Dag 4
Lösning
with open("input.txt", "r") as f:
lines = [l.strip() for l in f.readlines()]
def accessible(i, j):
global lines
p = [
(-1, 0),
(-1, -1),
(0, -1),
(1, -1),
(1, 0),
(1, 1),
(0, 1),
(-1, 1)
]
c = 0
for pos in p:
x, y = pos
if j + x < 0 or i + y < 0 or j + x > len(lines[0]) - 1 or i + y > len(lines) - 1: continue
if lines[i + y][j + x] == '@': c += 1
return c < 4
def scan(lines):
a = 0
for i, l in enumerate(lines):
for j, c in enumerate(l):
if c == '@':
if accessible(i, j):
tmp = list(lines[i])
tmp[j] = 'x'
tmp = ''.join(tmp)
lines[i] = tmp
a += 1
return a, lines
def solve1():
global lines
a, lines_ = scan(lines)
return a
def solve2():
global lines
lines_ = lines
a = 0
while True:
tmp, lines_ = scan(lines)
a += tmp
if tmp == 0:
break
return a
print(solve1())
print(solve2())
Visualisering
.
.
@
@
.
@
@
@
@
.
@
@
@
.
@
.
@
.
@
@
@
@
@
@
@
.
@
.
@
@
@
.
@
@
@
@
.
.
@
.
@
@
.
@
@
@
@
.
@
@
.
@
@
@
@
@
@
@
.
@
.
@
.
@
.
@
.
@
@
@
@
.
@
@
@
.
@
@
@
@
.
@
@
@
@
@
@
@
@
.
@
.
@
.
@
@
@
.
@
.
Dag 5
Lösning
with open("input.txt", "r") as f:
switch = False
ids = []
ranges = []
for l in f.readlines():
if l == '\n': switch = True
if switch: ids.append(l.strip())
else: ranges.append(l.strip())
ids = ids[1:]
def fresh(i, r):
l, h = r.split('-')
l = int(l)
h = int(h)
i = int(i)
return i >= l and i <= h
def solve1():
global ranges
a = 0
for i in ids:
for r in ranges:
if fresh(i, r):
a += 1
break
return a
def solve2():
global ranges
a = 0
ranges = sorted(ranges, key=lambda r: int(r.split('-')[0]))
m = 0
for r in ranges:
l, h = r.split('-')
l = int(l)
h = int(h)
if m > l:
l = m
if m > h:
continue
if m == l:
a -= 1
m = h
a += h-l+1
return a
print(solve1())
print(solve2())
Visualisering
IDs
1
5
8
11
17
32
Ranges
Coverage
X
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Dag 6
Lösning
with open('input.txt') as f:
lines = f.readlines()
def solve1():
global lines
a = 0
lines_ = [l.strip() for l in lines]
problems = [[] for n in lines_[0].split()]
for i in range(len(lines_[0].split())):
for j in range(len(lines_)):
problems[i].append(lines_[j].split()[i])
for p in problems:
o = p[-1]
p = p[:-1]
e = o.join(p)
a += eval(e)
return a
def solve2():
global lines
a = 0
problems = [[] for i in range(len(lines[0].split()))]
cols = []
for i in range(len(lines[0]) - 1):
col = []
for j in range(len(lines)):
col.append(lines[j][i])
cols.append(col)
cols = cols[::-1]
i = 0
eq = []
for j, col in enumerate(cols):
c = ''.join(col).strip()
if c == '': continue
if c[-1] in ['+', '*', '/', '-']:
o = c[-1]
c = c[:-1]
eq.append(c)
problems[i] = o.join(eq)
problems[i] = problems[i].strip()
eq = []
i += 1
continue
eq.append(c)
for p in problems:
a += eval(p)
return a
print(solve1())
print(solve2())
Visualisering
-- Kommer snart --
