From 51f9ac4bfcb1c69480a7013e9acca319620df3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Thu, 8 Feb 2024 13:58:09 +0100 Subject: [PATCH] feat: add day 8 - part 1 --- Cargo.lock | 4 + Cargo.toml | 2 +- day_8/.gitignore | 1 + day_8/Cargo.toml | 5 + day_8/README.md | 41 +++ day_8/input.txt | 732 ++++++++++++++++++++++++++++++++++++++ day_8/input_example_1.txt | 9 + day_8/input_example_2.txt | 5 + day_8/src/lib.rs | 146 ++++++++ day_8/src/main.rs | 8 + 10 files changed, 952 insertions(+), 1 deletion(-) create mode 100644 day_8/.gitignore create mode 100644 day_8/Cargo.toml create mode 100644 day_8/README.md create mode 100644 day_8/input.txt create mode 100644 day_8/input_example_1.txt create mode 100644 day_8/input_example_2.txt create mode 100644 day_8/src/lib.rs create mode 100644 day_8/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index ed99b90..523538e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,6 +104,10 @@ dependencies = [ "array-init", ] +[[package]] +name = "day_8" +version = "1.0.0" + [[package]] name = "either" version = "1.9.0" diff --git a/Cargo.toml b/Cargo.toml index 0ebe775..5595542 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["day_*"] resolver = "2" [workspace.package] -name = "advent_of_code_2023" +# name = "advent_of_code_2023" version = "1.0.0" edition = "2021" rust-version = "1.74.0" diff --git a/day_8/.gitignore b/day_8/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/day_8/.gitignore @@ -0,0 +1 @@ +/target diff --git a/day_8/Cargo.toml b/day_8/Cargo.toml new file mode 100644 index 0000000..b8f544b --- /dev/null +++ b/day_8/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "day_8" +version.workspace = true +edition.workspace = true +rust-version.workspace = true diff --git a/day_8/README.md b/day_8/README.md new file mode 100644 index 0000000..84bd1ad --- /dev/null +++ b/day_8/README.md @@ -0,0 +1,41 @@ +# - Day 8: Haunted Wasteland - + +Source: + +## Instructions - 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: + +```txt +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: `RL` really means `RLRLRLRLRLRLRLRL...` and so on. For example, here is a situation that takes **`6`** steps to reach `ZZZ`: + +```txt +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/day_8/input.txt b/day_8/input.txt new file mode 100644 index 0000000..821448b --- /dev/null +++ b/day_8/input.txt @@ -0,0 +1,732 @@ +LLLLLLLRRRLRRRLRLRLRLRRLLRRRLRLLRRRLLRRLRRLRRLRLRRLRLRRRLRRLRLRRRLRRLRRLRLRRLLRLLRLRRRLRRLRLLLLRRLLLLRLRLRLRRRLRLRLLLRLRRRLRRRLRRRLRLRRLRRRLRLLLRLLRRLRRRLRRLRRLRRLRLRRRLRLRLRLLRRRLRRRLRRLRRRLLLRRLRRLRRRLRLRRRLRRRLRLRRLRRRLRLRRLRLRRLRRRLRLRRLRLLRRRLLRLRRLRRRLLLRLRRLRRRR + +DGK = (KVQ, XHR) +KTC = (TVB, MTH) +CGR = (VVK, BKP) +LCG = (FQC, KHX) +PSZ = (FSF, QSM) +FBJ = (FHP, SPX) +KJD = (NRQ, VDH) +NCM = (JPJ, KNG) +TXH = (HNK, VHQ) +NND = (TRC, DFM) +JQN = (CNX, XLD) +RHB = (CDG, GBT) +JBN = (PXV, GVN) +DFC = (JRN, TXH) +TXG = (CHT, VBL) +XXQ = (JDC, JGV) +SVF = (FVD, LHQ) +FVK = (LCG, RNB) +XKT = (MPF, XJJ) +MHB = (JSJ, VQM) +FVC = (HXF, VVN) +JJR = (VNS, SLM) +RMT = (GDS, XHP) +CHT = (PXS, VLF) +SFJ = (XGC, LPM) +BJL = (XDN, VXN) +PQK = (NHS, DVB) +PDB = (JPQ, TVJ) +RGL = (DNN, NCN) +KRN = (SBL, PHL) +MTF = (PJL, KQR) +BTL = (CCF, LDP) +NLV = (CPM, HVL) +GCQ = (QMF, JVH) +KVH = (VKD, PQG) +RLB = (GSS, JVP) +QRB = (TNL, DKN) +JFV = (RDR, NSB) +BFC = (LGH, XLX) +HGQ = (SLM, VNS) +FQC = (VFQ, BXC) +DDS = (XHR, KVQ) +VQV = (SFT, BFQ) +XFD = (HVV, FLH) +TVP = (XQF, KSS) +GBH = (NPX, LDB) +KHL = (MGS, MMD) +NPX = (BJL, SFF) +VMG = (DHX, GVC) +RTJ = (XRF, BPK) +TLM = (NCG, QBB) +LXS = (TVB, MTH) +XNM = (QFL, KQK) +KQR = (QRD, JBN) +JQD = (DNN, NCN) +QCF = (MXL, MXL) +QMH = (NKG, SDJ) +NKK = (MCB, RLB) +MPJ = (BTL, JTF) +TLS = (VPJ, LMD) +XJB = (LML, TKZ) +HGF = (HBF, QHB) +KNJ = (QKM, XLR) +XCF = (QCJ, HTN) +HFS = (NKC, JFV) +QLS = (QBB, NCG) +QFL = (NXQ, QBN) +MTH = (FLN, LQR) +VND = (KMM, MQH) +NQQ = (VVH, NDL) +BTV = (QSM, FSF) +SLT = (NRQ, VDH) +NKG = (TBV, XCV) +SLM = (LBN, HPK) +CMQ = (KKS, VBH) +JTF = (LDP, CCF) +VFC = (GKH, KPS) +KCC = (JVM, MTF) +KFP = (MVX, NMX) +NQF = (QSK, KCC) +GGC = (VBV, TJR) +QQS = (NKK, NQC) +LXK = (FXC, QBC) +DVQ = (TFF, LKB) +PBL = (BGQ, FHQ) +KHV = (BVJ, XSD) +LDB = (SFF, BJL) +RJG = (LJB, CJM) +RCX = (QTB, PPL) +FLH = (HSX, KVX) +XTV = (HST, VCC) +GDC = (CMH, NCT) +RDP = (FKR, GHJ) +NXM = (PVV, KRX) +SCQ = (MFN, GRB) +MRT = (XGT, VLK) +DHJ = (XHP, GDS) +XFL = (TJK, QMH) +XQF = (CCQ, RPH) +CLV = (CKD, CCD) +CMT = (NCD, XDM) +NCT = (SXG, CLF) +JSS = (GMJ, LJC) +TLQ = (CRC, DXQ) +DMJ = (HHB, DRV) +JBH = (FVC, HDX) +QXK = (QSB, JBH) +DQN = (QMD, GDN) +SDH = (PRH, BCX) +QSB = (HDX, FVC) +MTP = (VVG, SPN) +CTM = (MFG, GGJ) +HND = (MNV, TXG) +MJS = (DMJ, QQG) +JNL = (LKB, TFF) +QLL = (FKM, HNH) +PNM = (PQR, NRL) +VSF = (LPM, XGC) +QBC = (QQS, MVF) +BFM = (FBP, BQJ) +KNG = (RTJ, CXN) +VFQ = (HFS, LBS) +LKB = (QVB, TXF) +KQD = (CDJ, CDJ) +ZZZ = (DHJ, RMT) +XKX = (HVP, NJF) +GSL = (QMS, DHM) +PNT = (CDJ, GJZ) +KBN = (CMQ, JBG) +DNX = (SFM, GFJ) +NMX = (XRR, LKL) +VPJ = (PSK, GQL) +XQD = (QCF, QCF) +HRM = (RHB, XXJ) +LCL = (MRT, FMB) +DRG = (RNB, LCG) +BKP = (QLL, MMQ) +FSF = (SLR, XGV) +KHX = (BXC, VFQ) +SXG = (NHL, KGK) +MJD = (TDH, RFP) +NRL = (CSG, MPT) +FBP = (QGC, GXH) +HDT = (LHQ, FVD) +BMP = (RMG, JXM) +NCN = (SLT, KJD) +NXQ = (JPP, QQL) +NGV = (HNJ, VVQ) +NPN = (MXL, NJK) +FVD = (XBQ, NKF) +GQM = (QCG, GGC) +QBP = (XML, HND) +VQT = (BTL, JTF) +HKP = (CMH, NCT) +JHP = (NDL, VVH) +FRL = (SHD, FNH) +FLN = (NVL, HGF) +MXH = (QSD, KKR) +BGL = (XCX, RFC) +RSQ = (MKH, TLS) +VML = (RJM, CXK) +XHP = (MJS, DNP) +GRV = (XRN, TVQ) +GKH = (BCF, VSD) +SPH = (MVX, NMX) +TLD = (LFR, GFQ) +LML = (QKS, NND) +PKT = (VXC, CMF) +LLQ = (THT, GRV) +CKD = (SPB, QDV) +KRM = (JQV, JNG) +RNS = (HJJ, MLV) +FKR = (FKK, BHN) +XNH = (SCQ, KLF) +GDN = (LBL, FHT) +CRB = (NHQ, QRB) +JKQ = (DPT, KBN) +NJF = (RJG, NGF) +NRT = (NSK, CSL) +LJR = (VTT, HCQ) +HLK = (DFC, DPV) +PQR = (MPT, CSG) +DNF = (NJF, HVP) +MPV = (HJD, XSF) +TBV = (XTJ, SVH) +LKX = (XFJ, HBM) +KTP = (CGR, RVR) +HVL = (BJH, QXK) +GJD = (XJJ, MPF) +HLC = (VVQ, HNJ) +JTX = (NCD, XDM) +CSG = (MFF, RKT) +PCH = (PGJ, GQM) +SFF = (VXN, XDN) +TCF = (KNG, JPJ) +LDP = (XFD, SBS) +XCR = (GJD, XKT) +QBM = (GFB, LRH) +CLP = (RJM, CXK) +MTT = (HST, VCC) +TQL = (BPM, GGT) +LFR = (HNF, RDP) +JHN = (CMT, JTX) +XCX = (CHG, CHG) +XGT = (HLK, QJF) +TQS = (TKL, MNF) +KFL = (HGQ, JJR) +LHQ = (XBQ, NKF) +CCQ = (DBP, HHC) +TKL = (VDN, PBP) +LBX = (BXM, SDH) +XLX = (NGJ, LHJ) +GVN = (BPG, TNK) +CCX = (HCQ, VTT) +KQK = (QBN, NXQ) +NQC = (MCB, RLB) +MPF = (DFT, SBG) +BNH = (MLV, HJJ) +CJM = (LBH, TNM) +CND = (VKB, PGQ) +HXM = (GMJ, LJC) +TKZ = (NND, QKS) +RKT = (LNQ, KFL) +GXH = (RQP, LBX) +TFF = (QVB, TXF) +LNK = (FDC, MXH) +KPK = (BNH, RNS) +QQG = (HHB, DRV) +MFP = (PQK, NBF) +VVK = (QLL, MMQ) +GFB = (RHN, LCN) +PPD = (QCJ, HTN) +FXV = (BPM, GGT) +KSS = (CCQ, RPH) +KCM = (LTB, PCH) +RHN = (FXV, TQL) +LFV = (DDJ, NQF) +JKG = (BGQ, FHQ) +MBS = (RQJ, LDQ) +HHC = (NGV, HLC) +CCD = (QDV, SPB) +RFM = (FTB, HSB) +QCG = (VBV, TJR) +GRH = (LXK, LGQ) +CDJ = (QQJ, BFC) +THT = (TVQ, XRN) +QRD = (PXV, GVN) +VHQ = (SXV, GJL) +MLV = (VRM, MCP) +XRR = (CLV, PBJ) +NMS = (DGK, DDS) +JPS = (JKT, KHL) +DJJ = (QMS, DHM) +MFG = (TLQ, VQD) +GPQ = (SHL, BNR) +QVB = (GDC, HKP) +LNN = (CMF, VXC) +VBQ = (LNN, PKT) +MMS = (SVV, HRM) +BGJ = (HXM, JSS) +QCK = (JSJ, VQM) +PKB = (XNH, JQH) +BCF = (TQR, TLL) +JRN = (VHQ, HNK) +SHL = (DMK, JBX) +LRG = (NSK, CSL) +NJV = (FJH, TXC) +GHQ = (FSB, HFX) +LHN = (TLS, MKH) +HBF = (BMP, GLM) +DTP = (DVQ, JNL) +FXR = (FTB, HSB) +PDS = (XLD, CNX) +SXV = (CPC, JNF) +QMF = (GGF, JDP) +JVP = (GPC, DNX) +RMG = (HRF, PKB) +LRH = (LCN, RHN) +HVP = (RJG, NGF) +TRC = (VKX, RQL) +PJL = (QRD, JBN) +HNK = (GJL, SXV) +CHM = (NBF, PQK) +PGH = (KMM, MQH) +SDN = (MFP, CHM) +DMK = (VRP, NNX) +MRR = (NGK, SMQ) +QDV = (NDM, JPT) +PGJ = (QCG, GGC) +BVJ = (NLV, HBR) +PGQ = (BGK, GBH) +LKL = (CLV, PBJ) +FDC = (KKR, QSD) +JDP = (PDB, PTX) +NJJ = (HSJ, GRH) +TVJ = (HDT, SVF) +NJK = (RBV, ZZZ) +HHN = (DPP, TLD) +QMS = (BLS, TQS) +QVR = (CVB, BNJ) +FFQ = (RMJ, HGZ) +LGQ = (QBC, FXC) +JPP = (RSG, KTG) +DPV = (TXH, JRN) +HBR = (CPM, HVL) +NSK = (JQN, PDS) +SBS = (HVV, FLH) +BFQ = (VBQ, JPB) +LBN = (LXJ, CCR) +RBD = (KRM, PNX) +NQX = (LML, LML) +XSA = (QKS, NND) +NDB = (HFX, FSB) +QMD = (LBL, FHT) +XTJ = (JDT, JDT) +GLH = (SHL, BNR) +FXC = (MVF, QQS) +HSX = (NFN, LVG) +JNF = (JJH, FNR) +CXK = (SPC, TVP) +GGF = (PDB, PTX) +XBQ = (QNX, JCQ) +LJC = (SFJ, VSF) +MMD = (XLK, CND) +HFG = (JKT, KHL) +NHQ = (TNL, DKN) +JVM = (KQR, PJL) +CGH = (RTC, VFC) +HNF = (GHJ, FKR) +TMF = (NMS, GJH) +NGJ = (NXM, BDB) +MRM = (JKQ, MPS) +QTC = (JSS, HXM) +TNM = (JFF, QXR) +DHK = (LJR, CCX) +XSB = (MRR, CXC) +XRN = (XDP, QBP) +XCV = (XTJ, SVH) +VVA = (QSM, FSF) +NKC = (RDR, NSB) +MXL = (RBV, RBV) +PTX = (TVJ, JPQ) +VRL = (NQX, XJB) +RQK = (MPS, JKQ) +PBJ = (CKD, CCD) +VRP = (DQN, QLC) +KLF = (MFN, GRB) +XXJ = (GBT, CDG) +HST = (QHV, MMS) +SFT = (JPB, VBQ) +NRR = (LCL, MQR) +CSL = (JQN, PDS) +LHJ = (BDB, NXM) +TXK = (DHK, KLC) +CDK = (XCX, XCX) +FHT = (NGQ, JHN) +QQJ = (LGH, XLX) +BQJ = (GXH, QGC) +TTA = (QKM, XLR) +TVB = (FLN, LQR) +PPL = (FRL, NCX) +SPS = (XFJ, HBM) +SVH = (JDT, VRL) +QBB = (PXG, MTP) +JSJ = (VVB, NRR) +KKS = (RGL, JQD) +JDT = (NQX, NQX) +GKQ = (LLT, JRH) +VVH = (BPT, KRN) +JCQ = (CRB, PBS) +QXR = (BTV, PSZ) +VQR = (RCX, KKP) +QSM = (SLR, XGV) +JKD = (JDC, JGV) +XLD = (LFS, QVR) +TJK = (NKG, SDJ) +PVV = (HDK, VQV) +BDB = (PVV, KRX) +MPS = (KBN, DPT) +FHP = (PPD, XCF) +HTN = (KCM, PXC) +GHJ = (BHN, FKK) +JCC = (LQM, KVH) +LQR = (HGF, NVL) +LGM = (DDJ, NQF) +NJN = (QCF, NPN) +PRH = (DFF, DFF) +XDM = (FBJ, PJM) +HFN = (KRS, XSB) +GLM = (JXM, RMG) +RQP = (BXM, SDH) +BPG = (QJT, KTP) +JPB = (LNN, PKT) +DKN = (CJD, BGR) +CDG = (GSL, DJJ) +XDN = (CGM, VMG) +NQP = (SKT, JCC) +DNP = (QQG, DMJ) +NNX = (DQN, QLC) +FTB = (JFL, VJJ) +FHQ = (SMJ, XFK) +GGT = (KPK, LNT) +NGF = (LJB, CJM) +QFX = (KCN, TMF) +NKF = (JCQ, QNX) +DBP = (NGV, HLC) +RBV = (RMT, DHJ) +NML = (MBV, VCG) +KXJ = (MXH, FDC) +GFQ = (RDP, HNF) +SHD = (CMG, CTM) +GTX = (LNK, KXJ) +HXF = (KTC, LXS) +CMM = (NQP, SBR) +SDJ = (TBV, XCV) +CHG = (RMJ, RMJ) +DPT = (CMQ, JBG) +BGR = (RLM, NML) +THM = (XQD, NJN) +HJJ = (MCP, VRM) +NRQ = (JHR, GCQ) +KKR = (HSN, NJJ) +KVX = (LVG, NFN) +LTT = (TLD, DPP) +NVL = (QHB, HBF) +CXN = (XRF, BPK) +KLC = (CCX, LJR) +JDC = (DNF, XKX) +MGS = (XLK, CND) +RLM = (VCG, MBV) +VDN = (NMQ, TPR) +HSB = (JFL, VJJ) +HDS = (KLC, DHK) +XSD = (NLV, HBR) +HJD = (NJQ, PBQ) +CGM = (DHX, GVC) +LDQ = (PBL, JKG) +RDR = (XHQ, MGQ) +GPC = (GFJ, SFM) +NQT = (KRS, XSB) +TDH = (RBD, XLP) +QJF = (DFC, DPV) +VNS = (HPK, LBN) +QSD = (NJJ, HSN) +JHR = (QMF, JVH) +SMQ = (FXR, RFM) +CMG = (MFG, GGJ) +NKQ = (VFC, RTC) +VXC = (LHN, RSQ) +DHX = (LCT, XNM) +TCX = (NQT, HFN) +DRV = (TFN, QBM) +JPT = (GQX, XCR) +HRF = (JQH, XNH) +MCS = (BGJ, QTC) +PQG = (NCM, TCF) +XHR = (PJK, VPN) +MLX = (PGH, VND) +MKH = (VPJ, LMD) +TJR = (VQT, MPJ) +VLF = (GLG, QFX) +JNG = (KLR, MPV) +HDK = (BFQ, SFT) +HFX = (MLX, SPL) +BXC = (HFS, LBS) +CCT = (GTX, HPT) +VLK = (HLK, QJF) +NCD = (PJM, FBJ) +VGT = (JNL, DVQ) +LMD = (PSK, GQL) +KRX = (HDK, VQV) +QNX = (CRB, PBS) +NJQ = (GHQ, NDB) +TNV = (FBP, BQJ) +GNR = (XQD, NJN) +KKP = (QTB, PPL) +DBX = (KCQ, CPS) +VRM = (TLM, QLS) +LVG = (BRP, NJV) +NHL = (SPS, LKX) +MNV = (VBL, CHT) +PJK = (HHN, LTT) +VBH = (JQD, RGL) +GRB = (NKQ, CGH) +SKT = (KVH, LQM) +TPR = (RXR, KHV) +VVB = (LCL, MQR) +VKX = (MRM, RQK) +LTB = (PGJ, GQM) +QCJ = (KCM, PXC) +BGQ = (SMJ, SMJ) +RCV = (NQQ, JHP) +DHM = (TQS, BLS) +KRS = (CXC, MRR) +JFL = (BLT, PNM) +TXC = (NRT, LRG) +MVX = (LKL, XRR) +QSK = (JVM, MTF) +BGK = (LDB, NPX) +GJZ = (BFC, QQJ) +PXC = (LTB, PCH) +HMQ = (BGJ, QTC) +RXR = (BVJ, XSD) +BXM = (PRH, BCX) +FJH = (LRG, NRT) +GJH = (DGK, DDS) +BPT = (SBL, PHL) +RQL = (RQK, MRM) +SJB = (RQJ, LDQ) +NFN = (NJV, BRP) +MND = (NQQ, JHP) +NMQ = (KHV, RXR) +XGV = (TXK, HDS) +SPC = (KSS, XQF) +RMJ = (SMR, QRS) +CNX = (LFS, QVR) +KLR = (HJD, XSF) +XLP = (KRM, PNX) +LPM = (CMM, PHF) +FNR = (CCT, VTP) +NGQ = (CMT, JTX) +CCR = (BFM, TNV) +LCN = (FXV, TQL) +HVV = (HSX, KVX) +FSB = (MLX, SPL) +GGJ = (TLQ, VQD) +NHS = (DRG, FVK) +RQJ = (PBL, JKG) +VCC = (MMS, QHV) +HDX = (VVN, HXF) +VQM = (NRR, VVB) +TFN = (GFB, LRH) +QRS = (VQR, MDS) +HBM = (VTQ, SDN) +CCF = (SBS, XFD) +VCG = (HMQ, MCS) +NBF = (DVB, NHS) +DVB = (FVK, DRG) +PGB = (TDH, RFP) +XRF = (KFP, SPH) +KMM = (PDJ, GQN) +KTG = (GGQ, HNR) +BJH = (JBH, QSB) +JJH = (CCT, VTP) +LBH = (JFF, JFF) +GLG = (TMF, KCN) +TQR = (THM, GNR) +MQH = (GQN, PDJ) +MQR = (FMB, MRT) +FNH = (CTM, CMG) +SMR = (VQR, MDS) +GDS = (DNP, MJS) +LCT = (KQK, QFL) +VKB = (GBH, BGK) +VTQ = (CHM, MFP) +TNK = (QJT, KTP) +HNJ = (VVS, GCX) +JKT = (MMD, MGS) +PBP = (TPR, NMQ) +CPS = (KNJ, RFZ) +RNB = (KHX, FQC) +CMF = (LHN, RSQ) +HPK = (CCR, LXJ) +MFN = (NKQ, CGH) +HHB = (QBM, TFN) +DDJ = (QSK, KCC) +DFM = (RQL, VKX) +FKM = (GKQ, VJG) +HSJ = (LGQ, LXK) +HPT = (KXJ, LNK) +BNR = (JBX, DMK) +GJL = (CPC, JNF) +DFF = (KCQ, KCQ) +MCB = (GSS, JVP) +CPC = (JJH, FNR) +XLR = (MJK, DSM) +SBG = (JDG, XFL) +XSF = (NJQ, PBQ) +NGK = (RFM, FXR) +HSN = (GRH, HSJ) +LQM = (PQG, VKD) +VXN = (VMG, CGM) +PSK = (VGT, DTP) +JQH = (KLF, SCQ) +RJM = (SPC, TVP) +VSD = (TLL, TQR) +KCN = (GJH, NMS) +QHB = (GLM, BMP) +FMB = (VLK, XGT) +GBT = (DJJ, GSL) +LGH = (NGJ, LHJ) +SLR = (HDS, TXK) +VVQ = (GCX, VVS) +MDS = (RCX, KKP) +KVQ = (VPN, PJK) +CLF = (NHL, KGK) +XFK = (KQD, PNT) +CJD = (NML, RLM) +MCP = (TLM, QLS) +SPL = (PGH, VND) +GSS = (GPC, DNX) +RPH = (DBP, HHC) +HNR = (XXQ, JKD) +VBV = (VQT, MPJ) +PHF = (NQP, SBR) +BRP = (FJH, TXC) +AAA = (RMT, DHJ) +PBQ = (GHQ, NDB) +PXG = (VVG, SPN) +PNX = (JQV, JNG) +XHQ = (SJB, MBS) +VBL = (VLF, PXS) +VVG = (MTT, XTV) +PXS = (GLG, QFX) +MMQ = (FKM, HNH) +PHG = (HFN, NQT) +MFF = (KFL, LNQ) +PJM = (FHP, SPX) +SMJ = (KQD, KQD) +XGC = (CMM, PHF) +PDJ = (QCK, MHB) +SPN = (MTT, XTV) +BPK = (KFP, SPH) +MVF = (NQC, NKK) +LJB = (LBH, LBH) +GQN = (QCK, MHB) +NCG = (MTP, PXG) +GQL = (VGT, DTP) +NCX = (FNH, SHD) +HGZ = (QRS, SMR) +GMJ = (VSF, SFJ) +RFP = (XLP, RBD) +TXF = (GDC, HKP) +NBA = (SMR, QRS) +QTB = (FRL, NCX) +QLC = (GDN, QMD) +QHV = (SVV, HRM) +XTM = (GRV, THT) +VPN = (HHN, LTT) +RFZ = (XLR, QKM) +CMH = (SXG, CLF) +GFJ = (TCX, PHG) +VTT = (LGM, LFV) +RTC = (GKH, KPS) +XFJ = (VTQ, SDN) +GCX = (PGB, MJD) +MJK = (CLP, VML) +QQL = (RSG, KTG) +NSB = (XHQ, MGQ) +HCQ = (LGM, LFV) +GQX = (GJD, XKT) +RVR = (VVK, BKP) +JBG = (KKS, VBH) +JFF = (BTV, BTV) +BPM = (LNT, KPK) +LNQ = (JJR, HGQ) +FKK = (GLH, GPQ) +SBL = (CDK, BGL) +LNT = (BNH, RNS) +JRH = (JPS, HFG) +VVS = (MJD, PGB) +DPP = (LFR, GFQ) +MNF = (PBP, VDN) +QKM = (DSM, MJK) +JPJ = (CXN, RTJ) +QGC = (RQP, LBX) +CVB = (MND, RCV) +RFC = (CHG, FFQ) +KPS = (BCF, VSD) +XML = (MNV, TXG) +JVH = (JDP, GGF) +CRC = (XTM, LLQ) +VJG = (JRH, LLT) +SPB = (JPT, NDM) +JGV = (XKX, DNF) +GGQ = (XXQ, JKD) +XLK = (PGQ, VKB) +JBX = (NNX, VRP) +RSG = (HNR, GGQ) +GVC = (LCT, XNM) +XDP = (XML, HND) +CXC = (SMQ, NGK) +PXV = (TNK, BPG) +NDM = (XCR, GQX) +CPM = (BJH, QXK) +KGK = (LKX, SPS) +BLT = (NRL, PQR) +BHN = (GLH, GPQ) +BLS = (TKL, MNF) +PHL = (CDK, BGL) +BCX = (DFF, DBX) +HNH = (GKQ, VJG) +VKD = (NCM, TCF) +DSM = (VML, CLP) +DNN = (SLT, KJD) +SBR = (SKT, JCC) +TNL = (BGR, CJD) +DFT = (XFL, JDG) +MPT = (RKT, MFF) +TVQ = (XDP, QBP) +LLT = (JPS, HFG) +DXQ = (XTM, LLQ) +SFM = (PHG, TCX) +BNJ = (MND, RCV) +PBS = (NHQ, QRB) +JXM = (HRF, PKB) +VTP = (HPT, GTX) +SVV = (XXJ, RHB) +MHA = (QQJ, BFC) +MBV = (HMQ, MCS) +VJJ = (BLT, PNM) +QJT = (RVR, CGR) +VVN = (LXS, KTC) +QKS = (TRC, DFM) +MGQ = (MBS, SJB) +NDL = (BPT, KRN) +JPQ = (SVF, HDT) +VDH = (JHR, GCQ) +KCQ = (KNJ, KNJ) +XJJ = (DFT, SBG) +JQV = (MPV, KLR) +VQD = (DXQ, CRC) +JDG = (TJK, QMH) +SPX = (XCF, PPD) +LXJ = (TNV, BFM) +LBL = (NGQ, JHN) +QBN = (QQL, JPP) +LFS = (CVB, BNJ) +LBS = (JFV, NKC) +TLL = (THM, GNR) diff --git a/day_8/input_example_1.txt b/day_8/input_example_1.txt new file mode 100644 index 0000000..59e2d47 --- /dev/null +++ b/day_8/input_example_1.txt @@ -0,0 +1,9 @@ +RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ) \ No newline at end of file diff --git a/day_8/input_example_2.txt b/day_8/input_example_2.txt new file mode 100644 index 0000000..7d1b58d --- /dev/null +++ b/day_8/input_example_2.txt @@ -0,0 +1,5 @@ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ) diff --git a/day_8/src/lib.rs b/day_8/src/lib.rs new file mode 100644 index 0000000..ffa83cc --- /dev/null +++ b/day_8/src/lib.rs @@ -0,0 +1,146 @@ +use core::str::FromStr; +use std::collections::HashMap; + +#[derive(Debug, Default, PartialEq, Eq, Clone)] +pub struct DesertMap { + pub directions: Vec, + pub nodes: HashMap, +} + +impl FromStr for DesertMap { + type Err = &'static str; + + /// Parses a string `string` to return a value of [`DesertMap`] + /// + /// If parsing succeeds, return the value inside [`Ok`], otherwise + /// when the string is ill-formatted return an error specific to the + /// inside [`Err`]. + /// + /// # Examples + /// + /// ``` + /// use std::str::FromStr; + /// use std::collections::HashMap; + /// use day_8::{DesertMap, HorizontalDirection}; + /// + /// let string = " + /// RL + /// + /// AAA = (BBB, CCC) + /// BBB = (DDD, EEE) + /// CCC = (ZZZ, GGG) + /// "; + /// let expected_result = DesertMap { + /// directions: vec![HorizontalDirection::Right, HorizontalDirection::Left], + /// nodes: HashMap::from([ + /// (String::from("AAA"), (String::from("BBB"), String::from("CCC"))), + /// (String::from("BBB"), (String::from("DDD"), String::from("EEE"))), + /// (String::from("CCC"), (String::from("ZZZ"), String::from("GGG"))), + /// ]) + /// }; + /// + /// let actual_result = DesertMap::from_str(string).unwrap(); + /// assert_eq!(actual_result, expected_result); + /// ``` + fn from_str(string: &str) -> Result { + let mut result = DesertMap::default(); + let mut lines = string.trim().lines(); + let first_line = lines.next().unwrap_or_default(); + for character in first_line.chars() { + result.directions.push(HorizontalDirection::from(character)); + } + lines.next(); + for line in lines { + let mut line_splitted = line.split(" = "); + let key = line_splitted.next().unwrap_or_default(); + let values_line = line_splitted + .next() + .unwrap_or_default() + .replace(['(', ')'], ""); + let mut values_line = values_line.split(", "); + let value = ( + values_line.next().unwrap_or_default().to_string(), + values_line.next().unwrap_or_default().to_string(), + ); + result.nodes.insert(key.to_string(), value); + } + Ok(result) + } +} + +#[derive(Debug, Default, PartialEq, Eq, Copy, Clone)] +pub enum HorizontalDirection { + #[default] + Left, + Right, +} + +impl From for HorizontalDirection { + fn from(direction: char) -> Self { + if direction == 'R' { + return HorizontalDirection::Right; + } + HorizontalDirection::Left + } +} + +const KEY_START: &str = "AAA"; + +const KEY_END: &str = "ZZZ"; + +pub fn part_1(input: &str) -> usize { + let desert_map = DesertMap::from_str(input).unwrap_or_default(); + let mut steps = 0; + let mut current_step_key = KEY_START.to_string(); + let mut current_direction_index: usize = 0; + + while current_step_key != KEY_END { + let current_direction = desert_map + .directions + .get(current_direction_index) + .cloned() + .unwrap_or_else(|| { + current_direction_index = 0; + desert_map + .directions + .get(current_direction_index) + .cloned() + .unwrap_or_default() + }); + + let current_step_value = desert_map + .nodes + .get(¤t_step_key) + .cloned() + .unwrap_or_default(); + current_step_key = match current_direction { + HorizontalDirection::Left => current_step_value.0.clone(), + HorizontalDirection::Right => current_step_value.1.clone(), + }; + + current_direction_index += 1; + steps += 1; + } + + steps +} + +#[cfg(test)] +mod day_8_tests { + use super::*; + + #[test] + fn test_part_1_example_1() { + assert_eq!(part_1(include_str!("../input_example_1.txt")), 2); + } + + #[test] + fn test_part_1_example_2() { + assert_eq!(part_1(include_str!("../input_example_2.txt")), 6); + } + + #[test] + fn test_part_1() { + assert_eq!(part_1(include_str!("../input.txt")), 15871); + } +} diff --git a/day_8/src/main.rs b/day_8/src/main.rs new file mode 100644 index 0000000..453d25f --- /dev/null +++ b/day_8/src/main.rs @@ -0,0 +1,8 @@ +use day_8::part_1; + +fn main() { + let input = include_str!("../input.txt"); + println!("- Day 8: Haunted Wasteland -"); + println!("Answer Part 1: {}", part_1(input)); + // println!("Answer Part 2: {}", part_2(input)); +}