Skip to content

Commit

Permalink
24-1
Browse files Browse the repository at this point in the history
  • Loading branch information
koosvary committed Dec 26, 2024
1 parent 2829d54 commit b9d6a45
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 2024/24/24-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import functools

codeValues = {}
sequences = {}

def runSequence(code):
sequence = sequences[code]

match sequence[1]:
case 'AND':
codeValues[code] = getValue(sequence[0]) and getValue(sequence[2])
case 'OR':
codeValues[code] = getValue(sequence[0]) or getValue(sequence[2])
case 'XOR':
codeValues[code] = getValue(sequence[0]) ^ getValue(sequence[2])

return codeValues[code]



@functools.cache
def getValue(code):
if code in codeValues:
return codeValues[code]

return runSequence(code)


with open('2024/24/input.txt') as f:
for line in f:
line = line.strip()

if '->' in line:
split = line.split(' -> ')
result = split[1]
sequence = tuple(split[0].split(' '))
sequences[result] = sequence

elif ':' in line:
split = line.split(': ')
code = split[0]
value = int(split[1])
codeValues[code] = value

zCodes = []
for code in sequences.keys():
if code[0] == 'z':
zCodes.append(code)

zCodes.sort()
print(zCodes)

zValues = ''
for code in reversed(zCodes):
zValues += str(getValue(code))

print(zValues)
print(int(zValues, 2))
47 changes: 47 additions & 0 deletions 2024/24/testinput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
x00: 1
x01: 0
x02: 1
x03: 1
x04: 0
y00: 1
y01: 1
y02: 1
y03: 1
y04: 1

ntg XOR fgs -> mjb
y02 OR x01 -> tnw
kwq OR kpj -> z05
x00 OR x03 -> fst
tgd XOR rvg -> z01
vdt OR tnw -> bfw
bfw AND frj -> z10
ffh OR nrd -> bqk
y00 AND y03 -> djm
y03 OR y00 -> psh
bqk OR frj -> z08
tnw OR fst -> frj
gnj AND tgd -> z11
bfw XOR mjb -> z00
x03 OR x00 -> vdt
gnj AND wpb -> z02
x04 AND y00 -> kjc
djm OR pbm -> qhw
nrd AND vdt -> hwm
kjc AND fst -> rvg
y04 OR y02 -> fgs
y01 AND x02 -> pbm
ntg OR kjc -> kwq
psh XOR fgs -> tgd
qhw XOR tgd -> z09
pbm OR djm -> kpj
x03 XOR y03 -> ffh
x00 XOR y04 -> ntg
bfw OR bqk -> z06
nrd XOR fgs -> wpb
frj XOR qhw -> z04
bqk OR frj -> z07
y03 OR x01 -> nrd
hwm AND bqk -> z03
tgd XOR rvg -> z12
tnw OR pbm -> gnj

0 comments on commit b9d6a45

Please sign in to comment.