Compare commits

..

2 Commits

Author SHA1 Message Date
056e29b96c
day 22 puzzle 1 2024-12-23 11:21:19 +01:00
47fbeb1ce9
started day 21 puzzle 1 2024-12-23 11:15:37 +01:00
10 changed files with 197 additions and 0 deletions

26
day22.py Normal file
View File

@ -0,0 +1,26 @@
with open("res/inputs/day22.txt", "r") as f:
values = list(map(int, f.read().split("\n")))
calls = 0
def step(a, shift):
global calls
calls += 1
s = (a >> -shift) if shift < 0 else (a << shift)
return (s ^ a) & 0xffffff
def rand(value):
b = step(value, 6)
c = step(b, -5)
d = step(c, 11)
return d
total = 0
for v in values:
for _ in range(2000):
v = rand(v)
total += v
print(total)
print(calls)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 130 KiB

After

Width:  |  Height:  |  Size: 130 KiB

View File

@ -37,4 +37,8 @@
19:
stars: 2
20:
stars: 1
21:
stars: 0
22:
stars: 1

5
res/examples/day21.txt Normal file
View File

@ -0,0 +1,5 @@
029A
980A
179A
456A
379A

1
res/examples/day22.txt Normal file
View File

@ -0,0 +1 @@
123

100
src/day21/puzzle1.typ Normal file
View File

@ -0,0 +1,100 @@
#import "/src/utils.typ": *
#let nums = (
"7": (0, 0),
"8": (1, 0),
"9": (2, 0),
"4": (0, 1),
"5": (1, 1),
"6": (2, 1),
"1": (0, 2),
"2": (1, 2),
"3": (2, 2),
"0": (1, 3),
"A": (2, 3),
)
#let arrows = (
"^": (1, 0),
"A": (2, 0),
"<": (0, 1),
"v": (1, 1),
">": (2, 1),
)
#let get-num-path(code) = {
let path = ""
let (x, y) = nums.at("A")
for char in code {
let (x2, y2) = nums.at(char)
let dy = y2 - y
let dx = x2 - x
let ver = if dy == 0 {
""
} else {
(if dy < 0 {"^"} else {"v"}) * calc.abs(dy)
}
let hor = if dx == 0 {
""
} else {
(if dx < 0 {"<"} else {">"}) * calc.abs(dx)
}
path += if y == 3 and x2 == 0 {
ver + hor
} else {
hor + ver
} + "A"
(x, y) = (x2, y2)
}
return path
}
#let get-dir-path(code) = {
let path = ""
let (x, y) = arrows.at("A")
for char in code {
let (x2, y2) = arrows.at(char)
let dy = y2 - y
let dx = x2 - x
let ver = if dy == 0 {
""
} else {
(if dy < 0 {"^"} else {"v"}) * calc.abs(dy)
}
let hor = if dx == 0 {
""
} else {
(if dx < 0 {"<"} else {">"}) * calc.abs(dx)
}
path += if x == 0 and dy < 0 {
hor + ver
} else {
ver + hor
} + "A"
(x, y) = (x2, y2)
}
return path
}
#let get-path(code) = {
let num-path = get-num-path(code)
let dir-path-1 = get-dir-path(num-path)
let dir-path-2 = get-dir-path(dir-path-1)
return dir-path-2
}
#let solve(input) = {
let codes = input.split("\n")
let total = 0
for code in codes {
let len = get-path(code).len()
let num = int(code.slice(0, code.len() - 1))
total += len * num
}
return total
}
#show-puzzle(
21, 1,
solve,
example: 126384
)

0
src/day21/puzzle2.typ Normal file
View File

61
src/day22/puzzle1.typ Normal file
View File

@ -0,0 +1,61 @@
#import "/src/utils.typ": *
#let random(prev) = {
let step(a, shift) = {
let s = if shift < 0 {
a.bit-rshift(-shift)
} else {
a.bit-lshift(shift)
}
return s.bit-xor(a).bit-and(0xffffff)
}
let b = step(prev, 6)
let c = step(b, -5)
let d = step(c, 11)
return d
}
#let solve(input, steps: 1) = {
let values = input.split("\n").map(int)
let total = 0
for value in values {
for _ in range(steps) {
value = random(value)
}
total += value
}
return total
}
#let examples = ()
#let results = (
15887950,
16495136,
527345,
704524,
1553684,
12683156,
11100544,
12249484,
7753432,
5908254
)
#for (i, res) in results.enumerate() {
examples.push((
result: res,
args: (steps: i + 1)
))
}
#show-puzzle(
22, 1,
solve.with(steps: 2000),
example: examples,
only-example: true
)
#show-result(18261820068)
//B = ((A << 6) ^ A) & 0xffffff\
//C = ((B >> 5) ^ B) & 0xffffff\
//D = ((C << 11) ^ C) & 0xffffff

0
src/day22/puzzle2.typ Normal file
View File

Binary file not shown.