From 38b84d497e7dbe47426f4b9ae7caab8ea505ffa6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 19 Dec 2023 14:23:55 -0500 Subject: [PATCH] Complete 2023 day 8 part 1 --- 2023/day8/Cargo.toml | 8 + 2023/day8/README.md | 49 ++ 2023/day8/src/example-input.txt | 5 + 2023/day8/src/input.txt | 796 ++++++++++++++++++++++++++++++++ 2023/day8/src/main.rs | 112 +++++ 5 files changed, 970 insertions(+) create mode 100644 2023/day8/Cargo.toml create mode 100644 2023/day8/README.md create mode 100644 2023/day8/src/example-input.txt create mode 100644 2023/day8/src/input.txt create mode 100644 2023/day8/src/main.rs diff --git a/2023/day8/Cargo.toml b/2023/day8/Cargo.toml new file mode 100644 index 0000000..4879259 --- /dev/null +++ b/2023/day8/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day8" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023/day8/README.md b/2023/day8/README.md new file mode 100644 index 0000000..e0879c8 --- /dev/null +++ b/2023/day8/README.md @@ -0,0 +1,49 @@ +# Day 8: Haunted Wasteland + +## Part 1 + +You're still riding a camel across Desert Island when you spot a sandstorm quickly approaching. When you turn to warn +the Elf, she disappears before your eyes! To be fair, she had just finished warning you about **ghosts** a few minutes +ago. + +One of the camel's pouches is labeled "maps" - sure enough, it's full of documents (your puzzle input) about how to +navigate the desert. At least, you're pretty sure that's what they are; one of the documents contains a list of +left/right instructions, and the rest of the documents seem to describe some kind of **network** of labeled nodes. + +It seems like you're meant to use the **left/right** instructions to **navigate the network**. Perhaps if you have the +camel follow the same instructions, you can escape the haunted wasteland! + +After examining the maps for a bit, two nodes stick out: `AAA` and `ZZZ`. You feel like `AAA` is where you are now, +and you have to follow the left/right instructions until you reach `ZZZ`. + +This format defines each **node** of the network individually. For example: + +``` +RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ) +``` + +Starting with `AAA`, you need to **look up the next element** based on the next left/right instruction in your input. +In this example, start with `AAA` and go **right** (R) by choosing the right element of `AAA`, **CCC**. Then, `L` means +to choose the **left** element of `CCC`, **ZZZ**. By following the left/right instructions, you reach `ZZZ` in 2 steps. + +Of course, you might not find `ZZZ` right away. If you run out of left/right instructions, repeat the whole sequence of +instructions as necessary: `R`L really means `RLRLRLRLRLRLRLRL...` and so on. For example, here is a situation that +takes `6` steps to reach `ZZZ`: + +``` +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ) +``` + +Starting at AAA, follow the left/right instructions. **How many steps are required to reach ZZZ**? diff --git a/2023/day8/src/example-input.txt b/2023/day8/src/example-input.txt new file mode 100644 index 0000000..34ffa8a --- /dev/null +++ b/2023/day8/src/example-input.txt @@ -0,0 +1,5 @@ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ) \ No newline at end of file diff --git a/2023/day8/src/input.txt b/2023/day8/src/input.txt new file mode 100644 index 0000000..ac178d0 --- /dev/null +++ b/2023/day8/src/input.txt @@ -0,0 +1,796 @@ +LRRLRLRRRLLRLRRRLRLLRLRLRRLRLRRLRRLRLRLLRRRLRRLLRRRLRRLRRRLRRLRLRLLRRLRLRRLLRRRLLLRRRLLLRRLRLRRLRLLRRRLRRLRRRLRRLLRRRLRRRLRRRLRLRRLRLRRRLRRRLRRLRLRRLLRRRLRRLLRRLRRLRLRLRRRLRLLRRRLRRLRRRLLRRLLLLLRRRLRRLLLRRRLRRRLRRLRLLLLLRLRRRLRRRLRLRRLLLLRLRRRLLRRRLRRRLRLRLRRLRRLRRLRLRLLLRLRRLRRLRRRLRRRLLRRRR + +RBX = (TMF, KTP) +RBP = (MKS, MKS) +CGR = (XDR, VDX) +KBX = (DGP, JKM) +BNJ = (NJC, QRL) +BXQ = (DKX, CJQ) +NDX = (PHJ, TRQ) +KFQ = (KXN, GBQ) +LRF = (CTS, MTT) +RTN = (VMK, JTK) +QKP = (FDS, SFG) +BVB = (BHS, VRQ) +NBT = (BPJ, DFF) +HQM = (QMV, XVG) +XMR = (RKH, NJQ) +PHC = (XHP, DKM) +HTX = (BXK, BQP) +TPJ = (TNQ, QDX) +RDJ = (KHB, MFG) +XTF = (PPR, FHG) +DVA = (DBM, MVD) +HFP = (CXV, VCD) +HPS = (GTD, NBL) +LBK = (FVN, LHX) +KKS = (FKM, LHN) +FJV = (LVN, PLP) +MLH = (SFR, RJK) +SVQ = (RCS, FFD) +PTB = (TMC, QCR) +RFL = (RXP, VRL) +PTH = (XTS, GFG) +QNM = (TJR, MHX) +BJT = (PJG, SHG) +JJD = (FMJ, QVV) +GMX = (CLD, QRM) +DGZ = (BSL, BRS) +BVM = (SFC, JGD) +GNQ = (PLP, LVN) +XPG = (VLH, BCM) +PND = (RXV, VGM) +RNV = (GVL, MTB) +JSG = (QVP, XCX) +QHP = (KQG, BML) +XBK = (NPP, CRN) +QRL = (RQH, BGH) +HDJ = (VRQ, BHS) +XPD = (HSB, LRC) +KJK = (XCV, MDG) +NJB = (RKL, SHQ) +MCQ = (XFT, MRQ) +QRC = (XVS, CSN) +FCT = (DFF, BPJ) +MTF = (PQT, CNQ) +RXG = (QVP, XCX) +MVZ = (QRC, GHJ) +RKL = (QQM, BFT) +VRT = (GMJ, BDK) +BNS = (HQV, HPS) +JSQ = (HTC, PGD) +BHB = (VGQ, BTM) +NLP = (RKB, XQB) +FGF = (CVK, VRT) +FPM = (GDS, LTG) +BCX = (NDX, GSL) +GVL = (VCX, PSP) +KRM = (MHK, JCX) +GLJ = (XNX, RKP) +LBV = (FDC, TPJ) +GSP = (KMC, LQR) +TFR = (HXJ, MFJ) +DLX = (FNK, GLJ) +DKX = (XSR, LHP) +SVR = (KPX, QTM) +JMT = (GXV, RTX) +XSX = (HHD, HHD) +RJF = (BRH, HGP) +BDK = (TGX, HKP) +TFF = (DSX, DSX) +TJR = (BKB, LCB) +BDQ = (CLD, QRM) +NTC = (BQL, BQL) +CVS = (RQC, FFV) +BML = (VNT, JRH) +CBK = (TGB, RTN) +HQV = (GTD, NBL) +PDT = (SRQ, MLD) +RXV = (KJK, FCD) +XTS = (SCG, KQJ) +PXG = (LKG, MTQ) +PCX = (FKJ, FCV) +HKR = (LMK, BJR) +CKK = (XSX, XSX) +TNP = (TDH, JPR) +PCH = (RBP, PQH) +KHB = (RQR, BXQ) +CJZ = (CNC, HJX) +JPN = (FRP, QTB) +RKP = (NKH, FQC) +BDP = (GCM, VKT) +HQX = (TQF, NJB) +PFV = (FPM, VNP) +NFQ = (GFX, GXH) +STX = (MCQ, DNS) +CKX = (CFS, BNS) +NRJ = (SBG, TLK) +DHS = (VBH, VMT) +TVL = (BPQ, NBM) +QTF = (MPS, RJB) +FHV = (LMK, BJR) +JCV = (JSQ, STQ) +RGX = (GVL, MTB) +CTS = (PCB, GJV) +RCS = (TNT, XTF) +PCS = (NJH, RLX) +KNG = (CHP, FNN) +FJL = (KKH, QGQ) +CPK = (KTX, CGR) +BLF = (PCQ, TKN) +FCR = (STF, SVQ) +BSL = (GFJ, CDL) +FKS = (GRG, JSB) +FCM = (LFX, RJF) +XLK = (CDP, CBD) +KNB = (VGQ, BTM) +KGC = (TJR, MHX) +JHM = (JSP, XDN) +HFN = (STF, SVQ) +GCM = (CKX, KPS) +CGV = (MHK, JCX) +TQP = (JRM, XSB) +GSM = (FJV, GNQ) +KBL = (DLX, BGD) +CBC = (MQK, RRF) +NJM = (KLQ, BVL) +THG = (CPG, PND) +DXP = (HDJ, BVB) +DTT = (XDN, JSP) +PLP = (BKF, FGP) +MSZ = (MVD, DBM) +BQC = (NPL, VXF) +CGB = (LRM, MTH) +RLX = (DBT, RJN) +RXP = (HFP, JNS) +TTH = (QNF, KNG) +LHS = (QFR, GTN) +NBL = (THT, VTQ) +DRS = (PJH, STX) +PSP = (CPK, QKF) +PPR = (LQK, VSN) +QHT = (RLG, TDG) +FVM = (MLR, HXF) +DHB = (CQC, BKK) +MVP = (GHJ, QRC) +TDG = (RVX, CDB) +LKN = (HXF, MLR) +CNR = (KCV, CDR) +SMC = (BBR, BMS) +QDP = (LXV, VPS) +LLD = (CGC, NGQ) +KXN = (PRR, PSR) +LRC = (SHB, PBK) +SQD = (LBV, CRP) +QTB = (KNS, JTS) +MTT = (GJV, PCB) +GMV = (JNH, MJF) +DTG = (SQD, KVV) +RTV = (DSB, SJB) +CDL = (FTQ, CPN) +JHR = (JRM, XSB) +BFP = (PFG, MTD) +DTB = (NDX, GSL) +SQM = (CXL, NBD) +NBP = (KBN, LQH) +BTC = (LJT, TCV) +PQT = (PXG, BSF) +QDH = (FPM, VNP) +VPT = (PRT, VHJ) +VKC = (GXK, QSB) +NXB = (QKP, TGK) +FDF = (VRT, CVK) +NTG = (FRV, BQR) +RKB = (PHC, JSD) +MHV = (XXL, THG) +PHV = (SNM, TFR) +XRK = (JNN, LKR) +XVS = (CBK, NNM) +GDQ = (BDQ, GMX) +TKR = (CKJ, LVQ) +XCX = (LSP, FNB) +JCX = (FGF, FDF) +MFJ = (DBB, HBH) +BQP = (HRT, DQD) +VNC = (BDQ, GMX) +SSC = (LQH, KBN) +TJD = (QFV, KRS) +NKL = (CKK, DDC) +NTM = (QHG, MCC) +JBQ = (HXV, JPN) +DGP = (FVM, LKN) +SJB = (LRQ, DMG) +LJT = (MMT, MDL) +RFC = (MVM, JXP) +FFV = (TPG, QMD) +PJH = (DNS, MCQ) +VCD = (FHV, HKR) +MPS = (DHH, LPT) +VHJ = (RGX, RNV) +FLT = (FRF, NHG) +DSF = (SDV, VKC) +HJQ = (CDR, KCV) +BQR = (PDX, PMX) +CRJ = (BPN, KKS) +MQN = (PNN, DHB) +KCC = (KMC, LQR) +BTH = (VLH, BCM) +KNS = (GGM, SJS) +SCG = (MBF, RDR) +VDQ = (JNN, LKR) +XFT = (VPT, FLH) +RDR = (GTF, GJR) +CXS = (RRF, MQK) +XDN = (SSF, VGC) +JPR = (VGX, TCF) +BTF = (LKM, GBG) +GCJ = (VPS, LXV) +FGJ = (XVG, QMV) +MBF = (GJR, GTF) +TPG = (TND, QSF) +BNR = (BHG, SMC) +GJV = (FLB, PCX) +LQF = (BJT, KTN) +VSB = (JVB, FVK) +NHB = (HQX, LLQ) +CKC = (PJD, TRS) +JVB = (QQV, NRS) +BKK = (LSR, LJN) +SBV = (GTN, QFR) +JMK = (MFG, KHB) +LPX = (NBP, SSC) +KLJ = (CDX, VTL) +KQJ = (RDR, MBF) +VMK = (JXJ, BMT) +TRS = (LQM, JJD) +KCM = (XBD, XMH) +JCT = (NFQ, NDJ) +FGM = (HMK, PGQ) +QKK = (RLS, LPX) +QJD = (PDT, RPQ) +LHP = (PLN, MQN) +NPL = (TFF, TFF) +XHP = (PJQ, KBL) +HMK = (JNV, XPS) +KMC = (VCG, NTG) +QDX = (TMM, HHM) +VNP = (LTG, GDS) +PDR = (CDX, VTL) +HVK = (BQC, SCN) +QMV = (BNJ, CRL) +DDR = (KRS, QFV) +RDG = (QHP, QXN) +CRL = (NJC, QRL) +SHB = (SQX, JVV) +TSP = (BPX, FVC) +RRF = (KJB, PTB) +CSG = (FRX, RTP) +RPQ = (SRQ, MLD) +VXS = (MTN, DJQ) +PJK = (NGD, NTM) +QHG = (NXT, DHS) +LSP = (MTR, GGH) +SHN = (JJS, RVC) +TNQ = (TMM, HHM) +NPK = (GCM, VKT) +MJF = (NBT, FCT) +JNN = (PTH, HFK) +NRS = (CLT, HGV) +HSX = (BPX, FVC) +MVM = (DSF, NQS) +PBK = (SQX, JVV) +HHD = (DBM, MVD) +MTD = (DVB, BTX) +PXP = (NPP, CRN) +TMF = (LPF, QPD) +BRT = (FJV, GNQ) +MFQ = (BHG, SMC) +DJQ = (MVP, MVZ) +LKR = (PTH, HFK) +STQ = (HTC, PGD) +DXF = (JBX, TRT) +RJN = (PDR, KLJ) +CGC = (KKL, RJV) +SRG = (QKK, LND) +MCC = (DHS, NXT) +JMG = (RLX, NJH) +NKH = (DGF, FJL) +JXP = (DSF, NQS) +GXX = (CKC, MFL) +MXD = (KBC, FGK) +SCH = (XPG, BTH) +XSR = (MQN, PLN) +KKD = (NBD, CXL) +CLJ = (GCN, GCN) +QVV = (THK, LQV) +VLH = (FFB, STD) +RTP = (CBC, CXS) +RVX = (BLF, PTM) +KBN = (JNF, BTC) +PFX = (JHM, DTT) +NVL = (KCM, DLK) +QRM = (CQK, RBB) +HKP = (SHN, TMV) +VXF = (TFF, JRP) +LDL = (LHM, JMT) +BQL = (DRS, VQQ) +FDC = (TNQ, QDX) +MMT = (MSL, JCT) +CXV = (HKR, FHV) +BKB = (FCM, QTC) +HRQ = (RXG, JSG) +RKH = (JFF, TKR) +VQR = (JGD, SFC) +KCV = (LNG, NCV) +XSB = (DTG, DFK) +JGD = (HLP, FNS) +VCK = (RXP, VRL) +GTN = (BCP, CKB) +QDK = (CBD, CDP) +SNF = (JNH, MJF) +FVK = (QQV, NRS) +HHM = (VXK, PJK) +JRM = (DTG, DFK) +QPL = (XLK, QDK) +SCV = (QHP, QXN) +RQC = (QMD, TPG) +LFX = (BRH, HGP) +FRX = (CXS, CBC) +BTM = (FPK, JVF) +BPN = (LHN, FKM) +LRQ = (LSQ, PMP) +TPV = (LQF, KDN) +XBN = (QDS, DXP) +RBB = (FCR, HFN) +XSM = (LNT, STB) +SBR = (PQT, CNQ) +LVM = (SBV, LHS) +KBC = (JHR, TQP) +DLJ = (FGK, KBC) +LKG = (VJX, HLQ) +NXT = (VMT, VBH) +CBD = (HGN, HBG) +BHS = (VTF, MRV) +BTP = (GBG, LKM) +DSM = (CKK, CKK) +LSR = (CMB, NLP) +NPP = (BCX, DTB) +XCV = (SMS, QJD) +PNM = (BPQ, NBM) +VPS = (JJB, XMR) +JNH = (FCT, NBT) +KTN = (SHG, PJG) +FMJ = (LQV, THK) +GFX = (NBB, NBB) +SRQ = (GNS, QGB) +FQC = (DGF, FJL) +MPA = (BRS, BSL) +BMS = (FKS, SKF) +BMT = (JMG, PCS) +QFV = (SCH, FDJ) +STF = (RCS, FFD) +JTS = (GGM, SJS) +NJQ = (TKR, JFF) +MTQ = (HLQ, VJX) +FGK = (TQP, JHR) +JNF = (TCV, LJT) +LKM = (KGC, QNM) +MXV = (BQP, BXK) +FRF = (LVM, HGK) +GGH = (BHB, KNB) +BGH = (KBX, FDN) +LQH = (JNF, BTC) +JVF = (LDS, DCD) +CLT = (QPL, LFV) +HRV = (TRD, RXH) +NBD = (PXP, XBK) +PJD = (JJD, LQM) +XMV = (BBF, LKX) +BCN = (PBH, HPL) +XMH = (JNG, GCX) +HFK = (GFG, XTS) +VTL = (RFC, VPF) +MSL = (NFQ, NDJ) +THH = (KNG, QNF) +PCB = (PCX, FLB) +GBQ = (PRR, PSR) +NMS = (BQC, SCN) +PDX = (BGP, SFH) +LDS = (TLJ, LLD) +HGV = (QPL, LFV) +LMK = (VKX, JCV) +NBM = (PKR, QCS) +NLR = (LKJ, DGZ) +QDN = (TRT, JBX) +MTH = (HMT, NJM) +CKF = (LKJ, LKJ) +FDN = (DGP, JKM) +CDR = (NCV, LNG) +SGJ = (GBQ, KXN) +TMM = (VXK, PJK) +JSL = (CTS, MTT) +QGB = (TBK, NHB) +TDA = (GHJ, QRC) +GTF = (QDP, GCJ) +TCG = (FVK, JVB) +LXJ = (FVN, LHX) +TQF = (RKL, SHQ) +ZZZ = (VQQ, DRS) +NCV = (CRJ, GBL) +BKF = (CJP, PRL) +QPD = (DQN, JBQ) +MFL = (PJD, TRS) +NHG = (HGK, LVM) +MRV = (MLH, TBV) +XHS = (PSN, HRQ) +JJB = (NJQ, RKH) +TGK = (SFG, FDS) +CGS = (TRD, RXH) +GCN = (NCN, CVS) +PNN = (BKK, CQC) +LSQ = (KCC, GSP) +LND = (RLS, LPX) +CPG = (VGM, RXV) +GSF = (HMJ, RMH) +JRH = (VQR, BVM) +FJB = (DTT, JHM) +FKM = (BFK, XCQ) +LPT = (PRD, CGB) +XVG = (CRL, BNJ) +KRS = (FDJ, SCH) +BNL = (BQL, ZZZ) +JMB = (XVD, MBH) +KKH = (RLN, PHV) +MTB = (PSP, VCX) +NCN = (FFV, RQC) +FVC = (QJC, SRG) +FTS = (THG, XXL) +SFG = (BCN, DBN) +RXH = (XQT, SDR) +MLR = (GDQ, VNC) +TRD = (XQT, SDR) +BGD = (GLJ, FNK) +XDJ = (MPS, RJB) +JMJ = (MDJ, RTV) +RVC = (PXQ, NRJ) +QQV = (CLT, HGV) +MLD = (QGB, GNS) +KPH = (HRQ, PSN) +RLN = (TFR, SNM) +JRP = (DSX, HPD) +SJS = (DSM, NKL) +PGD = (CGS, HRV) +HPL = (GKH, CVN) +MQK = (PTB, KJB) +QCR = (MHV, FTS) +VCG = (BQR, FRV) +LXV = (XMR, JJB) +PKR = (QSH, XMV) +JSB = (DLJ, MXD) +VRL = (JNS, HFP) +PHJ = (BTF, BTP) +CXL = (XBK, PXP) +FNN = (TSP, HSX) +XQT = (VHH, QHT) +LQV = (DQK, JMB) +RLS = (NBP, SSC) +TCV = (MDL, MMT) +QTC = (LFX, RJF) +LRK = (MFL, CKC) +MHK = (FGF, FDF) +RLJ = (HJX, CNC) +TLK = (RFL, VCK) +CNC = (HJQ, CNR) +LQL = (FRX, RTP) +QQM = (MFQ, BNR) +VBH = (HLB, JMJ) +GKX = (TGK, QKP) +KQG = (JRH, VNT) +XDR = (DNF, KJV) +GMJ = (HKP, TGX) +VRQ = (VTF, MRV) +HGP = (VNB, LDL) +LNG = (GBL, CRJ) +BSF = (LKG, MTQ) +BRH = (VNB, LDL) +PJQ = (BGD, DLX) +QKF = (CGR, KTX) +QMD = (TND, QSF) +PQH = (MKS, STP) +FRV = (PDX, PMX) +LPF = (JBQ, DQN) +CQM = (TDH, JPR) +HJX = (CNR, HJQ) +AAA = (DRS, VQQ) +TDH = (VGX, TCF) +TGX = (TMV, SHN) +BDH = (XFV, GSF) +GXK = (BFP, TFH) +BFT = (MFQ, BNR) +NGD = (QHG, MCC) +FPK = (DCD, LDS) +MTR = (KNB, BHB) +SNM = (MFJ, HXJ) +HLP = (RHQ, SVR) +JSP = (VGC, SSF) +NJH = (RJN, DBT) +SKF = (GRG, JSB) +PTM = (TKN, PCQ) +QGQ = (PHV, RLN) +SSF = (NVL, JPB) +PCQ = (LTC, PCH) +KJV = (GMS, XGF) +JSD = (XHP, DKM) +RRB = (LQF, KDN) +GJJ = (FLT, BNV) +FJA = (HJX, CNC) +HLQ = (XRK, VDQ) +KTP = (LPF, QPD) +PRD = (LRM, MTH) +QNF = (CHP, FNN) +DQK = (MBH, XVD) +TMS = (CLJ, XML) +QVP = (FNB, LSP) +BPX = (QJC, SRG) +QXN = (KQG, BML) +VGQ = (JVF, FPK) +BRS = (CDL, GFJ) +NQS = (VKC, SDV) +LFV = (XLK, QDK) +SMS = (RPQ, PDT) +BBF = (CGV, KRM) +VMT = (HLB, JMJ) +QSF = (JSL, LRF) +CJQ = (XSR, LHP) +RDF = (KKD, SQM) +MKS = (CKF, CKF) +STD = (MNM, TMS) +DNS = (MRQ, XFT) +MFG = (RQR, BXQ) +GBG = (QNM, KGC) +JNG = (RDG, SCV) +CVN = (GJJ, QVC) +HMT = (BVL, KLQ) +VTQ = (NTC, BNL) +TGB = (JTK, VMK) +PSR = (NPK, BDP) +MDJ = (DSB, SJB) +CKB = (VSB, TCG) +DNF = (GMS, XGF) +LQK = (XFR, RDF) +RHQ = (KPX, QTM) +VKX = (STQ, JSQ) +BRX = (DDR, TJD) +LJN = (NLP, CMB) +DJR = (HMK, PGQ) +VNB = (JMT, LHM) +DBM = (SBR, MTF) +KLQ = (XBN, MNH) +MDL = (MSL, JCT) +SQX = (TPV, RRB) +CLD = (CQK, RBB) +HPD = (RLJ, CJZ) +LHN = (BFK, XCQ) +KJB = (TMC, QCR) +RLG = (RVX, CDB) +PBH = (GKH, CVN) +DMB = (HHD, MSZ) +SDV = (GXK, QSB) +KPX = (XDJ, QTF) +NBB = (MTN, MTN) +BTX = (BRT, GSM) +HGN = (HJT, RBX) +GDS = (XSM, BGG) +KDN = (KTN, BJT) +GSL = (TRQ, PHJ) +SFR = (XHS, KPH) +JNV = (TVL, PNM) +JNS = (VCD, CXV) +LHM = (GXV, RTX) +TFH = (MTD, PFG) +GHJ = (CSN, XVS) +KNQ = (LRK, GXX) +PRT = (RNV, RGX) +JTK = (BMT, JXJ) +MRQ = (VPT, FLH) +TKN = (LTC, PCH) +VSN = (XFR, RDF) +HXJ = (DBB, HBH) +PRL = (THH, TTH) +GCX = (RDG, SCV) +MNH = (DXP, QDS) +CMB = (XQB, RKB) +CHP = (TSP, HSX) +BCP = (TCG, VSB) +LNT = (HQM, FGJ) +FNK = (XNX, RKP) +HRT = (FJB, PFX) +HTC = (CGS, HRV) +SDR = (QHT, VHH) +CQK = (FCR, HFN) +LVN = (FGP, BKF) +FDS = (DBN, BCN) +QCS = (QSH, XMV) +TRT = (KFQ, SGJ) +RQH = (FDN, KBX) +QJC = (QKK, LND) +MVD = (SBR, MTF) +SBG = (RFL, VCK) +DSX = (RLJ, RLJ) +JBX = (KFQ, SGJ) +JVV = (RRB, TPV) +BBR = (FKS, SKF) +KTX = (VDX, XDR) +CSN = (NNM, CBK) +NNM = (RTN, TGB) +VXK = (NGD, NTM) +DQD = (FJB, PFX) +CDB = (BLF, PTM) +GBL = (BPN, KKS) +BGP = (SNT, BRX) +CRP = (FDC, TPJ) +DFK = (KVV, SQD) +LVQ = (KNQ, FBX) +HSB = (PBK, SHB) +PJG = (TNP, CQM) +JPB = (KCM, DLK) +JJS = (PXQ, NRJ) +XGF = (QDH, PFV) +DKM = (KBL, PJQ) +DVB = (GSM, BRT) +TMC = (MHV, FTS) +TRQ = (BTP, BTF) +FLB = (FCV, FKJ) +BVL = (MNH, XBN) +XPA = (NCN, CVS) +XPS = (PNM, TVL) +CPN = (RDJ, JMK) +RQR = (DKX, CJQ) +SFH = (SNT, BRX) +DBT = (KLJ, PDR) +MBH = (XCS, XPD) +DLK = (XMH, XBD) +HBG = (HJT, RBX) +THK = (DQK, JMB) +DGF = (KKH, QGQ) +BPJ = (GKX, NXB) +MTN = (MVP, MVP) +TMV = (RVC, JJS) +GMS = (PFV, QDH) +FDJ = (BTH, XPG) +FFB = (MNM, MNM) +RTX = (LBK, LXJ) +BGG = (LNT, STB) +LRM = (HMT, NJM) +XVD = (XPD, XCS) +THT = (NTC, NTC) +GXH = (NBB, VXS) +LTG = (XSM, BGG) +GKH = (GJJ, QVC) +TNT = (FHG, PPR) +CDP = (HGN, HBG) +DQN = (HXV, JPN) +FNS = (SVR, RHQ) +DSB = (DMG, LRQ) +DCD = (TLJ, LLD) +FTQ = (JMK, RDJ) +VGX = (SFN, BDH) +VDX = (KJV, DNF) +PMX = (SFH, BGP) +VTF = (TBV, MLH) +LQM = (FMJ, QVV) +VNT = (VQR, BVM) +XML = (GCN, QFZ) +VPF = (MVM, JXP) +TBK = (LLQ, HQX) +PXQ = (TLK, SBG) +LKX = (CGV, KRM) +DBN = (PBH, HPL) +XXL = (CPG, PND) +BHG = (BBR, BMS) +VQQ = (STX, PJH) +VHH = (RLG, TDG) +RJK = (KPH, XHS) +BFK = (MXV, HTX) +VJX = (XRK, VDQ) +GFJ = (CPN, FTQ) +GRG = (MXD, DLJ) +CQC = (LJN, LSR) +RJB = (DHH, LPT) +FGP = (CJP, PRL) +BCM = (FFB, STD) +FBX = (GXX, LRK) +FLH = (VHJ, PRT) +GTD = (THT, THT) +GNS = (NHB, TBK) +QTM = (XDJ, QTF) +VGM = (KJK, FCD) +XBD = (JNG, GCX) +QVC = (BNV, FLT) +FCV = (LQL, CSG) +SCN = (NPL, VXF) +KKL = (HVK, NMS) +MDG = (QJD, SMS) +QSB = (BFP, TFH) +LKJ = (BRS, BSL) +XCS = (HSB, LRC) +HLB = (MDJ, RTV) +TLJ = (NGQ, CGC) +MNM = (CLJ, CLJ) +STB = (HQM, FGJ) +HJT = (TMF, KTP) +TND = (LRF, JSL) +FVN = (QDN, DXF) +SNT = (TJD, DDR) +JKM = (LKN, FVM) +LTC = (RBP, PQH) +STP = (CKF, NLR) +DMG = (LSQ, PMP) +CDX = (RFC, VPF) +CNQ = (PXG, BSF) +PMP = (KCC, GSP) +VKT = (CKX, KPS) +CKJ = (FBX, KNQ) +VGC = (JPB, NVL) +LCB = (QTC, FCM) +TCF = (BDH, SFN) +DDC = (XSX, DMB) +SFN = (XFV, GSF) +HXF = (GDQ, VNC) +BPQ = (QCS, PKR) +FFD = (TNT, XTF) +TBV = (SFR, RJK) +DBB = (FGM, DJR) +FHG = (VSN, LQK) +GGM = (DSM, NKL) +RJV = (NMS, HVK) +NGQ = (RJV, KKL) +CFS = (HQV, HPS) +PFG = (BTX, DVB) +VCX = (QKF, CPK) +XCQ = (MXV, HTX) +DHH = (PRD, CGB) +HBH = (DJR, FGM) +RMH = (GMV, SNF) +HXV = (QTB, FRP) +QFZ = (CVS, NCN) +CVK = (BDK, GMJ) +SHQ = (QQM, BFT) +PGQ = (XPS, JNV) +LQR = (NTG, VCG) +QDS = (BVB, HDJ) +CJP = (THH, TTH) +FNB = (GGH, MTR) +PSN = (RXG, JSG) +LHX = (QDN, DXF) +GFG = (SCG, KQJ) +PRR = (NPK, BDP) +QSH = (LKX, BBF) +KVV = (LBV, CRP) +DFF = (NXB, GKX) +JFF = (CKJ, LVQ) +FCD = (MDG, XCV) +SFC = (FNS, HLP) +KPS = (BNS, CFS) +PLN = (PNN, DHB) +SHG = (CQM, TNP) +GJR = (QDP, GCJ) +NDJ = (GFX, GXH) +XQB = (PHC, JSD) +FKJ = (CSG, LQL) +FRP = (KNS, JTS) +MHX = (BKB, LCB) +HGK = (LHS, SBV) +JXJ = (JMG, PCS) +NJC = (RQH, BGH) +LLQ = (TQF, NJB) +QFR = (BCP, CKB) +BXK = (DQD, HRT) +XFR = (KKD, SQM) +XFV = (HMJ, RMH) +HMJ = (GMV, SNF) +BNV = (NHG, FRF) +GXV = (LBK, LXJ) +XNX = (NKH, FQC) +BJR = (VKX, JCV) +CRN = (BCX, DTB) diff --git a/2023/day8/src/main.rs b/2023/day8/src/main.rs new file mode 100644 index 0000000..39d5290 --- /dev/null +++ b/2023/day8/src/main.rs @@ -0,0 +1,112 @@ +use crate::Direction::{Left, Right}; +use std::collections::HashMap; + +const FILE_STR: &str = include_str!("input.txt"); +const START_LOC: &str = "AAA"; +const END_LOC: &str = "ZZZ"; + +#[derive(Debug, Copy, Clone)] +enum Direction { + Left, + Right, +} + +impl From for Direction { + fn from(value: char) -> Self { + match value { + 'L' => Left, + 'R' => Right, + _ => panic!("Invalid direction char"), + } + } +} + +type DirectionMap = HashMap; + +#[derive(Debug)] +struct Network { + direction_list: Vec, + map: DirectionMap, +} + +impl Network { + fn parse(input: &str) -> Self { + let mut lines = input.split('\n'); + let direction_list: Vec = lines + .next() + .unwrap() + .trim() + .chars() + .map(Direction::from) + .collect(); + + let map = lines + .skip(1) + .filter(|l| !l.is_empty()) + .map(|l| { + let parts: Vec<_> = l.split(" = ").collect(); + let key = parts[0].trim(); + + let raw_dirs = parts[1].replace(['(', ')'], ""); + let dirs: Vec<_> = raw_dirs.split(", ").collect(); + let left = dirs[0].trim(); + let right = dirs[1].trim(); + + (String::from(key), (String::from(left), String::from(right))) + }) + .collect::(); + + Network { + direction_list, + map, + } + } + + fn count_steps(&self) -> usize { + let mut count = 0usize; + let mut next_key = START_LOC; + let mut current = self.map.get(next_key).unwrap(); + + loop { + for x in self.direction_list.iter() { + count += 1; + next_key = match x { + Direction::Left => ¤t.0, + Direction::Right => ¤t.1, + }; + + if next_key == END_LOC { + return count; + } + + current = self + .map + .get(next_key) + .unwrap_or_else(|| panic!("Unable to find key {} in map", next_key)); + } + } + } +} + +fn part_one() { + let network = Network::parse(FILE_STR); + let step_count = network.count_steps(); + + println!("Part 1: Step count from AAA to ZZZ {}", step_count); +} + +fn main() { + part_one(); +} + +#[cfg(test)] +mod tests { + const EXAMPLE_FILE_STR: &str = include_str!("example-input.txt"); + use super::*; + + #[test] + fn first_example() { + let network = Network::parse(EXAMPLE_FILE_STR); + assert_eq!(6, network.count_steps()); + } +}