diff --git a/24/input b/24/input new file mode 100644 index 0000000..a0b03c4 --- /dev/null +++ b/24/input @@ -0,0 +1,313 @@ +x00: 1 +x01: 0 +x02: 1 +x03: 1 +x04: 0 +x05: 0 +x06: 1 +x07: 1 +x08: 0 +x09: 1 +x10: 1 +x11: 1 +x12: 1 +x13: 0 +x14: 0 +x15: 1 +x16: 0 +x17: 0 +x18: 1 +x19: 1 +x20: 1 +x21: 1 +x22: 0 +x23: 0 +x24: 1 +x25: 0 +x26: 1 +x27: 1 +x28: 0 +x29: 0 +x30: 1 +x31: 1 +x32: 0 +x33: 1 +x34: 1 +x35: 0 +x36: 0 +x37: 0 +x38: 0 +x39: 1 +x40: 0 +x41: 1 +x42: 1 +x43: 0 +x44: 1 +y00: 1 +y01: 0 +y02: 0 +y03: 1 +y04: 1 +y05: 0 +y06: 0 +y07: 0 +y08: 0 +y09: 0 +y10: 0 +y11: 1 +y12: 0 +y13: 1 +y14: 1 +y15: 0 +y16: 1 +y17: 0 +y18: 1 +y19: 0 +y20: 1 +y21: 0 +y22: 1 +y23: 0 +y24: 0 +y25: 0 +y26: 0 +y27: 0 +y28: 0 +y29: 0 +y30: 1 +y31: 1 +y32: 1 +y33: 1 +y34: 0 +y35: 0 +y36: 0 +y37: 1 +y38: 1 +y39: 0 +y40: 1 +y41: 1 +y42: 0 +y43: 0 +y44: 1 + +x01 XOR y01 -> fht +cpg OR qrh -> wtp +wtk XOR thb -> z30 +mrj OR cbd -> bjr +y12 XOR x12 -> kng +hrh AND chp -> bgc +qkp XOR ggh -> z36 +x24 XOR y24 -> mqt +jbp AND qsw -> dhs +kng XOR jqh -> z12 +x21 XOR y21 -> rvk +qqd AND hfb -> qmd +ggh AND qkp -> svm +y26 AND x26 -> bch +mms AND qsf -> cvm +kdt AND rqf -> mrj +jrg OR kqm -> ngk +y43 XOR x43 -> fns +x10 XOR y10 -> jkn +hjc XOR cgv -> z09 +swm OR wjb -> cgv +y17 AND x17 -> fvk +qsw XOR jbp -> z44 +y01 AND x01 -> dmk +y29 XOR x29 -> gnj +x32 XOR y32 -> qqd +x25 XOR y25 -> cbd +y23 AND x23 -> bvb +dvj AND bjg -> djv +dbv XOR bcm -> z03 +x02 XOR y02 -> qdt +qqv OR qkh -> nqr +tmh AND bjr -> cjf +knp XOR nvr -> z08 +rmc XOR hdk -> z42 +y07 XOR x07 -> mms +nqr AND jtk -> jgf +y18 AND x18 -> qdp +x31 AND y31 -> cbh +x06 AND y06 -> z06 +wwt OR rjp -> bkk +y17 XOR x17 -> ptj +y14 XOR x14 -> fgr +y11 XOR x11 -> tsh +fhk XOR bkq -> z28 +kkg AND nwg -> jpt +y03 AND x03 -> qkh +tmh XOR bjr -> z26 +rkw OR pww -> kdt +tfn AND qgq -> jgt +nrs XOR qdt -> z02 +kgw OR rds -> bjg +fcb OR hnr -> jbp +y38 AND x38 -> cpg +rqf XOR kdt -> z25 +y12 AND x12 -> fcd +pnh XOR jsp -> z27 +hfb XOR qqd -> z32 +ftc OR fjm -> bkq +y05 XOR x05 -> pvt +cjt XOR sfm -> jmq +x27 XOR y27 -> jsp +rrt AND cjs -> nsb +hjc AND cgv -> dfj +x34 AND y34 -> qtd +x00 AND y00 -> nqp +qvh AND cgj -> mmm +x21 AND y21 -> vfv +gts XOR cvg -> z16 +x38 XOR y38 -> njc +pbb OR mkc -> bcm +hct OR hmc -> wbt +rvk XOR jgk -> z21 +cjt AND sfm -> fmd +svm OR bbb -> nsf +dmk OR ntf -> nrs +y44 XOR x44 -> qsw +x36 XOR y36 -> qkp +x27 AND y27 -> ftc +gbd OR fjv -> z13 +nmh OR nsb -> jgk +rjj OR fvk -> nwg +qqj OR vwp -> pqg +x04 AND y04 -> bwb +mqt AND ssw -> rkw +y30 XOR x30 -> thb +bdc AND pvt -> chv +wrj AND njp -> kqf +jtk XOR nqr -> z04 +gqf OR qtd -> njp +njc AND ngk -> z38 +x39 AND y39 -> rds +x10 AND y10 -> hct +hch AND dmm -> bhs +dvj XOR bjg -> z40 +y24 AND x24 -> pww +y03 XOR x03 -> dbv +y28 XOR x28 -> fhk +nmm XOR kwb -> gmh +vfv OR qpp -> pjk +gvt AND qpm -> rbf +rrt XOR cjs -> z20 +pvt XOR bdc -> z05 +y31 XOR x31 -> hrh +y40 XOR x40 -> dvj +y28 AND x28 -> ghp +y22 AND x22 -> stn +nwg XOR kkg -> z18 +ptw AND pjk -> cpb +cbh OR bgc -> hfb +x18 XOR y18 -> kkg +x35 XOR y35 -> wrj +vmr OR mwp -> srh +tsh AND wbt -> dwn +mpv XOR wtp -> z39 +y34 XOR x34 -> hvf +y19 AND x19 -> gqg +fcd OR bnt -> kwb +hdk AND rmc -> vwp +nbk OR knk -> chp +x30 AND y30 -> nbk +qmn OR kqf -> ggh +gts AND cvg -> mwp +tfn XOR qgq -> z19 +x33 AND y33 -> rjp +nrs AND qdt -> mkc +y36 AND x36 -> bbb +wbt XOR tsh -> z11 +kjv OR dfj -> whc +mvf OR mmm -> rmc +pnh AND jsp -> fjm +x11 AND y11 -> mfr +cvm OR sbj -> knp +jgt OR gqg -> cjs +x08 XOR y08 -> nvr +y39 XOR x39 -> mpv +tmm AND wqn -> wwt +qmd OR wtb -> tmm +fgr AND gmh -> ckd +wrj XOR njp -> z35 +wdq OR hrf -> wtk +y42 XOR x42 -> hdk +jqh AND kng -> bnt +bvb OR rbf -> ssw +cgj XOR qvh -> z41 +x13 XOR y13 -> nmm +ptj AND srh -> rjj +x13 AND y13 -> fjv +x43 AND y43 -> hnr +hvf XOR bkk -> z34 +x15 XOR y15 -> hch +y14 AND x14 -> cqb +fmd OR jmq -> qsf +ngk XOR njc -> qrh +x41 XOR y41 -> cgj +x07 AND y07 -> sbj +nmm AND kwb -> gbd +gmh XOR fgr -> z14 +gnj XOR bcv -> z29 +y32 AND x32 -> wtb +x08 AND y08 -> swm +y29 AND x29 -> wdq +pqg XOR fns -> z43 +ndr AND nsf -> jrg +bcm AND dbv -> qqv +jgk AND rvk -> qpp +gvt XOR qpm -> z23 +x22 XOR y22 -> ptw +y02 AND x02 -> pbb +jpt OR qdp -> qgq +nsf XOR ndr -> z37 +bhs OR msb -> cvg +y09 AND x09 -> kjv +dwn OR mfr -> jqh +x35 AND y35 -> qmn +jgf OR bwb -> bdc +y09 XOR x09 -> hjc +pjk XOR ptw -> z22 +x19 XOR y19 -> tfn +bcv AND gnj -> hrf +x16 AND y16 -> vmr +srh XOR ptj -> z17 +y40 AND x40 -> wgw +x04 XOR y04 -> jtk +jkn AND whc -> hmc +chp XOR hrh -> z31 +cpb OR stn -> qpm +y42 AND x42 -> qqj +fhk AND bkq -> mtn +y37 XOR x37 -> ndr +fht XOR nqp -> z01 +x25 AND y25 -> rqf +fns AND pqg -> fcb +tmm XOR wqn -> z33 +cjf OR bch -> pnh +x23 XOR y23 -> gvt +mms XOR qsf -> z07 +hvf AND bkk -> gqf +x33 XOR y33 -> wqn +y37 AND x37 -> kqm +y15 AND x15 -> msb +x20 AND y20 -> nmh +wtk AND thb -> knk +mtn OR ghp -> bcv +wvm OR dhs -> z45 +wgw OR djv -> qvh +ckd OR cqb -> dmm +x41 AND y41 -> mvf +y05 AND x05 -> smt +hch XOR dmm -> z15 +y44 AND x44 -> wvm +smt OR chv -> cjt +knp AND nvr -> wjb +x06 XOR y06 -> sfm +x26 XOR y26 -> tmh +wtp AND mpv -> kgw +y20 XOR x20 -> rrt +ssw XOR mqt -> z24 +nqp AND fht -> ntf +y00 XOR x00 -> z00 +jkn XOR whc -> z10 +y16 XOR x16 -> gts \ No newline at end of file diff --git a/24/main.rb b/24/main.rb new file mode 100644 index 0000000..735b6e6 --- /dev/null +++ b/24/main.rb @@ -0,0 +1,67 @@ +#!/usr/bin/env ruby + +input = (ARGV.first.nil? ? DATA : ARGF) + .readlines(chomp: true) +split = input.index("") + +gates = input[split+1..] +wires = input.join(" ").scan(/([a-z0-9]{3})/).flatten.uniq.map { [_1, nil] }.to_h +input[...split] + .map { _1.split(": ") } + .each { wires[_1] = _2.to_i } + + +i = 0 +until gates.empty? + if i >= gates.size + i = 0 + end + gate = gates[i] + in1, op, in2, out = gate.scan(/([a-z0-9]{3}) ([A-Z]+) ([a-z0-9]{3}) -> ([a-z0-9]{3})/).first + + if wires[in1].nil? || wires[in2].nil? + i += 1 + next + end + + case op + when "AND" + wires[out] = wires[in1] & wires[in2] + when "OR" + wires[out] = wires[in1] | wires[in2] + when "XOR" + wires[out] = wires[in1] ^ wires[in2] + end + + gates.delete(gate) +end + +x = wires.select { _1.match?(/^x/) }.sort_by(&:first).map { _1.last.to_s }.reverse.join +xi = x.to_i(2) +y = wires.select { _1.match?(/^y/) }.sort_by(&:first).map { _1.last.to_s }.reverse.join +yi = y.to_i(2) +z = wires.select { _1.match?(/^z/) }.sort_by(&:first).map { _1.last.to_s }.reverse.join +zi = z.to_i(2) +p zi + +__END__ +x00: 0 +x01: 1 +x02: 0 +x03: 1 +x04: 0 +x05: 1 +y00: 0 +y01: 0 +y02: 1 +y03: 1 +y04: 0 +y05: 1 + +x00 AND y00 -> z05 +x01 AND y01 -> z02 +x02 AND y02 -> z01 +x03 AND y03 -> z03 +x04 AND y04 -> z04 +x05 AND y05 -> z00 +