diff --git a/day24/Cargo.toml b/day24/Cargo.toml new file mode 100644 index 0000000..ddaaf58 --- /dev/null +++ b/day24/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day24" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day24/pbcopy b/day24/pbcopy new file mode 100644 index 0000000..8da310e --- /dev/null +++ b/day24/pbcopy @@ -0,0 +1,545 @@ +Hello, AoC day 24! +{"mmq": And("gcd", "rtg"), "z01": Xor("wnt", "gct"), "qkr": Xor("y18", "x18"), "gvb": And("y15", "x15"), "ktn": Or("vhv", "vhs"), "fgn": And("y23", "x23"), "z32": Xor("nfh", "hnc"), "ddq": And("ngv", "ktn"), "nhb": And("x19", "y19"), "qvt": And("x10", "y10"), "y02": Value(false), "hpn": And("rqh", "wvd"), "vqn": And("cfc", "qgm"), "wdj": And("kvf", "drc"), "shg": Or("ntn", "kjm"), "rtg": Xor("y41", "x41"), "vfb": Or("gfm", "wkt"), "wtg": And("tdb", "jdv"), "cgn": And("y18", "x18"), "x12": Value(true), "z15": Xor("rgk", "mrg"), "gfm": And("hks", "rvt"), "z02": Xor("qcs", "gwm"), "jph": Or("tpm", "tbc"), "y18": Value(false), "y44": Value(true), "kmj": Xor("y06", "x06"), "x35": Value(true), "y30": Value(true), "y33": Value(true), "z13": Xor("jdv", "tdb"), "x44": Value(true), "smb": Xor("y37", "x37"), "gfw": And("nkg", "qjw"), "hjb": Xor("x07", "y07"), "z27": Xor("kpm", "tjh"), "y42": Value(true), "nkg": Xor("y10", "x10"), "z10": Xor("qjw", "nkg"), "rsf": Xor("y16", "x16"), "x41": Value(false), "gcq": And("x01", "y01"), "ckq": Or("kbq", "qtb"), "jqk": Or("qvt", "gfw"), "gtb": Or("bnk", "qpk"), "tpm": And("x29", "y29"), "srr": And("tft", "jph"), "z18": Xor("msb", "qkr"), "y03": Value(true), "kbq": And("mwh", "dpt"), "vhp": Or("bfh", "hpn"), "x03": Value(true), "dqt": Or("twc", "mmq"), "z23": Xor("fgn", "shg"), "cfc": Or("wth", "qwn"), "z30": Xor("jph", "tft"), "tft": Xor("x30", "y30"), "fsp": Xor("x31", "y31"), "x38": Value(true), "gct": And("x00", "y00"), "x02": Value(true), "y37": Value(true), "qtb": And("x39", "y39"), "sbw": And("x14", "y14"), "y36": Value(false), "vgb": Xor("x42", "y42"), "shj": And("x25", "y25"), "y04": Value(true), "jbg": And("mrt", "jfv"), "x43": Value(false), "mrg": Xor("y15", "x15"), "x04": Value(false), "mgw": Xor("x28", "y28"), "msb": Or("ffj", "vqn"), "z33": Xor("kdc", "gts"), "smm": And("x43", "y43"), "drc": Xor("x24", "y24"), "bwh": Or("qkw", "wnm"), "kqf": Xor("y43", "x43"), "x40": Value(false), "y20": Value(false), "gwm": Xor("y02", "x02"), "z17": Xor("cfc", "qgm"), "z16": Xor("rsf", "fqb"), "y05": Value(false), "x37": Value(true), "x18": Value(false), "x00": Value(true), "rck": And("nvh", "dwr"), "jdv": Xor("x13", "y13"), "y15": Value(false), "x17": Value(false), "hrb": And("kqh", "ntb"), "x26": Value(false), "bjt": And("scd", "jts"), "tdb": Or("qdg", "fvt"), "x09": Value(true), "z43": Xor("kqf", "fkb"), "jpn": Xor("y35", "x35"), "pdw": Xor("x12", "y12"), "x23": Value(true), "z40": Xor("ckq", "npv"), "cjg": And("npv", "ckq"), "ntn": And("y22", "x22"), "sjb": Xor("y20", "x20"), "y34": Value(false), "pkc": Xor("x26", "y26"), "qgm": Xor("y17", "x17"), "jkh": And("y42", "x42"), "z44": Xor("scd", "jts"), "kss": And("x32", "y32"), "z22": Xor("gtb", "hbj"), "gcd": Or("cjg", "ttn"), "fvt": And("rtk", "pdw"), "y22": Value(false), "x25": Value(false), "x08": Value(false), "npv": Xor("x40", "y40"), "z21": Xor("fgw", "pqg"), "vfp": Or("srr", "ktw"), "fqb": Or("dtn", "gvb"), "x16": Value(false), "thd": And("x03", "y03"), "qfd": And("kdc", "gts"), "y23": Value(true), "x42": Value(false), "y40": Value(true), "z39": Xor("dpt", "mwh"), "y32": Value(true), "cjj": And("jht", "ptw"), "x07": Value(true), "qwn": And("x16", "y16"), "x31": Value(false), "kfb": And("x27", "y27"), "vtj": And("y08", "x08"), "nvh": Xor("smb", "vhp"), "rgk": Or("sbw", "hrb"), "ffj": And("x17", "y17"), "x28": Value(true), "z42": Xor("vgb", "dqt"), "y39": Value(true), "x05": Value(false), "z20": Xor("sjb", "ssr"), "chr": Or("hbd", "btk"), "rqh": Xor("x36", "y36"), "x22": Value(false), "dwr": Xor("x38", "y38"), "wkt": And("y05", "x05"), "y12": Value(false), "y08": Value(false), "y14": Value(true), "vks": And("tjh", "kpm"), "fnq": And("vfp", "fsp"), "y06": Value(false), "y11": Value(true), "y19": Value(false), "y13": Value(true), "y09": Value(false), "y31": Value(false), "mjf": And("sjb", "ssr"), "z19": And("wmb", "jcg"), "sbt": And("x13", "y13"), "jts": Xor("y44", "x44"), "qkw": And("kmj", "vfb"), "qcs": Or("gcq", "dnc"), "z26": Xor("pkc", "hnq"), "qdb": And("kqf", "fkb"), "vhv": And("y02", "x02"), "kvf": Or("dck", "fwj"), "mrt": Xor("y09", "x09"), "z05": Xor("rvt", "hks"), "btk": And("x28", "y28"), "ntb": Or("sbt", "wtg"), "jfv": Or("vtj", "ptr"), "y27": Value(true), "knj": And("y44", "x44"), "rtk": Or("dkt", "hcm"), "rfr": Xor("x29", "y29"), "fgw": Or("wmd", "mjf"), "hrt": And("jrr", "jpn"), "wmb": Xor("x19", "y19"), "kdc": Or("kss", "tbr"), "hbd": And("dpc", "mgw"), "qdg": Xor("pdw", "rtk"), "jrr": Or("vcc", "jhv"), "tvg": And("y26", "x26"), "cdw": And("x09", "y09"), "jrv": And("y07", "x07"), "x14": Value(false), "x34": Value(false), "z37": Or("gng", "qgb"), "twc": And("y41", "x41"), "y41": Value(true), "gds": Or("jrv", "qks"), "rvt": Xor("x05", "y05"), "x24": Value(false), "x27": Value(true), "bfh": And("x36", "y36"), "dck": Xor("x23", "y23"), "dkt": And("x11", "y11"), "qks": And("hjb", "bwh"), "dnc": And("wnt", "gct"), "tjh": Xor("x27", "y27"), "z08": Xor("hwq", "gds"), "x10": Value(true), "x29": Value(true), "z25": Xor("jgq", "msf"), "dtn": And("mrg", "rgk"), "y00": Value(true), "dpt": Or("rck", "rbs"), "tbr": And("hnc", "nfh"), "qjw": Or("cdw", "jbg"), "hdg": And("y24", "x24"), "fwj": And("shg", "fgn"), "z12": And("x12", "y12"), "kqh": Xor("x14", "y14"), "x21": Value(false), "vvf": Xor("jcg", "wmb"), "nfh": Or("fnq", "wpv"), "ttn": And("y40", "x40"), "z35": Xor("jrr", "jpn"), "x01": Value(false), "kjm": And("gtb", "hbj"), "y26": Value(true), "mnq": Xor("y11", "x11"), "wnm": And("x06", "y06"), "mcc": And("msb", "qkr"), "dpc": Or("vks", "kfb"), "pmm": And("x33", "y33"), "jht": Or("thd", "ddq"), "z09": Xor("mrt", "jfv"), "jgq": Or("hdg", "wdj"), "x13": Value(false), "rbs": And("y38", "x38"), "ngv": Xor("x03", "y03"), "qwb": And("y35", "x35"), "x33": Value(false), "hnq": Or("fqj", "shj"), "z00": Xor("x00", "y00"), "z03": Xor("ngv", "ktn"), "wth": And("fqb", "rsf"), "jhv": And("y34", "x34"), "bnk": And("x21", "y21"), "y07": Value(false), "y01": Value(false), "rgv": And("dqt", "vgb"), "z38": Xor("dwr", "nvh"), "ktw": And("x30", "y30"), "y25": Value(true), "ptw": Xor("y04", "x04"), "msf": Xor("y25", "x25"), "hnc": Xor("x32", "y32"), "fkb": Or("jkh", "rgv"), "jcg": Or("mcc", "cgn"), "vhs": And("qcs", "gwm"), "y24": Value(false), "wpv": And("y31", "x31"), "wvd": Or("qwb", "hrt"), "wmd": And("y20", "x20"), "x06": Value(true), "x19": Value(true), "gng": And("x37", "y37"), "hrg": Xor("y34", "x34"), "z34": Xor("rmg", "hrg"), "hwq": Xor("y08", "x08"), "z07": Xor("bwh", "hjb"), "y43": Value(true), "tpf": And("y04", "x04"), "x39": Value(false), "scd": Or("smm", "qdb"), "tbc": And("rfr", "chr"), "qgb": And("vhp", "smb"), "z14": Xor("ntb", "kqh"), "hks": Or("tpf", "cjj"), "gts": Xor("x33", "y33"), "z31": Xor("fsp", "vfp"), "hcm": And("jqk", "mnq"), "z11": Xor("jqk", "mnq"), "y16": Value(true), "y17": Value(false), "z36": Xor("wvd", "rqh"), "z29": Xor("chr", "rfr"), "y28": Value(true), "hbj": Xor("x22", "y22"), "x11": Value(true), "rmg": Or("qfd", "pmm"), "vds": And("pkc", "hnq"), "y10": Value(false), "x15": Value(true), "ptr": And("hwq", "gds"), "x30": Value(true), "z06": Xor("kmj", "vfb"), "z45": Or("knj", "bjt"), "ssr": Or("nhb", "vvf"), "y29": Value(false), "wnt": Xor("y01", "x01"), "z04": Xor("jht", "ptw"), "qpk": And("fgw", "pqg"), "y38": Value(false), "fqj": And("jgq", "msf"), "kpm": Or("tvg", "vds"), "z41": Xor("rtg", "gcd"), "y21": Value(false), "x20": Value(true), "x36": Value(false), "pqg": Xor("x21", "y21"), "z28": Xor("mgw", "dpc"), "z24": Xor("drc", "kvf"), "vcc": And("rmg", "hrg"), "y35": Value(true), "x32": Value(true), "mwh": Xor("y39", "x39")} +z45z44z43z42z41z40z39z38z37z36z35z34z33z32z31z30z29z28z27z26z25z24z23z22z21z20z19z18z17z16z15z14z13z12z11z10z09z08z07z06z05z04z03z02z01z00 +the resulting z value is 52728619468518 +begin graphviz output + + + + +graph addingmachine { +mmq -- gcd +mmq -- rtg +z01 -- wnt +z01 -- gct +qkr -- y18 +qkr -- x18 +gvb -- y15 +gvb -- x15 +ktn -- vhv +ktn -- vhs +fgn -- y23 +fgn -- x23 +z32 -- nfh +z32 -- hnc +ddq -- ngv +ddq -- ktn +nhb -- x19 +nhb -- y19 +qvt -- x10 +qvt -- y10 +y02 [shape=box]; +hpn -- rqh +hpn -- wvd +vqn -- cfc +vqn -- qgm +wdj -- kvf +wdj -- drc +shg -- ntn +shg -- kjm +rtg -- y41 +rtg -- x41 +vfb -- gfm +vfb -- wkt +wtg -- tdb +wtg -- jdv +cgn -- y18 +cgn -- x18 +x12 [shape=box]; +z15 -- rgk +z15 -- mrg +gfm -- hks +gfm -- rvt +z02 -- qcs +z02 -- gwm +jph -- tpm +jph -- tbc +y18 [shape=box]; +y44 [shape=box]; +kmj -- y06 +kmj -- x06 +x35 [shape=box]; +y30 [shape=box]; +y33 [shape=box]; +z13 -- jdv +z13 -- tdb +x44 [shape=box]; +smb -- y37 +smb -- x37 +gfw -- nkg +gfw -- qjw +hjb -- x07 +hjb -- y07 +z27 -- kpm +z27 -- tjh +y42 [shape=box]; +nkg -- y10 +nkg -- x10 +z10 -- qjw +z10 -- nkg +rsf -- y16 +rsf -- x16 +x41 [shape=box]; +gcq -- x01 +gcq -- y01 +ckq -- kbq +ckq -- qtb +jqk -- qvt +jqk -- gfw +gtb -- bnk +gtb -- qpk +tpm -- x29 +tpm -- y29 +srr -- tft +srr -- jph +z18 -- msb +z18 -- qkr +y03 [shape=box]; +kbq -- mwh +kbq -- dpt +vhp -- bfh +vhp -- hpn +x03 [shape=box]; +dqt -- twc +dqt -- mmq +z23 -- fgn +z23 -- shg +cfc -- wth +cfc -- qwn +z30 -- jph +z30 -- tft +tft -- x30 +tft -- y30 +fsp -- x31 +fsp -- y31 +x38 [shape=box]; +gct -- x00 +gct -- y00 +x02 [shape=box]; +y37 [shape=box]; +qtb -- x39 +qtb -- y39 +sbw -- x14 +sbw -- y14 +y36 [shape=box]; +vgb -- x42 +vgb -- y42 +shj -- x25 +shj -- y25 +y04 [shape=box]; +jbg -- mrt +jbg -- jfv +x43 [shape=box]; +mrg -- y15 +mrg -- x15 +x04 [shape=box]; +mgw -- x28 +mgw -- y28 +msb -- ffj +msb -- vqn +z33 -- kdc +z33 -- gts +smm -- x43 +smm -- y43 +drc -- x24 +drc -- y24 +bwh -- qkw +bwh -- wnm +kqf -- y43 +kqf -- x43 +x40 [shape=box]; +y20 [shape=box]; +gwm -- y02 +gwm -- x02 +z17 -- cfc +z17 -- qgm +z16 -- rsf +z16 -- fqb +y05 [shape=box]; +x37 [shape=box]; +x18 [shape=box]; +x00 [shape=box]; +rck -- nvh +rck -- dwr +jdv -- x13 +jdv -- y13 +y15 [shape=box]; +x17 [shape=box]; +hrb -- kqh +hrb -- ntb +x26 [shape=box]; +bjt -- scd +bjt -- jts +tdb -- qdg +tdb -- fvt +x09 [shape=box]; +z43 -- kqf +z43 -- fkb +jpn -- y35 +jpn -- x35 +pdw -- x12 +pdw -- y12 +x23 [shape=box]; +z40 -- ckq +z40 -- npv +cjg -- npv +cjg -- ckq +ntn -- y22 +ntn -- x22 +sjb -- y20 +sjb -- x20 +y34 [shape=box]; +pkc -- x26 +pkc -- y26 +qgm -- y17 +qgm -- x17 +jkh -- y42 +jkh -- x42 +z44 -- scd +z44 -- jts +kss -- x32 +kss -- y32 +z22 -- gtb +z22 -- hbj +gcd -- cjg +gcd -- ttn +fvt -- rtk +fvt -- pdw +y22 [shape=box]; +x25 [shape=box]; +x08 [shape=box]; +npv -- x40 +npv -- y40 +z21 -- fgw +z21 -- pqg +vfp -- srr +vfp -- ktw +fqb -- dtn +fqb -- gvb +x16 [shape=box]; +thd -- x03 +thd -- y03 +qfd -- kdc +qfd -- gts +y23 [shape=box]; +x42 [shape=box]; +y40 [shape=box]; +z39 -- dpt +z39 -- mwh +y32 [shape=box]; +cjj -- jht +cjj -- ptw +x07 [shape=box]; +qwn -- x16 +qwn -- y16 +x31 [shape=box]; +kfb -- x27 +kfb -- y27 +vtj -- y08 +vtj -- x08 +nvh -- smb +nvh -- vhp +rgk -- sbw +rgk -- hrb +ffj -- x17 +ffj -- y17 +x28 [shape=box]; +z42 -- vgb +z42 -- dqt +y39 [shape=box]; +x05 [shape=box]; +z20 -- sjb +z20 -- ssr +chr -- hbd +chr -- btk +rqh -- x36 +rqh -- y36 +x22 [shape=box]; +dwr -- x38 +dwr -- y38 +wkt -- y05 +wkt -- x05 +y12 [shape=box]; +y08 [shape=box]; +y14 [shape=box]; +vks -- tjh +vks -- kpm +fnq -- vfp +fnq -- fsp +y06 [shape=box]; +y11 [shape=box]; +y19 [shape=box]; +y13 [shape=box]; +y09 [shape=box]; +y31 [shape=box]; +mjf -- sjb +mjf -- ssr +z19 -- wmb +z19 -- jcg +sbt -- x13 +sbt -- y13 +jts -- y44 +jts -- x44 +qkw -- kmj +qkw -- vfb +qcs -- gcq +qcs -- dnc +z26 -- pkc +z26 -- hnq +qdb -- kqf +qdb -- fkb +vhv -- y02 +vhv -- x02 +kvf -- dck +kvf -- fwj +mrt -- y09 +mrt -- x09 +z05 -- rvt +z05 -- hks +btk -- x28 +btk -- y28 +ntb -- sbt +ntb -- wtg +jfv -- vtj +jfv -- ptr +y27 [shape=box]; +knj -- y44 +knj -- x44 +rtk -- dkt +rtk -- hcm +rfr -- x29 +rfr -- y29 +fgw -- wmd +fgw -- mjf +hrt -- jrr +hrt -- jpn +wmb -- x19 +wmb -- y19 +kdc -- kss +kdc -- tbr +hbd -- dpc +hbd -- mgw +qdg -- pdw +qdg -- rtk +jrr -- vcc +jrr -- jhv +tvg -- y26 +tvg -- x26 +cdw -- x09 +cdw -- y09 +jrv -- y07 +jrv -- x07 +x14 [shape=box]; +x34 [shape=box]; +z37 -- gng +z37 -- qgb +twc -- y41 +twc -- x41 +y41 [shape=box]; +gds -- jrv +gds -- qks +rvt -- x05 +rvt -- y05 +x24 [shape=box]; +x27 [shape=box]; +bfh -- x36 +bfh -- y36 +dck -- x23 +dck -- y23 +dkt -- x11 +dkt -- y11 +qks -- hjb +qks -- bwh +dnc -- wnt +dnc -- gct +tjh -- x27 +tjh -- y27 +z08 -- hwq +z08 -- gds +x10 [shape=box]; +x29 [shape=box]; +z25 -- jgq +z25 -- msf +dtn -- mrg +dtn -- rgk +y00 [shape=box]; +dpt -- rck +dpt -- rbs +tbr -- hnc +tbr -- nfh +qjw -- cdw +qjw -- jbg +hdg -- y24 +hdg -- x24 +fwj -- shg +fwj -- fgn +z12 -- x12 +z12 -- y12 +kqh -- x14 +kqh -- y14 +x21 [shape=box]; +vvf -- jcg +vvf -- wmb +nfh -- fnq +nfh -- wpv +ttn -- y40 +ttn -- x40 +z35 -- jrr +z35 -- jpn +x01 [shape=box]; +kjm -- gtb +kjm -- hbj +y26 [shape=box]; +mnq -- y11 +mnq -- x11 +wnm -- x06 +wnm -- y06 +mcc -- msb +mcc -- qkr +dpc -- vks +dpc -- kfb +pmm -- x33 +pmm -- y33 +jht -- thd +jht -- ddq +z09 -- mrt +z09 -- jfv +jgq -- hdg +jgq -- wdj +x13 [shape=box]; +rbs -- y38 +rbs -- x38 +ngv -- x03 +ngv -- y03 +qwb -- y35 +qwb -- x35 +x33 [shape=box]; +hnq -- fqj +hnq -- shj +z00 -- x00 +z00 -- y00 +z03 -- ngv +z03 -- ktn +wth -- fqb +wth -- rsf +jhv -- y34 +jhv -- x34 +bnk -- x21 +bnk -- y21 +y07 [shape=box]; +y01 [shape=box]; +rgv -- dqt +rgv -- vgb +z38 -- dwr +z38 -- nvh +ktw -- x30 +ktw -- y30 +y25 [shape=box]; +ptw -- y04 +ptw -- x04 +msf -- y25 +msf -- x25 +hnc -- x32 +hnc -- y32 +fkb -- jkh +fkb -- rgv +jcg -- mcc +jcg -- cgn +vhs -- qcs +vhs -- gwm +y24 [shape=box]; +wpv -- y31 +wpv -- x31 +wvd -- qwb +wvd -- hrt +wmd -- y20 +wmd -- x20 +x06 [shape=box]; +x19 [shape=box]; +gng -- x37 +gng -- y37 +hrg -- y34 +hrg -- x34 +z34 -- rmg +z34 -- hrg +hwq -- y08 +hwq -- x08 +z07 -- bwh +z07 -- hjb +y43 [shape=box]; +tpf -- y04 +tpf -- x04 +x39 [shape=box]; +scd -- smm +scd -- qdb +tbc -- rfr +tbc -- chr +qgb -- vhp +qgb -- smb +z14 -- ntb +z14 -- kqh +hks -- tpf +hks -- cjj +gts -- x33 +gts -- y33 +z31 -- fsp +z31 -- vfp +hcm -- jqk +hcm -- mnq +z11 -- jqk +z11 -- mnq +y16 [shape=box]; +y17 [shape=box]; +z36 -- wvd +z36 -- rqh +z29 -- chr +z29 -- rfr +y28 [shape=box]; +hbj -- x22 +hbj -- y22 +x11 [shape=box]; +rmg -- qfd +rmg -- pmm +vds -- pkc +vds -- hnq +y10 [shape=box]; +x15 [shape=box]; +ptr -- hwq +ptr -- gds +x30 [shape=box]; +z06 -- kmj +z06 -- vfb +z45 -- knj +z45 -- bjt +ssr -- nhb +ssr -- vvf +y29 [shape=box]; +wnt -- y01 +wnt -- x01 +z04 -- jht +z04 -- ptw +qpk -- fgw +qpk -- pqg +y38 [shape=box]; +fqj -- jgq +fqj -- msf +kpm -- tvg +kpm -- vds +z41 -- rtg +z41 -- gcd +y21 [shape=box]; +x20 [shape=box]; +x36 [shape=box]; +pqg -- x21 +pqg -- y21 +z28 -- mgw +z28 -- dpc +z24 -- drc +z24 -- kvf +vcc -- rmg +vcc -- hrg +y35 [shape=box]; +x32 [shape=box]; +mwh -- y39 +mwh -- x39 +} diff --git a/day24/src/main.rs b/day24/src/main.rs new file mode 100644 index 0000000..b6dc050 --- /dev/null +++ b/day24/src/main.rs @@ -0,0 +1,128 @@ +use std::collections::HashMap; + +use std::env; +use std::fs; + +#[derive(Debug, Eq, PartialEq, Hash)] +enum Wire<'a> { + Value(bool), + And(&'a str, &'a str), + Or(&'a str, &'a str), + Xor(&'a str, &'a str), +} + +fn main() { + println!("Hello, AoC day 24!"); + + let args: Vec = env::args().collect(); + if args.len() != 2 { + println!("wrong number of arguments!"); + std::process::exit(1); + } + + let file_path = &args[1]; + + let file = fs::read_to_string(file_path) + .expect("should be able to read the file"); + + let (values, connections) = file.split_once("\n\n").expect("failed to split the file?"); + let mut wires: HashMap<&str, Wire> = HashMap::new(); + values + .lines() + .for_each(|x| { + let (label, val_str) = x.split_once(": ").expect("wires should have a : in them"); + wires.insert(label, Wire::Value(val_str == "1")); + }); + + connections + .lines() + .for_each(|line| { + let (expr, label) = line.split_once(" -> ").expect("connections should have ->"); + let expression: Vec<_> = expr.split(" ").collect(); + let (a, op, b) = (expression[0], expression[1], expression[2]); + wires.insert(label, match op { + "AND" => Wire::And(a, b), + "OR" => Wire::Or(a, b), + "XOR" => Wire::Xor(a, b), + _ => Wire::Value(false), + }); + }); + + println!("{wires:?}"); + + // part a + + let mut zs: Vec = wires.keys() + .filter(|name| name.starts_with("z")) + .map(|name| name.to_string()) + .collect(); + + zs.sort(); + zs = zs.into_iter().rev().collect(); + for z in &zs { print!("{z}"); } + print!("\n"); + + let mut part_a: u64 = 0; + for z in &zs { + part_a = part_a << 1; + let this = get_value(z, &wires); + if this { part_a = part_a + 1; } + } + + println!("the resulting z value is {part_a}"); + + // part b + + println!("BEGIN GRAPHVIZ OUTPUT\n\n\n\n"); + println!("digraph addingmachine {{"); + for (label, wire) in &wires { + match wire { + Wire::Value(_) => print!("{label} [shape=box, color={}];", if label.starts_with("x") { "blue" } else { "red" }), + _ => () + } + } + + for (label, wire) in &wires { + match wire { + Wire::Value(_) => (), + Wire::And(a, b) => { + if label.starts_with("z") { print!("{label} [shape=triangle, color=green];"); + } else { print!("{label} [shape=triangle];"); } + print!("{a} -> {label};"); + print!("{b} -> {label};"); + }, + Wire::Or(a, b) => { + if label.starts_with("z") { print!("{label} [shape=egg, color=green];"); + } else { print!("{label} [shape=egg];"); } + print!("{a} -> {label};"); + print!("{b} -> {label};"); + }, + Wire::Xor(a, b) => { + if label.starts_with("z") { print!("{label} [shape=star, color=green];"); + } else { print!("{label} [shape=star];"); } + print!("{a} -> {label};"); + print!("{b} -> {label};"); + }, + } + } + println!("}}"); + println!("\n\n\n\nEND GRAPHVIZ OUTPUT"); + + println!("by direct inspection of the graph rendered by the above graphviz output,"); + println!("we find that the pairs that need to swap are z12+qdg, z19+vvf, z37+nvh, fgn+dck"); + let mut answer = vec!["z12", "qdg", "z19", "vvf", "z37", "nvh", "fgn", "dck"]; + answer.sort(); + print!("or, in order: "); + for x in answer { print!("{x},"); } + print!("\n"); +} + +fn get_value<'a>(name: &'a str, system: &HashMap<&'a str, Wire>) -> bool { + match system.get(name) { + None => false, + Some(Wire::Value(v)) => *v, + Some(Wire::And(a, b)) => get_value(a, system) && get_value(b, system), + Some(Wire::Or(a, b)) => get_value(a, system) || get_value(b, system), + Some(Wire::Xor(a, b)) => get_value(a, system) ^ get_value(b, system), + } +} \ No newline at end of file diff --git a/day25/Cargo.toml b/day25/Cargo.toml new file mode 100644 index 0000000..e5f4cc3 --- /dev/null +++ b/day25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day25" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day25/src/main.rs b/day25/src/main.rs new file mode 100644 index 0000000..19ec18b --- /dev/null +++ b/day25/src/main.rs @@ -0,0 +1,84 @@ +use std::env; +use std::fs; +use std::iter::zip; + +fn main() { + println!("Hello, AoC day 24!"); + + let args: Vec = env::args().collect(); + if args.len() != 2 { + println!("wrong number of arguments!"); + std::process::exit(1); + } + + let file_path = &args[1]; + + let file = fs::read_to_string(file_path) + .expect("should be able to read the file"); + + let keys = file.split("\n\n") + .filter(|section| section.starts_with(".....")) + .map(string_transpose) + .map(|vec_of_vec_of_char| vec_of_vec_of_char + .into_iter() + .map(|vec_of_char| vec_of_char + .into_iter() + .filter(|c| *c == '#') + .count() - 1 + ) + .collect::>() + ) + .collect::>(); + let locks = file.split("\n\n") + .filter(|section| section.starts_with("#####")) + .map(string_transpose) + .map(|vec_of_vec_of_char| vec_of_vec_of_char + .into_iter() + .map(|vec_of_char| vec_of_char + .into_iter() + .filter(|c| *c == '#') + .count() - 1 + ) + .collect::>() + ) + .collect::>(); + + + println!("{keys:?}, {locks:?}"); + + let mut sum = 0; + for lock in &locks { + for key in &keys { + if fits(lock, key) { + sum += 1; + } + } + } + println!("the number of unique lock/key pairs is {sum}"); +} +fn string_transpose(several_lines: &str) -> Vec> { + let mut output = Vec::with_capacity(several_lines.lines().next().unwrap().len()); + + for line in several_lines.lines() { + for (idx, chr) in line.chars().enumerate() { + if idx == output.len() { + output.push(vec![]); + } + output[idx].push(chr); + } + } + + return output +} + +fn fits(lock: &Vec, key: &Vec) -> bool { + //println!("testing {lock:?} against {key:?}"); + for (tumbler, tooth) in zip(lock, key) { + //println!("tumbler: {tumbler}; tooth: {tooth}"); + if *tumbler + *tooth > 5 { + return false; + } + } + //println!("we good!"); + return true; +} \ No newline at end of file