diff --git a/day17/d17p1.swift b/day17/d17p1.swift new file mode 100644 index 0000000..7ec0671 --- /dev/null +++ b/day17/d17p1.swift @@ -0,0 +1,45 @@ +import Foundation + +/* +0 adv: A / 2^OP -> A +1 bxl: B xor LIT -> B +2 bst: OP % 8 -> B +3 jnz: jump LIT if A != 0 +4 bxc: B xor C -> B (ignore op/lit) +5 out: OP % 8 -> screen +6 bdv: A / 2^OP -> B +7 cdv: A / 2^OP -> C + +adv 1 // A = A / 2 +out A // print(A % 8) +jnz 0 // loop while A != 0 + + +2,4, 1,1, 7,5, 1,5, 4,0, 5,5, 0,3, 3,0 +bst 4 // B = A % 8 +bxl 1 // B = B xor 1 +cdv 5 // C = A / 2^B +bxl 5 // B = B xor 5 +bxc 0 // B = B xor C +out 5 // print(B % 8) +adv 3 // A = A / 2^3 +jnz 0 // loop while A != 0 +*/ + +// let prog = [2,4, 1,1, 7,5, 1,5, 4,0, 5,5, 0,3, 3,0] +//var A = 164279024971709 +var A = 164279024971453 +var B = 0 +var C = 0 + +while true { + B = A % 8 // 1 + B = B ^ 1 // 2 + C = A >> B // 3 + B = B ^ 5 // 4 + B = B ^ C // 5 + print(B % 8, terminator: ",") + A = A >> 3 + if A == 0 { break } +} + diff --git a/day17/d17p2.swift b/day17/d17p2.swift new file mode 100644 index 0000000..1752287 --- /dev/null +++ b/day17/d17p2.swift @@ -0,0 +1,100 @@ +import Foundation + +/* +0 adv: A / 2^OP -> A +1 bxl: B xor LIT -> B +2 bst: OP % 8 -> B +3 jnz: jump LIT if A != 0 +4 bxc: B xor C -> B (ignore op/lit) +5 out: OP % 8 -> screen +6 bdv: A / 2^OP -> B +7 cdv: A / 2^OP -> C + +adv 1 // A = A / 2 +out A // print(A % 8) +jnz 0 // loop while A != 0 + + +2,4, 1,1, 7,5, 1,5, 4,0, 5,5, 0,3, 3,0 +bst 4 // B = A % 8 +bxl 1 // B = B xor 1 +cdv 5 // C = A / 2^B +bxl 5 // B = B xor 5 +bxc 0 // B = B xor C +out 5 // print(B % 8) +adv 3 // A = A / 2^3 +jnz 0 // loop while A != 0 +*/ + +extension RangeReplaceableCollection where Self: StringProtocol { + func paddingToLeft(upTo length: Int, using element: Element = " ") -> SubSequence { + return repeatElement(element, count: Swift.max(0, length-count)) + suffix(Swift.max(count, count-length)) + } +} + +let prog = [2,4, 1,1, 7,5, 1,5, 4,0, 5,5, 0,3, 3,0] + +var A0 = -1 +var Asuffixlen = 16 +var Asuffixes = [0b0110111010111101, 0b0110111110111101] +var maxLen = 0 +aLoop: while true { + A0 += 1 + for suffix in Asuffixes { + //var A = A0 // do this at the beginning to find the suffixes + let Ax = (A0 << Asuffixlen) | suffix + var A = Ax + var B = 0 + var C = 0 + progLoop: for i in 0..> B // 3 + B = B ^ 5 // 4 + B = B ^ C // 5 + // print(B % 8, terminator: ",") + if B % 8 != prog[i] { + if i >= maxLen { + maxLen = i + print(String(Ax, radix: 2).paddingToLeft(upTo: 64) + + " (\(i)) \(prog.prefix(i))") + } + continue aLoop + } + A = A >> 3 + if A == 0 { + if i == prog.count - 1 { + print(Ax) + break aLoop + } + continue aLoop + } + } + } +} + +/* +Loop 1 +B1 = A%8 | B2 | C3 | B4 | B5 = B4^C3 = xxxxx010 | A? +---------+-----+------+-----+-----------------------+------------ + 000 | 001 | A>>1 | 100 | 100^(A>>1) = xxxxx010 | ...xxxx001x (❌) + 001 | 000 | A | 101 | 101^A = xxxxx010 | ...xxxxx111 (❌) + 010 | 011 | A>>3 | 110 | 110^(A>>3) = xxxxx010 | ...xx100010 + 011 | 010 | A>>2 | 111 | 111^(A>>2) = xxxxx010 | ...xxx101xx (❌) + 100 | 101 | A>>5 | 000 | 000^(A>>5) = xxxxx010 | ...010xx100 + 101 | 100 | A>>4 | 001 | 001^(A>>4) = xxxxx010 | ...x011x101 + 110 | 111 | A>>7 | 010 | 010^(A>>7) = xxxxx010 | .000xxxx110 + 111 | 110 | A>>6 | 011 | 011^(A>>6) = xxxxx010 | ..001xxx111 + +Loop 2 +B1 = A%8 | B2 | C3 | B4 | B5 = B4^C3 = xxxxx100 | A? +---------+-----+------+-----+-----------------------+--------- + 000 | 001 | A>>1 | 100 | 100^(A>>1) = xxxxx100 | ...xxxx0000 + 001 | 000 | A | 101 | 101^A = xxxxx100 | ...xxxxx001 + 010 | 011 | A>>3 | 110 | 110^(A>>3) = xxxxx100 | ...xx010010 + 011 | 010 | A>>2 | 111 | 111^(A>>2) = xxxxx100 | ...xxx011xx (❌) + 100 | 101 | A>>5 | 000 | 000^(A>>5) = xxxxx100 | 010xx100 + 101 | 100 | A>>4 | 001 | 001^(A>>4) = xxxxx100 | 0011x101 + 110 | 111 | A>>7 | 010 | 010^(A>>7) = xxxxx100 | 0xxxx110 + 111 | 110 | A>>6 | 011 | 011^(A>>6) = xxxxx100 | 01xxx111 +*/