diff --git a/README.md b/README.md index 6e266db..f08b915 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,11 @@ -# Times +Done with this. Overall everything is solvable. It's more about consistency +and focus. Of course, learning more algorithms and techniques helps. + +Possible to-dos: + +- [ ] Optimize day 19 because it is terrible. + +# Times (and comments) - Day 1: 7:52 ... so slow brah :/ top 100 required 2:05... - Day 2: 22:30 ... I mistyped the first and second was just bad top 100 would @@ -33,4 +40,5 @@ because I loose focus and make silly errors. Like back in Middleschool. - Day 24: Easy. Should have been faster, but no crazy mistakes. Still way too slow for something easy like this. -- Day 25: +- Day 25: Quickly solved via constraint solver. Actual solution much simpler. Don't know + if I would have been able to figure it out. diff --git a/d25.py b/d25.py new file mode 100644 index 0000000..84e245e --- /dev/null +++ b/d25.py @@ -0,0 +1,82 @@ +from lib import * + +EXAMPLE = """1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 +""" + +def snafu_to_dec(n: str) -> int: + r = 0 + for i, c in enumerate(reversed(n)): + c = "=-012".index(c) - 2 + r += c * 5**i + return r + +def dec_to_snafu(n: int) -> str: + r = "" + while n != 0: + rem = n % 5 + n //= 5 + if rem <= 2: + r = str(rem) + r + else: + r = " =-"[rem] + r + n += 1 + return r + +def dec_to_snafu_cs(n: int) -> str: + import cpmpy as cp + import numpy as np + size = 20 + xs = cp.intvar(-2, 2, shape=size, name="xs") + ns = np.array([5**i for i in range(size)]) + m = cp.Model(cp.sum(ns * xs) == n) + m.solve() + + value = xs.value() + assert value is not None + # print(value) + + r = "" + for v in value: + if v >= 0: + r += str(v) + elif v == -1: + r += "-" + elif v == -2: + r += "=" + else: + assert False + r = "".join(reversed(r)) + r = r.lstrip("0") + return r + +def solve(input: Input): + res = 0 + for s in input.lines(): + d = snafu_to_dec(s) + res += d + try: + assert dec_to_snafu_cs(res) == dec_to_snafu(res) + except ModuleNotFoundError: + print("Install 'cpmpy' to solve with constraint solver.") + return dec_to_snafu(res) + +def main(): + DAY_INPUT = "i25.txt" + print("Example 1:", solve(Input(EXAMPLE))) + print("Solution 1:", solve(Input(DAY_INPUT))) + + +if __name__ == "__main__": + main()