From 9af4235a99753bded2ee693fb62ea934549b4d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Fri, 8 Dec 2023 15:21:13 +0100 Subject: [PATCH] day 08 --- Cargo.lock | 86 +++++ Cargo.toml | 3 +- day-08/Cargo.toml | 12 + day-08/src/bin/input.txt | 804 +++++++++++++++++++++++++++++++++++++++ day-08/src/bin/part1.rs | 119 ++++++ day-08/src/bin/part2.rs | 125 ++++++ 6 files changed, 1148 insertions(+), 1 deletion(-) create mode 100644 day-08/Cargo.toml create mode 100644 day-08/src/bin/input.txt create mode 100644 day-08/src/bin/part1.rs create mode 100644 day-08/src/bin/part2.rs diff --git a/Cargo.lock b/Cargo.lock index 7d63119..af7db25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,16 @@ dependencies = [ "itertools", ] +[[package]] +name = "day-08" +version = "0.0.0" +dependencies = [ + "indoc", + "itertools", + "nom", + "num", +] + [[package]] name = "either" version = "1.9.0" @@ -146,6 +156,82 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + [[package]] name = "ranges" version = "0.3.3" diff --git a/Cargo.toml b/Cargo.toml index e222399..06ff2b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,8 @@ members = [ "day-03", "day-05", "day-06", - "day-07" + "day-07", + "day-08" ] [workspace.dependencies] diff --git a/day-08/Cargo.toml b/day-08/Cargo.toml new file mode 100644 index 0000000..d33bcfa --- /dev/null +++ b/day-08/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "day-08" +version = "0.0.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +indoc.workspace = true +itertools.workspace = true +nom.workspace = true +num = "0.4.1" diff --git a/day-08/src/bin/input.txt b/day-08/src/bin/input.txt new file mode 100644 index 0000000..2fe5b29 --- /dev/null +++ b/day-08/src/bin/input.txt @@ -0,0 +1,804 @@ +LRRRLRRLLLRRRLRLRRLRRRLRLRRRLLLRRLRRLRRRLRRRLRLLRLRRLRRLLRRLRLRRRLRRLRRLRRLLRRRLRLRLRLRLLRRLLLRRLRLRRLRLLLLRRLRRRLRRLRRRLLRRRLRRLRRRLRLLRLRRLRRLLRRRLLLRLRRRLLLRRLLRRRLLRRLRRLRRLRLRRRLLRRRLRLLRLRRLLRLRRLRLLRLRRLRRRLLRRLLRRRLRRLRLRLRRRLRLRRRLRRRLRRLRRRLRLLRRRLLRRRR + +FLR = (SXT, CRV) +VGG = (FBP, THJ) +VSK = (TBN, GJB) +XMP = (PSF, QLH) +KHT = (JPM, BLF) +KMD = (TPH, JHR) +TTD = (RVT, MJX) +VKS = (DRS, QJV) +SFR = (GFR, XPS) +MVD = (RLP, VRP) +RJF = (TBK, FMB) +HFD = (JRL, QKG) +LVV = (PDP, SCV) +JRH = (GLG, LTF) +PVQ = (VRC, SHR) +GXN = (PXL, LFT) +NGT = (LFX, TNX) +MTF = (KLC, GXN) +DGS = (NNC, NGB) +SVK = (GXN, KLC) +TJD = (NFP, LJM) +CNM = (JQD, DVR) +XBM = (PHK, LCH) +MJD = (KLH, CQH) +LDQ = (PBC, RXJ) +BSF = (DTN, FNF) +SXX = (CFM, DCK) +LFL = (PRH, LJC) +RMT = (DLM, PGT) +VDK = (LLP, HRP) +CMB = (CFX, VRV) +LPC = (QXS, LHN) +QSL = (SNC, BSK) +XCV = (FXR, RVX) +KMF = (VMP, NVR) +VBM = (HFD, HFD) +SLN = (QLM, FMX) +SRD = (DKM, DPT) +SCN = (DGC, NNT) +MQD = (RFS, FBN) +KJX = (HNQ, TFD) +SDL = (GMG, QVG) +RDM = (NFS, KBG) +FCN = (CBM, FLR) +FCF = (LXM, LHJ) +NPR = (DCL, RPR) +CFX = (CKQ, CLR) +XLF = (MLJ, JCX) +RNK = (GML, MXK) +BNQ = (QJQ, NNQ) +KTF = (CLD, MFT) +LFV = (GFN, GFN) +TBK = (SRD, LCB) +JFS = (FPR, RMR) +DDV = (MXH, GQL) +VTK = (TGV, KJD) +XFQ = (QLM, FMX) +LFX = (TNR, DHR) +FJN = (DRD, MNV) +CMS = (GSK, FGT) +GLN = (LBM, LPJ) +VRC = (NXG, GXQ) +DXR = (GMR, KRJ) +QCX = (CNJ, NGT) +DKM = (LCR, HMK) +QNV = (XVH, NTQ) +ZZZ = (SJH, LBH) +SSG = (FLB, FTT) +JJC = (VGG, QFQ) +GBJ = (VNV, VPT) +NSF = (DMF, KQH) +GTL = (BKD, CFQ) +JMQ = (RXK, BMJ) +FNK = (QNN, JCR) +SGC = (LPC, VHP) +HFP = (VTD, CNL) +HNQ = (FPF, BBS) +JHH = (MDT, TRH) +XXS = (PSG, DRP) +KMC = (BRR, FTJ) +LCB = (DPT, DKM) +QXC = (VGL, PLT) +RLP = (JNB, TGL) +MMK = (HDV, XMP) +HMK = (CTP, TGK) +PQR = (FDC, VNX) +PQN = (BKT, XDX) +SMQ = (BBT, MGM) +TGK = (NND, JGM) +QNQ = (GML, MXK) +JBG = (MDT, TRH) +MLJ = (HFV, XPP) +STB = (QXR, SCN) +LQL = (NCV, NVX) +BGT = (GGT, MKM) +DLM = (JFS, JMN) +KNP = (KQH, DMF) +XDT = (PCS, MHT) +GFR = (GLP, KPP) +PHF = (FCF, NLH) +RFH = (KSC, QRN) +KBG = (SQT, CRH) +RQJ = (VXF, SDQ) +MDT = (RSL, RSL) +KRK = (BKN, RJR) +BMK = (SGN, TTC) +TVQ = (LDC, JRP) +TTH = (NVH, RJF) +VDJ = (HCM, GHQ) +RVX = (JKT, QMK) +MRK = (DQF, BSL) +QSH = (RJR, BKN) +DLB = (PTM, CBH) +JML = (JPL, FHX) +FRK = (CMF, TVQ) +DRP = (PKD, TJD) +NDC = (FCK, BKK) +PCD = (VBM, MBD) +LXN = (FCX, FLM) +SCV = (QXJ, BSF) +NJM = (MNF, QHM) +CHX = (VNV, VPT) +XJC = (CXR, SGJ) +NRR = (PHQ, PVN) +TMN = (PQS, XHL) +PSG = (PKD, TJD) +GHV = (QHB, CDM) +XPV = (BNG, KHF) +HJF = (HSJ, QQT) +NVR = (DGS, JTZ) +BVV = (SSG, KRB) +LQG = (TNQ, FRM) +GMG = (XHX, MRK) +DBD = (SFR, GVF) +TTC = (DBD, GPK) +JFR = (MJB, GSV) +PDF = (BJJ, DSX) +DMF = (JDD, PVS) +NSJ = (NXV, DVS) +XHX = (BSL, DQF) +PHS = (NLV, SHG) +FNL = (KDB, NCN) +PMS = (BNK, QPG) +JDD = (DDB, HJF) +FMH = (JMK, STT) +HBP = (GDC, JJG) +JCR = (GHS, XBM) +RGV = (KNM, BTG) +PQC = (SKV, LQL) +TPG = (BRK, KJX) +BNJ = (DVS, NXV) +FGL = (SVK, MTF) +JNB = (PKM, SXB) +TXQ = (HRL, NJN) +LFB = (DQV, XHD) +SNC = (BKP, XDT) +LBK = (LPP, XCT) +JPL = (KMC, SRL) +GVS = (JHG, HPG) +QSF = (PVN, PHQ) +RPR = (FFH, FNS) +VLN = (RNJ, BFQ) +TLX = (PHF, PTJ) +QBN = (KNP, NSF) +BCH = (JCX, MLJ) +HPB = (MCN, TMN) +GQH = (MTF, SVK) +FMX = (PTC, DGD) +RGK = (RVT, MJX) +VTN = (HLN, GKV) +HTM = (JBC, NMR) +FBP = (KXM, MQL) +CLM = (HTM, QVS) +TLJ = (XRB, DXM) +NTH = (STT, JMK) +NGB = (XTC, PQN) +XHS = (XFX, DGB) +VBB = (VFQ, MVD) +RXH = (KBQ, HGL) +PCS = (RRH, MBP) +BNG = (PJQ, SBX) +XVH = (DFC, RTT) +GMC = (KJX, BRK) +LXD = (KKP, DLF) +GNB = (SCP, RFM) +BRK = (TFD, HNQ) +PQS = (SMQ, FGF) +LKN = (SCN, QXR) +HPV = (KKH, PMS) +GHQ = (VBB, RCB) +FJF = (CXR, SGJ) +GVF = (GFR, XPS) +DCK = (JMP, XCV) +PKM = (HRR, HJL) +TGJ = (CJF, NSR) +NNQ = (BVV, FLD) +JRP = (SLN, XFQ) +CJT = (CVN, LFB) +HLG = (TCH, LTD) +FNX = (RMT, NGD) +QXR = (NNT, DGC) +BRR = (SQF, XHS) +XXB = (TGV, KJD) +FDC = (LDQ, GGD) +XGP = (NJH, VJM) +FTT = (MXV, DPG) +LPP = (LFJ, QJP) +BJL = (GTK, MJJ) +FNS = (KMD, TSC) +LHJ = (MJD, VCP) +HFV = (STB, LKN) +TPR = (QNN, JCR) +RVT = (DFN, NQL) +LVB = (FQT, KHM) +VCL = (LQQ, DTB) +GXQ = (TGJ, XDD) +GSX = (VTM, GPQ) +PTM = (STQ, SVD) +NFS = (CRH, SQT) +XFX = (RXH, TFB) +QLH = (DVK, XRH) +MJB = (HHV, HFP) +QQB = (TMN, MCN) +BKN = (FNX, FMQ) +RKP = (HPT, SDL) +CKD = (TTD, RGK) +QKX = (GKP, TKQ) +HFR = (DSM, MQD) +DMC = (NMD, RFR) +XPS = (GLP, KPP) +CLZ = (TKJ, FMR) +KDB = (XTN, GVS) +BPQ = (BMK, MNL) +PXL = (VCL, QVT) +SQT = (GHH, SNV) +FBN = (HKK, DXR) +SRN = (KLQ, LQC) +KXD = (QJQ, NNQ) +HLN = (NSJ, BNJ) +KQH = (JDD, PVS) +BKB = (KHT, PVC) +RNJ = (QNV, GRM) +BDQ = (BNT, TLJ) +NRQ = (CMB, HLJ) +SMG = (CNJ, NGT) +MKM = (HNP, SJN) +MCL = (RDS, HXK) +QJQ = (BVV, FLD) +XSH = (KLV, NHL) +NXK = (RJF, NVH) +TGV = (LPT, JSX) +XDV = (LJS, PPL) +KVS = (BDS, LFL) +VXB = (QFQ, VGG) +SCP = (LTB, SJQ) +FGH = (NMD, RFR) +RJH = (MCR, MMP) +VMP = (DGS, DGS) +DQS = (GMP, TBV) +FPL = (LCL, TFF) +XXT = (VMC, QPR) +QHM = (JSD, DDL) +BSL = (NXK, TTH) +PRH = (JHX, RHL) +FHX = (KMC, SRL) +SGK = (RQJ, QKL) +NGD = (DLM, PGT) +PVN = (BXC, LXD) +SJN = (GJD, XRV) +KFV = (XNG, FTZ) +JRL = (XDM, TLX) +RSM = (TCS, MMH) +CJF = (BDB, GDR) +XRB = (FQP, GSX) +CBH = (SVD, STQ) +BNT = (XRB, DXM) +BCQ = (LSF, VDJ) +VHG = (HTK, MFH) +DCG = (KKH, KKH) +TDH = (LMN, ZZZ) +TFF = (XHP, LQK) +TRH = (RSL, JDM) +MCR = (GCK, XLT) +KCB = (CMF, TVQ) +HMV = (FCK, BKK) +LQC = (CHN, FQJ) +SNT = (NRQ, SXL) +HXP = (FRK, KCB) +SKV = (NCV, NVX) +TBV = (XKQ, HCL) +XTN = (JHG, HPG) +SCK = (SDL, HPT) +KHM = (HMP, SPQ) +KDT = (GSK, FGT) +GKZ = (VTN, QMF) +TPH = (XXP, CKD) +PFQ = (THX, SNX) +GCJ = (VGL, PLT) +MNF = (DDL, JSD) +BDS = (LJC, PRH) +KJD = (LPT, JSX) +PRV = (MBC, NPR) +KTD = (FXV, PFQ) +NVH = (FMB, TBK) +MJX = (NQL, DFN) +MBV = (CFQ, BKD) +LBM = (SFN, XDV) +FXR = (QMK, JKT) +CHN = (GBJ, CHX) +GGD = (PBC, RXJ) +DTB = (QKX, HFC) +NQL = (JKJ, NJM) +DQF = (NXK, TTH) +QXJ = (DTN, FNF) +BGS = (PFQ, FXV) +FQP = (GPQ, VTM) +DPT = (HMK, LCR) +PHK = (DNF, GHV) +STQ = (FCN, NNS) +LCL = (XHP, LQK) +QJP = (FJN, CCV) +GJD = (BCQ, VBR) +GMR = (BJX, XNH) +JKT = (VTR, QKB) +RTT = (PRV, LNL) +LNL = (NPR, MBC) +DRD = (JRH, KVD) +SHR = (NXG, GXQ) +RXK = (LFQ, QSK) +VBR = (LSF, VDJ) +BKT = (VHQ, KPD) +QJV = (CMS, KDT) +CPH = (NRQ, SXL) +CSM = (QRN, KSC) +KLV = (GMC, TPG) +HDR = (RQJ, QKL) +TNX = (DHR, TNR) +VHQ = (CXX, CVL) +FCX = (FNK, TPR) +MBP = (BPV, NNB) +KKH = (BNK, BNK) +LQQ = (QKX, HFC) +QKL = (SDQ, VXF) +BKP = (PCS, MHT) +JTB = (BRS, XXS) +HRL = (HTC, FSM) +FXV = (SNX, THX) +TNR = (PVQ, KSF) +GLG = (TXQ, HSH) +PGV = (RFM, SCP) +FLM = (TPR, FNK) +JHX = (QSF, NRR) +PVC = (JPM, BLF) +GML = (VKS, CPT) +PDD = (PGV, GNB) +FMQ = (NGD, RMT) +XNG = (XMC, GVV) +LNH = (PBB, PBB) +XFN = (HTK, HTK) +LFT = (VCL, QVT) +DVS = (RGV, PMX) +VGL = (XBL, TQB) +MNV = (JRH, KVD) +LTB = (XXT, CPL) +NKN = (XKF, FQX) +FQX = (RDJ, CNR) +BJJ = (HFR, KBJ) +CFM = (JMP, XCV) +FQT = (HMP, SPQ) +PLT = (XBL, TQB) +QSN = (GQL, MXH) +HKV = (JBG, JHH) +LLC = (SHG, NLV) +SXT = (QPN, TFS) +MBD = (HFD, VCZ) +HMP = (CJN, SGC) +KBF = (LML, QVC) +HNC = (RFH, CSM) +JMP = (FXR, RVX) +JSX = (CNF, XKM) +VCZ = (QKG, JRL) +CSQ = (BFQ, RNJ) +GMP = (XKQ, HCL) +GLM = (PTM, CBH) +VPT = (BGT, BPH) +RJR = (FMQ, FNX) +KQT = (XJC, FJF) +NNS = (FLR, CBM) +VKV = (KNP, NSF) +NCV = (QFS, STN) +XCT = (LFJ, QJP) +VJK = (BKB, XJM) +FNF = (LNH, JNJ) +JHG = (VSK, MML) +HLJ = (VRV, CFX) +GRM = (XVH, NTQ) +FCK = (BDQ, GQS) +DRS = (CMS, KDT) +DVK = (SRC, VJK) +PDP = (QXJ, BSF) +THX = (FGM, XVD) +DNF = (QHB, CDM) +GKH = (HXK, RDS) +RFS = (HKK, DXR) +GKP = (GTL, MBV) +MXH = (BLG, CLM) +PQQ = (JJG, GDC) +RKC = (BCH, XLF) +QVG = (XHX, MRK) +DGC = (PNS, JTB) +HTC = (RSF, TTS) +FDV = (FQX, XKF) +CNF = (XGP, BNN) +HJL = (QJR, RJH) +QNN = (GHS, XBM) +BPV = (GGQ, GQJ) +SVD = (FCN, NNS) +XKF = (RDJ, CNR) +XSR = (LKM, PTB) +VTR = (SSL, DQS) +DPG = (LFV, TLP) +XTC = (BKT, XDX) +QFS = (FRQ, XPV) +RDJ = (CJT, MKS) +CXR = (NDC, HMV) +LML = (GLB, RSM) +NKG = (LBK, MSS) +CLD = (XXB, VTK) +VXF = (NQS, JFR) +MCN = (PQS, XHL) +GQD = (DVR, JQD) +FTZ = (GVV, XMC) +SNV = (SNT, CPH) +NCN = (XTN, GVS) +QKB = (DQS, SSL) +GSV = (HHV, HFP) +LCH = (GHV, DNF) +KBJ = (DSM, MQD) +FRQ = (KHF, BNG) +BFQ = (QNV, GRM) +HSJ = (SVR, RPX) +XSM = (SHJ, VMR) +VDX = (XNG, XNG) +HCL = (MJQ, JML) +NJH = (NDP, VTX) +LCX = (LCL, TFF) +VNX = (LDQ, GGD) +HPT = (QVG, GMG) +NSR = (GDR, BDB) +XRH = (SRC, VJK) +CPL = (VMC, QPR) +XHD = (KRK, QSH) +GGQ = (PQC, RVF) +LSF = (GHQ, HCM) +XHL = (SMQ, FGF) +HXK = (NTH, FMH) +VNV = (BPH, BGT) +MHX = (VMP, VMP) +MST = (HVL, PLQ) +JHR = (XXP, CKD) +FMB = (SRD, LCB) +FTJ = (XHS, SQF) +VTX = (MRS, GRS) +CXX = (HDG, KQT) +SFN = (LJS, PPL) +STT = (JTP, VDK) +RVK = (MFT, CLD) +XJM = (PVC, KHT) +PMX = (KNM, BTG) +NFP = (QSL, GBP) +HRR = (RJH, QJR) +VRV = (CLR, CKQ) +MSS = (XCT, LPP) +KHF = (PJQ, SBX) +SJH = (BLH, MDV) +BNK = (VVF, VVF) +FFH = (KMD, TSC) +LFJ = (FJN, CCV) +TFD = (FPF, BBS) +CJN = (LPC, VHP) +TFB = (HGL, KBQ) +TNQ = (KNF, MGF) +NLV = (KTF, RVK) +CBM = (CRV, SXT) +NTQ = (DFC, RTT) +DSM = (RFS, FBN) +DLF = (QXC, GCJ) +XXN = (XLF, BCH) +TFV = (DVV, PDD) +TKJ = (DNV, HXP) +JGM = (QCX, SMG) +HRP = (MMK, DDN) +FRT = (LFL, BDS) +CLR = (STS, SRN) +NQS = (GSV, MJB) +CQH = (GQD, CNM) +PPL = (JGT, HKV) +PVS = (HJF, DDB) +LJC = (JHX, RHL) +MGM = (PHS, LLC) +VJH = (PLQ, HVL) +LDC = (SLN, XFQ) +TQB = (VLN, CSQ) +BLF = (QNQ, RNK) +HPG = (VSK, MML) +BTG = (FNL, HMQ) +CRH = (GHH, SNV) +DML = (DVV, PDD) +QPG = (VVF, CLZ) +RSF = (QSN, DDV) +DNV = (FRK, KCB) +CVN = (DQV, XHD) +TCH = (DLB, GLM) +LJM = (GBP, QSL) +QMF = (GKV, HLN) +PTJ = (NLH, FCF) +KKP = (QXC, GCJ) +RHL = (NRR, QSF) +PGT = (JMN, JFS) +PTC = (DMC, FGH) +BRS = (DRP, PSG) +VHP = (LHN, QXS) +GKV = (NSJ, BNJ) +MNL = (SGN, TTC) +FSD = (VPX, GLN) +RMR = (RKP, SCK) +MDV = (DML, TFV) +VFQ = (VRP, RLP) +XBL = (VLN, CSQ) +NMD = (RKC, XXN) +MJQ = (FHX, JPL) +DPN = (PDP, SCV) +XNH = (NKN, FDV) +NDP = (MRS, GRS) +KPD = (CVL, CXX) +DFC = (PRV, LNL) +KBQ = (LQT, LNC) +NXG = (TGJ, XDD) +XRV = (VBR, BCQ) +JDM = (VDX, KFV) +QHB = (FSD, PML) +GDR = (BPQ, SBC) +SDQ = (NQS, JFR) +VJM = (VTX, NDP) +QLT = (VMR, SHJ) +STN = (XPV, FRQ) +AAA = (LBH, SJH) +MJJ = (HDR, SGK) +STS = (LQC, KLQ) +CFQ = (BNQ, KXD) +PML = (GLN, VPX) +KLC = (PXL, LFT) +PLQ = (HBP, PQQ) +NLH = (LHJ, LXM) +DVV = (GNB, PGV) +TGL = (PKM, SXB) +MRS = (NKG, GMD) +HMQ = (NCN, KDB) +TBN = (FPL, LCX) +HNP = (XRV, GJD) +DGD = (DMC, FGH) +PTB = (PDF, GDT) +TCS = (BGS, KTD) +QRN = (RTJ, XSK) +DDN = (XMP, HDV) +PJM = (MJJ, GTK) +NND = (QCX, SMG) +LTD = (GLM, DLB) +DDL = (VXB, JJC) +HCM = (VBB, RCB) +GQJ = (PQC, RVF) +MGF = (DPN, LVV) +LXT = (DCG, HPV) +BXC = (KKP, DLF) +HPK = (QVC, LML) +JBC = (FCT, JMQ) +JGT = (JBG, JHH) +BJX = (FDV, NKN) +KSF = (SHR, VRC) +JJG = (QLT, XSM) +GBP = (SNC, BSK) +GJB = (FPL, LCX) +LJS = (HKV, JGT) +LQT = (QQB, HPB) +GGT = (HNP, SJN) +KLH = (GQD, CNM) +KSC = (RTJ, XSK) +SXL = (HLJ, CMB) +BBT = (PHS, LLC) +MXV = (LFV, TLP) +GQS = (TLJ, BNT) +PSF = (XRH, DVK) +KTC = (RFH, CSM) +HSH = (NJN, HRL) +HGG = (HPK, KBF) +PKD = (LJM, NFP) +HFC = (GKP, TKQ) +VLA = (QMF, VTN) +JKJ = (MNF, QHM) +FQJ = (GBJ, CHX) +JPM = (QNQ, RNK) +MMP = (GCK, XLT) +NNB = (GGQ, GQJ) +QMK = (QKB, VTR) +FRM = (KNF, MGF) +GPK = (GVF, SFR) +GTK = (HDR, SGK) +VCP = (KLH, CQH) +CPT = (QJV, DRS) +BPH = (MKM, GGT) +GRS = (NKG, GMD) +LTF = (HSH, TXQ) +SJQ = (XXT, CPL) +DGB = (TFB, RXH) +XMC = (KLM, SXX) +SRC = (BKB, XJM) +VTD = (MRN, NPH) +SSL = (TBV, GMP) +QVS = (NMR, JBC) +KLQ = (FQJ, CHN) +XDM = (PTJ, PHF) +JQD = (CFN, JTS) +BDB = (BPQ, SBC) +XKM = (XGP, BNN) +GQL = (CLM, BLG) +JTP = (HRP, LLP) +NMR = (FCT, JMQ) +RFR = (XXN, RKC) +XPT = (TCH, LTD) +TTS = (DDV, QSN) +LNC = (HPB, QQB) +NJN = (FSM, HTC) +FLD = (KRB, SSG) +KNF = (DPN, LVV) +BMJ = (LFQ, QSK) +GLB = (MMH, TCS) +XDD = (CJF, NSR) +MXK = (CPT, VKS) +DCL = (FNS, FFH) +PBC = (MHX, MHX) +RFM = (LTB, SJQ) +KLM = (DCK, CFM) +XVD = (RDM, QQJ) +RDS = (NTH, FMH) +RVF = (SKV, LQL) +BLG = (QVS, HTM) +CNR = (CJT, MKS) +LFQ = (HNC, KTC) +FPR = (SCK, RKP) +LQK = (CDR, XSR) +QVC = (RSM, GLB) +DDB = (HSJ, QQT) +SQV = (QMF, VTN) +HDG = (XJC, FJF) +LCR = (CTP, TGK) +JTZ = (NGB, NNC) +FSM = (RSF, TTS) +BSK = (BKP, XDT) +GHS = (LCH, PHK) +SRL = (FTJ, BRR) +BNN = (NJH, VJM) +PNS = (XXS, BRS) +PJA = (XMC, GVV) +XXF = (FDC, VNX) +JMN = (FPR, RMR) +GHH = (CPH, SNT) +QXS = (LXT, NVN) +BKD = (KXD, BNQ) +MKS = (LFB, CVN) +XKQ = (MJQ, JML) +PJQ = (TQP, LVB) +QPR = (GKH, MCL) +MFH = (SQV, GKZ) +SBC = (BMK, MNL) +THJ = (MQL, KXM) +XHP = (XSR, CDR) +HDV = (QLH, PSF) +RSL = (VDX, VDX) +MMH = (KTD, BGS) +HVL = (HBP, PQQ) +FGF = (BBT, MGM) +RRH = (NNB, BPV) +NHL = (GMC, TPG) +NNC = (PQN, XTC) +KPP = (XFN, VHG) +VTM = (HGG, CTC) +XDX = (VHQ, KPD) +SPQ = (CJN, SGC) +GDC = (XSM, QLT) +QQJ = (KBG, NFS) +MML = (TBN, GJB) +QFQ = (FBP, THJ) +VVF = (FMR, TKJ) +JJQ = (KLV, NHL) +PHQ = (BXC, LXD) +KNM = (FNL, HMQ) +GCK = (PQR, XXF) +DSX = (KBJ, HFR) +TKQ = (MBV, GTL) +JNJ = (PBB, TDH) +CVL = (KQT, HDG) +SHJ = (QBN, VKV) +NNT = (PNS, JTB) +CRV = (QPN, TFS) +PBB = (LMN, LMN) +GDT = (BJJ, DSX) +FGM = (QQJ, RDM) +BLH = (DML, TFV) +FMR = (DNV, HXP) +FPF = (VJH, MST) +KXM = (BJL, PJM) +FCT = (BMJ, RXK) +FLB = (MXV, DPG) +SXB = (HRR, HJL) +CMF = (JRP, LDC) +HKK = (KRJ, GMR) +TSC = (TPH, JHR) +QKG = (XDM, TLX) +QVT = (DTB, LQQ) +SHG = (KTF, RVK) +GMD = (MSS, LBK) +NXV = (PMX, RGV) +KRB = (FTT, FLB) +CTC = (KBF, HPK) +DTN = (LNH, LNH) +DXM = (GSX, FQP) +LBH = (BLH, MDV) +FGT = (HLG, XPT) +QPN = (XSH, JJQ) +DQV = (KRK, QSH) +LMN = (LBH, SJH) +SGJ = (HMV, NDC) +LPJ = (XDV, SFN) +MHT = (RRH, MBP) +TFS = (JJQ, XSH) +QLM = (PTC, DGD) +GSK = (HLG, XPT) +LXM = (VCP, MJD) +GLP = (XFN, XFN) +BBS = (MST, VJH) +SVR = (MCS, LQG) +NPH = (FGL, GQH) +NVN = (DCG, HPV) +VSA = (FMR, TKJ) +MCS = (TNQ, FRM) +JSD = (JJC, VXB) +QQT = (SVR, RPX) +CNL = (MRN, NPH) +HTK = (SQV, SQV) +JMK = (VDK, JTP) +VMC = (GKH, MCL) +RCB = (MVD, VFQ) +LHN = (LXT, NVN) +RXJ = (MHX, KMF) +VMR = (QBN, VKV) +DVR = (JTS, CFN) +XXP = (TTD, RGK) +BKK = (GQS, BDQ) +CCV = (DRD, MNV) +GPQ = (CTC, HGG) +SBX = (TQP, LVB) +SQF = (XFX, DGB) +SGN = (GPK, DBD) +LPT = (XKM, CNF) +CFN = (KVS, FRT) +NVX = (STN, QFS) +KRJ = (XNH, BJX) +VRP = (TGL, JNB) +LLP = (DDN, MMK) +NBQ = (FCX, FLM) +DFN = (JKJ, NJM) +RPX = (MCS, LQG) +QJR = (MCR, MMP) +CNJ = (LFX, TNX) +GVV = (SXX, KLM) +MFT = (VTK, XXB) +QKA = (JRL, QKG) +SNX = (FGM, XVD) +JTS = (KVS, FRT) +XPP = (LKN, STB) +XSK = (LXN, NBQ) +CPA = (NNC, NGB) +MBC = (RPR, DCL) +CTP = (NND, JGM) +MRN = (FGL, GQH) +CDM = (PML, FSD) +LKM = (GDT, PDF) +TQP = (FQT, KHM) +RTJ = (LXN, NBQ) +CDR = (LKM, PTB) +HHV = (CNL, VTD) +QSK = (KTC, HNC) +KVD = (GLG, LTF) +VPX = (LBM, LPJ) +GFN = (VBM, VBM) +JCX = (HFV, XPP) +DHR = (PVQ, KSF) +XLT = (XXF, PQR) +CKQ = (STS, SRN) +MQL = (PJM, BJL) +TLP = (GFN, PCD) +HGL = (LNC, LQT) \ No newline at end of file diff --git a/day-08/src/bin/part1.rs b/day-08/src/bin/part1.rs new file mode 100644 index 0000000..c883494 --- /dev/null +++ b/day-08/src/bin/part1.rs @@ -0,0 +1,119 @@ +use std::collections::HashMap; + +use itertools::{FoldWhile, Itertools}; +use nom::{ + bytes::complete::{is_a, tag, take_while1}, + character::{ + complete::{char, line_ending}, + is_alphabetic, + }, + combinator::map, + multi::{count, separated_list1}, + sequence::{delimited, separated_pair}, + IResult, +}; + +fn main() { + println!("{}", part1(include_str!("./input.txt"))); +} + +fn part1(input: &str) -> u32 { + let (directions, nodes) = separated_pair( + map(is_a("RL"), |s: &str| { + s.chars() + .map(|c| match c { + 'R' => Direction::Right, + 'L' => Direction::Left, + _ => unreachable!(), + }) + .collect::>() + }), + count(line_ending, 2), + separated_list1(line_ending, node_parser), + )(input) + .expect("invalid input") + .1; + + let nodes = nodes.into_iter().collect::>(); + + match directions + .iter() + .cycle() + .enumerate() + .fold_while(("AAA", 0), |(acc, _), (i, d)| { + let (l, r) = nodes.get(acc).unwrap(); + let next = match d { + Direction::Right => r, + Direction::Left => l, + }; + + if next == &"ZZZ" { + FoldWhile::Done((next, i)) + } else { + FoldWhile::Continue((next, i)) + } + }) { + FoldWhile::Done((_, i)) => i as u32 + 1, + FoldWhile::Continue(_) => panic!("invalid input"), + } +} + +fn node_parser(i: &str) -> IResult<&str, (&str, (&str, &str))> { + separated_pair( + take_while1(|c| is_alphabetic(c as u8)), + tag(" = "), + delimited( + char('('), + separated_pair( + take_while1(|c| is_alphabetic(c as u8)), + tag(", "), + take_while1(|c| is_alphabetic(c as u8)), + ), + char(')'), + ), + )(i) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Direction { + Right, + Left, +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + #[test] + fn test_part1() { + assert_eq!( + part1(indoc!( + " + RL + + AAA = (BBB, CCC) + BBB = (DDD, EEE) + CCC = (ZZZ, GGG) + DDD = (DDD, DDD) + EEE = (EEE, EEE) + GGG = (GGG, GGG) + ZZZ = (ZZZ, ZZZ) + " + )), + 2 + ); + assert_eq!( + part1(indoc!( + " + LLR + + AAA = (BBB, BBB) + BBB = (AAA, ZZZ) + ZZZ = (ZZZ, ZZZ) + " + )), + 6 + ); + } +} diff --git a/day-08/src/bin/part2.rs b/day-08/src/bin/part2.rs new file mode 100644 index 0000000..93a3d3c --- /dev/null +++ b/day-08/src/bin/part2.rs @@ -0,0 +1,125 @@ +use std::collections::HashMap; + +use itertools::{FoldWhile, Itertools}; +use nom::{ + bytes::complete::{is_a, tag, take_while1}, + character::{ + complete::{char, line_ending}, + is_alphanumeric, + }, + combinator::map, + multi::{count, separated_list1}, + sequence::{delimited, separated_pair}, + IResult, +}; +use num::integer; + +fn main() { + println!("{}", part2(include_str!("./input.txt"))); +} + +fn part2(input: &str) -> usize { + let (directions, nodes) = separated_pair( + map(is_a("RL"), |s: &str| { + s.chars() + .map(|c| match c { + 'R' => Direction::Right, + 'L' => Direction::Left, + _ => unreachable!(), + }) + .collect::>() + }), + count(line_ending, 2), + separated_list1(line_ending, node_parser), + )(input) + .expect("invalid input") + .1; + + let nodes = nodes.into_iter().collect::>(); + + let starting_nodes = nodes + .iter() + .filter(|(n, (_, _))| n.ends_with('A')) + .map(|(n, (_, _))| *n) + .collect::>(); + + starting_nodes + .into_iter() + .map(|n| find_steps_to_goal(n, &nodes, &directions)) + .fold(1_usize, integer::lcm) +} + +fn find_steps_to_goal( + starting_node: &str, + nodes: &HashMap<&str, (&str, &str)>, + directions: &[Direction], +) -> usize { + match directions.iter().cycle().enumerate().fold_while( + (starting_node, 0), + |(acc, _), (i, d)| { + let (l, r) = nodes.get(acc).unwrap(); + let next = match d { + Direction::Right => r, + Direction::Left => l, + }; + + if next.ends_with('Z') { + FoldWhile::Done((next, i)) + } else { + FoldWhile::Continue((next, i)) + } + }, + ) { + FoldWhile::Done((_, i)) => i + 1, + FoldWhile::Continue(_) => panic!("invalid input"), + } +} + +fn node_parser(i: &str) -> IResult<&str, (&str, (&str, &str))> { + separated_pair( + take_while1(|c| is_alphanumeric(c as u8)), + tag(" = "), + delimited( + char('('), + separated_pair( + take_while1(|c| is_alphanumeric(c as u8)), + tag(", "), + take_while1(|c| is_alphanumeric(c as u8)), + ), + char(')'), + ), + )(i) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Direction { + Right, + Left, +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + #[test] + fn test_part2() { + assert_eq!( + part2(indoc!( + " + LR + + 11A = (11B, XXX) + 11B = (XXX, 11Z) + 11Z = (11B, XXX) + 22A = (22B, XXX) + 22B = (22C, 22C) + 22C = (22Z, 22Z) + 22Z = (22B, 22B) + XXX = (XXX, XXX) + " + )), + 6 + ); + } +}