Add solution and non solution for day 24
This commit is contained in:
1 parent
fd55e976ae
commit
a905d491a7
7 files changed
+509
No files matched your search
Executable
+60
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys, re, math
|
||||
from operator import add, mul, truediv, mod
|
||||
from functools import reduce
|
||||
|
||||
def parse(line):
|
||||
l = line.split(' ')
|
||||
if l[0] == 'inp': i,o = l
|
||||
else: i,o = l[0], [l[1],l[2]]
|
||||
return (i,o)
|
||||
|
||||
def replist(l,v,r):
|
||||
return [r if i == v else i for i in l]
|
||||
|
||||
def eql(a,b):
|
||||
return int(a == b)
|
||||
|
||||
def div(a,b):
|
||||
return math.trunc(reduce(truediv,[a,b]))
|
||||
|
||||
def alu(instruction, inp):
|
||||
reg = {'w': 0, 'x': 0, 'y': 0, 'z': 0}
|
||||
inp = list(str(inp))
|
||||
index = 0
|
||||
for i,ops in instruction:
|
||||
if i == 'inp':
|
||||
reg[ops] = inp[index]
|
||||
index += 1
|
||||
else:
|
||||
reg[ops[0]] = reduce(eval(i),[int(reg[o]) if o in 'wxyz' else int(o) for o in ops])
|
||||
|
||||
return reg
|
||||
|
||||
if __name__ == '__main__':
|
||||
instr = [parse(line.strip('\n')) for line in open(sys.argv[1])]
|
||||
|
||||
for i in range(1,9):
|
||||
for k in range(11111111111,99999999999):
|
||||
for j in ['18','27']:
|
||||
r = alu(instr,str(i)+j+str(k))
|
||||
print(str(i)+j+str(k),r)
|
||||
if r['z'] == 0: print(i,j,k)
|
||||
|
||||
for i in range(99999999999999,9999999999999,-1):
|
||||
if i % 10000 == 0: print(i)
|
||||
if '0' in str(i): continue
|
||||
r = alu(instr,str(i))
|
||||
if r['z'] == 0:
|
||||
print(i)
|
||||
break
|
||||
|
||||
# challenge 1
|
||||
res1 = ""
|
||||
print("challenge 1:" + "\n" + res1 + "\n")
|
||||
|
||||
# challenge 2
|
||||
res2 = ""
|
||||
print("challenge 2:" + "\n" + res2 + "\n")
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
:- use_module(library(clpfd)).
|
||||
|
||||
% den state kannst du mit nem dictoniary modellieren:
|
||||
initState(state{ x: 0, y: 0, z:0, w: 0 , in: I}, I).
|
||||
% Dictonary lookup: initstate(State), R = State.x
|
||||
% Dictonary update: initstate(State), NewState = State.put(x, 1)
|
||||
|
||||
% Convert List of input digits into single number
|
||||
%input_number(+Inputs, -Number)
|
||||
input_number(Inputs, Number) :- atomic_list_concat(Inputs, Number).
|
||||
|
||||
|
||||
% read program helper functions
|
||||
noteof(L) :- \+ L = [end_of_file].
|
||||
string_term(String,Term) :- term_string(Term, String).
|
||||
parse_instr(String, Inst) :-
|
||||
split_string(String, " ", "", Words),
|
||||
maplist(string_term, Words, Inst).
|
||||
|
||||
% Read a list of Instructions from Filename
|
||||
% read_program(+Filename, -Prog)
|
||||
read_program(Filename, Prog) :-
|
||||
open(Filename, read, Str),
|
||||
read_string(Str, _, S),
|
||||
split_string(S, "\n", "", Lines),
|
||||
maplist(parse_instr, Lines, Instrs),
|
||||
include(noteof, Instrs, Prog),
|
||||
close(Str).
|
||||
|
||||
% Takes input and state and binds O to a number
|
||||
num(N,S,O) :-
|
||||
atom(N),
|
||||
O = S.N.
|
||||
|
||||
num(N,S,O) :-
|
||||
number(N),
|
||||
O = N.
|
||||
|
||||
eq(A,B,R) :-
|
||||
A = B,
|
||||
R = 1.
|
||||
|
||||
eq(A,B,R) :-
|
||||
A #\= B,
|
||||
R = 0.
|
||||
|
||||
read_input(In,F,NIn) :-
|
||||
[F | NIn] = In.
|
||||
|
||||
% interprets an instruction
|
||||
% interpret(+list,+state,R)
|
||||
interpret([inp, A],InState,OutState) :-
|
||||
read_input(InState.in,C,NInput),
|
||||
BS = InState.put(A,C),
|
||||
OutState = BS.put(in,NInput).
|
||||
|
||||
interpret([add, A, B],InState,OutState) :-
|
||||
num(B,InState,C),
|
||||
R #= InState.A + C,
|
||||
OutState = InState.put(A,R).
|
||||
|
||||
interpret([mul, A, B],InState,OutState) :-
|
||||
num(B,InState,C),
|
||||
R #= InState.A*C,
|
||||
OutState = InState.put(A,R).
|
||||
|
||||
interpret([div, A, B],InState,OutState) :-
|
||||
num(B,InState,C),
|
||||
R #= InState.A div C,
|
||||
OutState = InState.put(A,R).
|
||||
|
||||
interpret([mod, A, B],InState,OutState) :-
|
||||
num(B,InState,C),
|
||||
R #= InState.A mod C,
|
||||
OutState = InState.put(A,R).
|
||||
|
||||
interpret([eql, A, B],InState,OutState) :-
|
||||
num(B,InState,C),
|
||||
eq(InState.A, C, R),
|
||||
OutState = InState.put(A,R).
|
||||
|
||||
rprogram(State,[],O) :-
|
||||
O = State.z.
|
||||
|
||||
rprogram(State,[I|Instr], O) :-
|
||||
interpret(I,State,Out),
|
||||
rprogram(Out,Instr,O).
|
||||
|
||||
program(Instr,Input) :-
|
||||
length(Input, 14),
|
||||
Input ins 1..9,
|
||||
initState(S,Input),
|
||||
rprogram(S,Instr, O),
|
||||
O = 0.
|
||||
|
||||
max_monad(M) :-
|
||||
read_program(input24,Instr),
|
||||
program(Instr,R),
|
||||
labeling([max,down],R),
|
||||
input_number(R,M),
|
||||
write(M).
|
||||
|
||||
min_monad(M) :-
|
||||
read_program(input24,Instr),
|
||||
program(Instr,R),
|
||||
labeling([min],R),
|
||||
input_number(R,M),
|
||||
write(M).
|
||||
|
||||
Reference in new issue
Block a user