From e9068fdf78fa1e929977d7b0d628ac1ef8fde80b Mon Sep 17 00:00:00 2001 From: xuu Date: Fri, 8 Dec 2023 15:11:24 -0700 Subject: [PATCH] chore: add day 8 --- day08/example1.txt | 9 + day08/example2.txt | 5 + day08/example3.txt | 10 + day08/input.txt | 708 +++++++++++++++++++++++++++++++++++++++++++++ day08/main.go | 249 ++++++++++++++++ day08/main_test.go | 72 +++++ 6 files changed, 1053 insertions(+) create mode 100644 day08/example1.txt create mode 100644 day08/example2.txt create mode 100644 day08/example3.txt create mode 100644 day08/input.txt create mode 100644 day08/main.go create mode 100644 day08/main_test.go diff --git a/day08/example1.txt b/day08/example1.txt new file mode 100644 index 0000000..59e2d47 --- /dev/null +++ b/day08/example1.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/day08/example2.txt b/day08/example2.txt new file mode 100644 index 0000000..34ffa8a --- /dev/null +++ b/day08/example2.txt @@ -0,0 +1,5 @@ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ) \ No newline at end of file diff --git a/day08/example3.txt b/day08/example3.txt new file mode 100644 index 0000000..a8e2c98 --- /dev/null +++ b/day08/example3.txt @@ -0,0 +1,10 @@ +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) \ No newline at end of file diff --git a/day08/input.txt b/day08/input.txt new file mode 100644 index 0000000..472f91e --- /dev/null +++ b/day08/input.txt @@ -0,0 +1,708 @@ +LLLLRLRLRRLRRRLRRLRLRRLRLLRRRLRRLRRRLRLLLRLRRLRLLRRRLRRLRLRRLLRRRLRRRLRLRRLRRRLRRLRRLLRRRLLLLRRLRRLRRLRRRLLLRLRLRLRRLRRRLRLRRRLRLRRRLRRLRRLLRRLLRLRRRLRLRRRLLLRLRRRLRLRRRLRRLRLRRLRRRLRRRLRRLLLRRRLRRLRRLRRLRRRLLLRRLRLRRRLLLLRRRLRRLRRRLLRLRLRRLLRRRLLRLRLRLRRLRRLRRRLRRLLRLRRLRRLLLLRRLRLRRLLRRLLRRLRRLRRRLLLRRRR + +BRR = (LVC, FSJ) +BHX = (GMG, QTN) +TJD = (HQF, PBJ) +JGN = (JXR, DGK) +NMC = (VPC, CRF) +DVJ = (KMM, DFJ) +LML = (MTG, KNX) +JKL = (LRX, FRD) +FJR = (MHX, SBX) +LMC = (QNB, QCK) +LJF = (KCJ, HCT) +KTL = (XTG, NGM) +GJB = (KTQ, KTQ) +BXR = (TDR, KMK) +GKB = (VXT, NQR) +PJL = (FPL, TFX) +LNZ = (CJC, DQL) +KCG = (KDL, LMF) +PGG = (NDL, XXC) +PMS = (XQM, DFG) +CJF = (MCG, RTJ) +JTH = (PGS, SGZ) +QBS = (JSC, KMT) +DQL = (KRR, PBL) +HSF = (SNP, KJR) +XVQ = (VQR, HSX) +QKF = (PBT, XLC) +DNA = (DQL, CJC) +LNF = (RVC, TJD) +MKT = (LDX, CGC) +BXP = (RGX, TTR) +SBX = (PGT, JGN) +JQK = (HDQ, BHQ) +MHL = (TLX, JDN) +BBD = (QNJ, BRM) +MTT = (BVK, DXB) +FVR = (VLN, PRV) +RVC = (HQF, PBJ) +LDX = (LRS, KBV) +BKF = (PXH, TNB) +BRM = (XTV, NGH) +DDL = (JNG, SFF) +BBT = (BQJ, BCN) +GPK = (CVK, DDL) +TLX = (BDN, XNT) +JHL = (BRX, QBT) +NTL = (HSR, DJD) +XBH = (PJX, JQK) +PPF = (LCT, BJL) +SFF = (FVR, NFG) +HNA = (CTC, BSG) +CNG = (DXM, QFN) +GML = (NRD, TVN) +RHH = (NBT, LJH) +LDM = (DDN, TRN) +XSK = (PLB, NVJ) +NCQ = (MDN, XGT) +FCQ = (HCT, KCJ) +DRC = (JKL, JNM) +SHR = (KLX, VLH) +FTP = (DRN, DKV) +QBF = (GVS, NXK) +GXK = (XDK, LVF) +KMK = (VXV, XCS) +PKK = (QNJ, BRM) +NJH = (TKL, PQK) +BRX = (MNM, HVC) +VTD = (BJK, LML) +QVX = (DVJ, DSF) +GRM = (JCV, SQB) +RDQ = (HCG, XMB) +RGX = (FCN, TPD) +HBH = (FPR, DHX) +PHM = (JSP, RCR) +NBT = (FRG, CMJ) +XRH = (HCB, FBM) +GMR = (FDJ, NJD) +MVG = (RDJ, QSD) +BDL = (JSB, TFN) +VXT = (MBM, JND) +TXC = (QDG, LTV) +SRL = (RRX, NDS) +SGN = (MPL, CBB) +HVH = (SLJ, CBC) +VPC = (CCG, QSJ) +KCJ = (CKC, TTD) +RLT = (LLT, VVL) +RXV = (KXQ, VBR) +FQS = (STP, BHX) +SGC = (RLJ, QGB) +XLX = (HRG, DGV) +XKV = (QQB, TNQ) +SLJ = (GTB, MPF) +PKV = (CQN, THS) +RPP = (QFT, TNK) +DKL = (MFC, MFC) +BCG = (FXM, GPR) +RFV = (DRC, MPD) +VVC = (FGN, CVL) +HQB = (QNT, VVC) +RRP = (VVC, QNT) +TRS = (RCM, GHN) +QBT = (HVC, MNM) +XKS = (SCC, VXX) +TPS = (FMH, KFN) +MTS = (VXT, NQR) +FXM = (LDM, FQT) +LNB = (XJV, SJN) +HVF = (QXC, RBH) +LSG = (QHM, TSM) +JMP = (HBH, NQK) +NGM = (KDP, HHP) +HCG = (SLR, TSC) +HGX = (DFG, XQM) +DLG = (BVN, GMM) +DJD = (VGP, VML) +HBT = (XKL, JBQ) +NVJ = (GKX, PKS) +CPS = (PKJ, RSV) +SKN = (NBT, LJH) +CCK = (TTJ, SLV) +DXM = (CBN, BXR) +RJD = (XJH, SKH) +RDJ = (CNF, TTG) +PKJ = (BCM, XKC) +FQL = (SRV, DFC) +KDS = (MFB, CLQ) +QKR = (VGD, CXK) +XXC = (NFD, SPS) +PSQ = (NJH, BLR) +BLR = (PQK, TKL) +LMX = (DGV, HRG) +GCK = (CSR, CSR) +GKJ = (JHL, PNN) +GVS = (TLN, LJX) +FPR = (JCH, GKD) +VQV = (DGB, GGN) +CPF = (VMH, JQQ) +GKD = (SGF, PHM) +JNM = (LRX, FRD) +CXV = (LNF, HGF) +TNK = (RRD, HKC) +DXX = (LMC, ZZZ) +DGG = (PPF, TRR) +MLH = (TVL, PBR) +LBC = (LJL, FTR) +FVQ = (TBC, LKB) +LTF = (CBC, SLJ) +TVH = (XMS, GJS) +SFB = (TTJ, SLV) +LVF = (MBT, SJB) +XPP = (QDM, PJL) +BVK = (RPV, JMP) +VGD = (MRX, FJR) +FRD = (XBH, CDN) +LKM = (CMF, XNS) +TCJ = (MMS, PGG) +DHQ = (MTT, BKS) +HDQ = (GPH, XKH) +KTJ = (QQT, MKR) +NMQ = (VFK, VSD) +RSK = (QDM, PJL) +XQC = (KFP, HVB) +LQN = (NGM, XTG) +TKG = (XDQ, XDQ) +KBB = (VQR, HSX) +TQQ = (QDG, LTV) +SQN = (TDQ, XKV) +TSM = (TSH, MRP) +KPH = (THS, CQN) +HCB = (DVL, RTV) +MLR = (GHT, NSN) +BFM = (GCK, SLX) +GXQ = (QKM, JJL) +GNF = (HRQ, XTK) +TRR = (BJL, LCT) +AAA = (QNB, QCK) +SDL = (KJX, LSN) +DKV = (KBB, XVQ) +RMR = (MQR, CPR) +MQC = (CBV, SRP) +PRV = (KDS, FBX) +GTB = (CCK, SFB) +SNP = (PXX, SHR) +NTR = (VHT, KXK) +MKJ = (DDL, CVK) +LSQ = (HSF, RMN) +TTJ = (CBH, QKF) +DNH = (MVG, JCJ) +KMM = (KPH, PKV) +NTT = (RQK, PFK) +QMF = (BFM, PJB) +RJB = (RBH, QXC) +LRS = (MJT, BVM) +CJG = (RMR, BMN) +DVQ = (GXM, DDH) +MFC = (LMC, LMC) +XTG = (HHP, KDP) +HKB = (RDQ, TVT) +PKS = (JQM, DGG) +BJK = (KNX, MTG) +TVT = (XMB, HCG) +JNR = (JHG, JLN) +VFK = (XKS, QBH) +NPC = (PKK, BBD) +PXX = (KLX, VLH) +XLC = (NST, BXP) +CBB = (LMX, XLX) +KLX = (MSH, LCF) +CLQ = (JNF, JNC) +TSS = (MKR, QQT) +SSS = (RMQ, RCS) +QSD = (TTG, CNF) +NHG = (DVQ, VNB) +CTC = (VCN, RJD) +THS = (QRH, GPT) +KDR = (RTK, GKJ) +NTD = (CGG, SPN) +GHP = (NCQ, FCD) +KXQ = (RLS, VRM) +GNV = (BQJ, BCN) +DXQ = (QHC, FXN) +RQB = (FVQ, TNF) +CNF = (TXC, TQQ) +BGB = (QQS, LRV) +ZZZ = (QCK, QNB) +NFM = (MGV, VTD) +MFD = (DBF, KSN) +NST = (RGX, TTR) +XJV = (LGL, LGL) +TRQ = (TVH, KJL) +PPV = (CJP, VHH) +DNV = (DBM, VGJ) +JBQ = (MHL, RFN) +HRG = (JFD, VQP) +RMB = (JKJ, JGJ) +RGQ = (RSK, XPP) +KHF = (TJG, KPX) +HPZ = (BSG, CTC) +LFJ = (TSN, NMB) +MBT = (XRC, PJR) +QFN = (CBN, BXR) +QSJ = (BRS, BKF) +XJH = (RSF, VNP) +GST = (LVF, XDK) +DDN = (NMS, RLT) +CXK = (FJR, MRX) +PHH = (FPB, TCJ) +XHV = (KTJ, TSS) +VHT = (NPC, DMG) +RNL = (NHG, DVN) +TKL = (KPN, FKX) +DGB = (GPJ, QMF) +PNN = (QBT, BRX) +TBC = (NTR, FKG) +PGS = (QDT, JQL) +SLX = (CSR, LNB) +BVV = (LVC, FSJ) +JQL = (MLH, GSC) +PXH = (CJF, CKM) +LHM = (DSF, DVJ) +NND = (NTD, DTB) +MFB = (JNF, JNC) +TTR = (FCN, TPD) +HRQ = (JTD, JTD) +SLR = (TXT, JSN) +VQP = (XPK, XGB) +QHM = (MRP, TSH) +RTJ = (PXB, TLP) +QQT = (RRL, QHQ) +CSR = (XJV, XJV) +CJP = (CJG, HQQ) +RMC = (MPD, DRC) +XMS = (LXX, GPN) +HSR = (VGP, VML) +MNM = (VSS, QXR) +SJN = (LGL, LNZ) +KBV = (MJT, BVM) +KFN = (NRC, VSG) +JNC = (RHR, LFJ) +MPF = (SFB, CCK) +QVK = (GNK, BCG) +NJD = (KCG, LDB) +DBM = (MJG, MFD) +NKF = (GGN, DGB) +BCN = (JNR, MHK) +GNK = (FXM, GPR) +KMT = (HFC, CPS) +FDJ = (KCG, LDB) +LVD = (PHH, QDR) +FCN = (VDP, MQC) +QQB = (GJB, GJB) +PLF = (JNL, HBT) +QDM = (FPL, TFX) +GXM = (CLD, NNL) +MRV = (BCG, GNK) +LKB = (NTR, FKG) +TTG = (TQQ, TXC) +GFD = (VSD, VFK) +RCS = (KQQ, DNH) +QXR = (FCQ, LJF) +HMQ = (RJQ, FQL) +VCN = (SKH, XJH) +QQG = (HSR, DJD) +HGF = (RVC, TJD) +XMB = (TSC, SLR) +PJB = (GCK, SLX) +HFC = (RSV, PKJ) +JJL = (GXK, GST) +QTN = (JLV, SGX) +KDP = (GNV, BBT) +VRF = (TFN, JSB) +GPN = (CPF, SRQ) +TSC = (TXT, JSN) +CRF = (QSJ, CCG) +TNF = (TBC, LKB) +VLH = (LCF, MSH) +BCV = (JSC, KMT) +NNL = (KVX, FQS) +RVT = (HCB, FBM) +QHC = (QKV, NCJ) +VDP = (CBV, SRP) +CBV = (VQV, NKF) +CMF = (XHV, TPR) +TRN = (RLT, NMS) +JSB = (XTH, TRS) +CFC = (TVT, RDQ) +KDL = (TKG, TKG) +TTD = (BBC, GHP) +KJR = (PXX, SHR) +MTV = (GVS, NXK) +BVN = (KDR, PVP) +DXB = (RPV, JMP) +FSJ = (QPQ, GML) +PBJ = (LQN, KTL) +PXB = (BDL, VRF) +NDS = (BGB, NGX) +CBN = (KMK, TDR) +NFG = (VLN, PRV) +JTD = (PGS, PGS) +MKR = (QHQ, RRL) +JSN = (RFV, RMC) +CVK = (SFF, JNG) +FGN = (BVQ, XQC) +HHF = (RPS, QQF) +PLB = (PKS, GKX) +BJB = (QMC, GPG) +STP = (QTN, GMG) +DGK = (RKK, BVT) +JJM = (VGD, CXK) +LTS = (QPN, BCL) +LVC = (QPQ, GML) +QNB = (GJN, RVB) +RBH = (HMQ, HPS) +NQK = (DHX, FPR) +MRP = (BDT, SGN) +JQM = (TRR, PPF) +TSH = (BDT, SGN) +JLN = (JCC, FCB) +BRP = (LDX, CGC) +SJC = (HRQ, HRQ) +LJL = (BVB, PPV) +SPN = (SDL, VHM) +SDM = (DDF, LTS) +LJH = (FRG, CMJ) +CMG = (MKD, JGT) +GPR = (LDM, FQT) +QPQ = (NRD, TVN) +JCJ = (RDJ, QSD) +HSX = (CMG, SHF) +VML = (RMB, MHC) +DHX = (JCH, GKD) +MTG = (FPX, SPD) +BDT = (MPL, CBB) +CJC = (PBL, KRR) +JLS = (LJL, FTR) +HQF = (LQN, KTL) +HQQ = (RMR, BMN) +QMC = (RBP, PXD) +RPV = (HBH, NQK) +JHG = (JCC, FCB) +TPD = (VDP, MQC) +BHQ = (XKH, GPH) +XKC = (HDD, GXR) +RPS = (KJP, KJP) +QNT = (CVL, FGN) +JSC = (HFC, CPS) +KXG = (DTB, NTD) +CKM = (RTJ, MCG) +FTD = (KPX, TJG) +VSD = (XKS, QBH) +MCG = (TLP, PXB) +XKH = (HLP, CXV) +TNB = (CJF, CKM) +XTH = (RCM, GHN) +DFN = (GMR, DSS) +LMF = (TKG, XST) +FMH = (NRC, VSG) +XCS = (JTS, GRM) +GVF = (NDS, RRX) +CXM = (HGX, PMS) +VRM = (XJT, DXQ) +JLV = (QVX, LHM) +JCH = (PHM, SGF) +NMS = (VVL, LLT) +BHS = (XJR, LKM) +CKC = (BBC, GHP) +CBH = (XLC, PBT) +PRL = (QFT, TNK) +SDF = (VTD, MGV) +RVB = (MLR, XHT) +GPT = (PLF, MJX) +KRR = (RVT, XRH) +GXR = (BVV, BRR) +NSN = (RGQ, JMQ) +KJX = (TCR, MSL) +PHL = (TSM, QHM) +MQR = (MTS, GKB) +XDK = (SJB, MBT) +NRC = (DGN, BJB) +JQQ = (LBC, JLS) +JTS = (JCV, SQB) +RLJ = (JJM, QKR) +DTB = (CGG, SPN) +BRS = (PXH, TNB) +SRV = (SPC, DNV) +MNP = (JJL, QKM) +KTQ = (XSK, XSK) +FPX = (SJC, GNF) +VBR = (VRM, RLS) +KSN = (HQB, RRP) +RJQ = (SRV, DFC) +BKS = (BVK, DXB) +BVB = (VHH, CJP) +XTK = (JTD, JTH) +PBT = (BXP, NST) +PVP = (RTK, GKJ) +QPN = (BCV, QBS) +QQF = (KJP, HPZ) +QRH = (PLF, MJX) +GHS = (BKN, TPS) +RCM = (MNP, GXQ) +DCT = (DDF, LTS) +JLC = (HGX, PMS) +VNC = (XSK, CXZ) +BQJ = (MHK, JNR) +SHF = (JGT, MKD) +FXN = (QKV, NCJ) +FPB = (PGG, MMS) +JGJ = (KXG, NND) +VBM = (PSQ, QKN) +GKX = (DGG, JQM) +DSF = (KMM, DFJ) +CMJ = (GPK, MKJ) +NCJ = (SDM, DCT) +MKD = (GQG, DHQ) +CFZ = (RQB, SHH) +RQK = (DKL, DKL) +XGT = (HPQ, VRQ) +SRP = (NKF, VQV) +BSG = (VCN, RJD) +XRC = (NFM, SDF) +DVL = (SRL, GVF) +HVB = (HXS, MLD) +MPD = (JNM, JKL) +JSP = (PHL, LSG) +BCR = (XJR, LKM) +PQK = (KPN, FKX) +FCD = (XGT, MDN) +MDL = (JLC, CXM) +FBX = (CLQ, MFB) +VNP = (RGM, GHS) +HMK = (RMN, HSF) +SJB = (PJR, XRC) +JFD = (XPK, XGB) +HHP = (GNV, BBT) +TVN = (VBM, KJN) +DMG = (PKK, BBD) +RGM = (BKN, TPS) +TRF = (KXQ, VBR) +XHT = (NSN, GHT) +DFJ = (KPH, PKV) +KJP = (CTC, BSG) +LLT = (NTS, LVD) +RMN = (SNP, KJR) +GSC = (TVL, PBR) +RSV = (XKC, BCM) +TLP = (VRF, BDL) +GGN = (GPJ, QMF) +XJR = (CMF, XNS) +GQG = (BKS, MTT) +JPN = (MRV, QVK) +RSF = (RGM, GHS) +QQS = (DLG, JPV) +DGV = (VQP, JFD) +JDN = (XNT, BDN) +LMA = (QDT, JQL) +QDG = (QGC, DTX) +HDD = (BVV, BRR) +RRD = (CNG, XLJ) +QBH = (VXX, SCC) +GPH = (HLP, CXV) +CVN = (MRV, QVK) +RLS = (XJT, DXQ) +QKN = (NJH, BLR) +NRD = (VBM, KJN) +NMB = (HVF, RJB) +XNT = (RXV, TRF) +XNS = (XHV, TPR) +SGX = (LHM, QVX) +NTS = (PHH, QDR) +SPS = (LTF, HVH) +KPX = (BCR, BHS) +GNM = (RPS, RPS) +CXZ = (NVJ, PLB) +VVL = (LVD, NTS) +VTC = (NHG, DVN) +PXD = (BHH, SSS) +TVL = (MKT, BRP) +TPR = (KTJ, TSS) +MJX = (JNL, HBT) +BKN = (FMH, KFN) +MBM = (BVC, FTP) +RTV = (GVF, SRL) +GPG = (PXD, RBP) +KXK = (DMG, NPC) +JPV = (GMM, BVN) +LXP = (VPC, CRF) +RMQ = (DNH, KQQ) +DRN = (KBB, XVQ) +MMS = (XXC, NDL) +GMG = (SGX, JLV) +MJT = (KHF, FTD) +XJT = (QHC, FXN) +JMQ = (XPP, RSK) +QCS = (TVH, KJL) +BVC = (DRN, DKV) +SPC = (DBM, VGJ) +SHH = (TNF, FVQ) +VGA = (PLB, NVJ) +RKK = (JPN, CVN) +SCC = (TRQ, QCS) +CDN = (JQK, PJX) +VSS = (LJF, FCQ) +JNF = (LFJ, RHR) +VGP = (MHC, RMB) +VGJ = (MJG, MFD) +QGC = (RHH, SKN) +RKJ = (KTQ, VNC) +DTX = (SKN, RHH) +HPQ = (VDR, SGC) +CCG = (BRS, BKF) +VLN = (FBX, KDS) +BHH = (RMQ, RCS) +BBC = (FCD, NCQ) +JCV = (GNM, GNM) +TNQ = (GJB, RKJ) +QNJ = (NGH, XTV) +GHT = (RGQ, JMQ) +LJX = (CFC, HKB) +TLN = (HKB, CFC) +PFK = (DKL, LFL) +PJR = (SDF, NFM) +JNG = (NFG, FVR) +PJX = (BHQ, HDQ) +VHH = (CJG, HQQ) +RTK = (PNN, JHL) +XGB = (MLN, SQN) +JCC = (NQX, NTT) +BVT = (CVN, JPN) +HPS = (RJQ, FQL) +TCR = (DFN, RHS) +LLA = (SHH, RQB) +NGX = (QQS, LRV) +SLV = (CBH, QKF) +BMN = (CPR, MQR) +XKL = (RFN, MHL) +CGG = (VHM, SDL) +KJN = (PSQ, QKN) +LDB = (KDL, LMF) +SPD = (SJC, GNF) +VDR = (RLJ, QGB) +DDF = (QPN, BCL) +KQQ = (MVG, JCJ) +DSS = (FDJ, NJD) +NFD = (HVH, LTF) +GPJ = (BFM, PJB) +LGL = (DQL, CJC) +TDQ = (QQB, TNQ) +RCR = (PHL, LSG) +SKH = (VNP, RSF) +DGN = (QMC, GPG) +NGH = (RNL, VTC) +TSN = (HVF, RJB) +MGV = (LML, BJK) +QHQ = (LXP, NMC) +KJL = (GJS, XMS) +RBP = (BHH, SSS) +QDR = (FPB, TCJ) +RHR = (NMB, TSN) +NXK = (LJX, TLN) +FCB = (NQX, NTT) +TFN = (TRS, XTH) +MJG = (KSN, DBF) +BVQ = (KFP, HVB) +GMM = (PVP, KDR) +MDN = (VRQ, HPQ) +LTV = (DTX, QGC) +LRV = (DLG, JPV) +MHC = (JKJ, JGJ) +DFC = (DNV, SPC) +LXV = (SHH, RQB) +PBL = (RVT, XRH) +BDN = (RXV, TRF) +JKJ = (KXG, NND) +MSH = (MTV, QBF) +NDL = (SPS, NFD) +FKG = (VHT, KXK) +PBR = (MKT, BRP) +VSG = (BJB, DGN) +RRX = (BGB, NGX) +NQR = (JND, MBM) +MRX = (MHX, SBX) +DVN = (DVQ, VNB) +HKC = (CNG, XLJ) +PGT = (JXR, DGK) +DBF = (HQB, RRP) +FRG = (GPK, MKJ) +DFG = (NMQ, GFD) +QDT = (MLH, GSC) +KVX = (STP, BHX) +LCT = (MDL, KCL) +LSN = (TCR, MSL) +TXT = (RMC, RFV) +MXM = (LXV, CFZ) +TJG = (BHS, BCR) +BCM = (GXR, HDD) +FKX = (NTL, QQG) +SRQ = (JQQ, VMH) +BCL = (BCV, QBS) +LRX = (CDN, XBH) +RHS = (DSS, GMR) +CLD = (FQS, KVX) +XLJ = (DXM, QFN) +KCL = (CXM, JLC) +HXS = (PRL, RPP) +JND = (BVC, FTP) +VQR = (SHF, CMG) +VNB = (DDH, GXM) +QGB = (JJM, QKR) +CVL = (BVQ, XQC) +RFN = (JDN, TLX) +FBM = (DVL, RTV) +TFX = (LSQ, HMK) +XQM = (NMQ, GFD) +BJL = (KCL, MDL) +FTR = (PPV, BVB) +KNX = (FPX, SPD) +MHK = (JHG, JLN) +TDR = (XCS, VXV) +HVC = (VSS, QXR) +QXC = (HPS, HMQ) +RRL = (NMC, LXP) +MLD = (PRL, RPP) +LCF = (MTV, QBF) +VXV = (JTS, GRM) +XTV = (RNL, VTC) +LFL = (MFC, DXX) +CBC = (GTB, MPF) +CQN = (GPT, QRH) +DDH = (NNL, CLD) +GHN = (GXQ, MNP) +JGT = (DHQ, GQG) +HCT = (CKC, TTD) +QCK = (GJN, RVB) +SGF = (JSP, RCR) +MPL = (XLX, LMX) +XDQ = (LXV, LXV) +MLN = (TDQ, XKV) +SGZ = (JQL, QDT) +VRQ = (SGC, VDR) +FPL = (LSQ, HMK) +SQB = (GNM, HHF) +MHX = (PGT, JGN) +QFT = (HKC, RRD) +LXX = (CPF, SRQ) +CPR = (GKB, MTS) +HLP = (LNF, HGF) +GJS = (GPN, LXX) +QKV = (DCT, SDM) +XST = (XDQ, MXM) +VXX = (QCS, TRQ) +KFP = (HXS, MLD) +MSL = (RHS, DFN) +NQX = (RQK, PFK) +CGC = (LRS, KBV) +FQT = (TRN, DDN) +VMH = (LBC, JLS) +VHM = (LSN, KJX) +JXR = (BVT, RKK) +KPN = (QQG, NTL) +QKM = (GST, GXK) +JNL = (XKL, JBQ) +BVM = (KHF, FTD) +XPK = (SQN, MLN) +GJN = (MLR, XHT) diff --git a/day08/main.go b/day08/main.go new file mode 100644 index 0000000..3a9cb66 --- /dev/null +++ b/day08/main.go @@ -0,0 +1,249 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +func main() { + if len(os.Args) != 2 { + fmt.Fprintln(os.Stderr, "Usage: day08 FILE") + } + + input, err := os.Open(os.Args[1]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + + scan := bufio.NewScanner(input) + + result, err := run(scan) + if err != nil { + fmt.Println("ERR", err) + os.Exit(1) + } + + fmt.Println("result", result) +} + +type result struct { + stepsPT1 int + stepsPT2 uint64 +} + +func run(scan *bufio.Scanner) (*result, error) { + var path []rune + m := make(nodeMap) + + for scan.Scan() { + text := scan.Text() + if len(text) == 0 { + continue + } + + if len(path) == 0 { + fmt.Println("path", text) + path = []rune(strings.TrimSpace(text)) + continue + } + + n := &node{} + i, err := fmt.Sscanf(text, "%s = (%s %s", &n.value, &n.lvalue, &n.rvalue) + if err != nil { + return nil, err + } + n.lvalue = strings.TrimRight(n.lvalue, ",)") + n.rvalue = strings.TrimRight(n.rvalue, ",)") + m[n.value] = n + + fmt.Println("value", i, n.value, n.lvalue, n.rvalue) + } + if err := m.mapNodes(); err != nil { + return nil, err + } + + steps1 := SolutionPT1(m, path) + steps2 := SolutionPT2(m, path) + + return &result{steps1, steps2}, nil +} + +type node struct { + value string + lvalue, rvalue string + left, right *node +} + +type nodeMap map[string]*node + +func (m nodeMap) mapNodes() error { + for k, v := range m { + if ptr, ok := m[v.lvalue]; ok { + v.left = ptr + } else { + return fmt.Errorf("%s L-> %s not found", k, v.lvalue) + } + if ptr, ok := m[v.rvalue]; ok { + v.right = ptr + } else { + return fmt.Errorf("%s R-> %s not found", k, v.rvalue) + } + + m[k] = v + } + return nil +} + +func SolutionPT1(m nodeMap, path []rune) int { + fmt.Println("---- PART 1 BEGIN ----") + position, ok := m["AAA"] + if !ok { + return 0 + } + var i int + var steps int + + for steps < 100000 { + steps++ + if path[i] == 'R' { + fmt.Println("step", steps, position.value, "R->", position.rvalue) + position = position.right + } else { + fmt.Println("step", steps, position.value, "L->", position.lvalue) + position = position.left + } + + if position.value == "ZZZ" { + break + } + + i++ + if i > len(path)-1 { + i = 0 + } + } + fmt.Println("---- PART 1 END ----") + return steps +} + +func SolutionPT2(m nodeMap, path []rune) uint64 { + fmt.Println("---- PART 2 BEGIN ----") + + type loop struct { + start, position, end *node + steps uint64 + } + loops := make(map[*node]loop) + + endpoints := make(map[*node]struct{}) + for k, n := range m { + if strings.HasSuffix(k, "A") { + fmt.Println("start", k) + loops[n] = loop{start: n, position: n} + } + + if strings.HasSuffix(k, "Z") { + fmt.Println("stop", k) + endpoints[n] = struct{}{} + } + } + + var i int + var steps uint64 + var stops int + maxUint := ^uint64(0) + loopsFound := 0 + + for steps < maxUint { + steps++ + if path[i] == 'R' { + for k, loop := range loops { + // fmt.Println("step", steps, position.value, "R->", position.rvalue) + loop.position = loop.position.right + loops[k] = loop + } + } else { + for k, loop := range loops { + // fmt.Println("step", steps, position.value, "L->", position.lvalue) + loop.position = loop.position.left + loops[k] = loop + } + } + + done := true + s := 0 + for k, loop := range loops { + if _, ok := endpoints[loop.position]; !ok { + // fmt.Println("no stop", i, position.value) + done = false + // break + } else { + // fmt.Println("stop", i, position.value) + if loop.end == nil { + loop.end = loop.position + loop.steps = steps + fmt.Println("loop found", loop.position.value, "steps", steps) + loops[k] = loop + loopsFound++ + } + s++ + } + } + + if loopsFound == len(loops) { + var values []uint64 + for _, loop := range loops { + values = append(values, loop.steps) + } + return LCM(values...) + } + + if s > stops { + stops = s + fmt.Println("stops", stops, "steps", steps) + } + + if done { + break + } + + i++ + if i > len(path)-1 { + i = 0 + } + } + + fmt.Println("---- PART 2 END ----") + return steps +} + +// greatest common divisor (GCD) via Euclidean algorithm +func GCD(a, b uint64) uint64 { + for b != 0 { + t := b + b = a % b + a = t + } + return a +} + +// find Least Common Multiple (LCM) via GCD +func LCM(integers ...uint64) uint64 { + if len(integers) == 0 { + return 0 + } + if len(integers) == 1 { + return integers[0] + } + + a, b := integers[0], integers[1] + result := a * b / GCD(a, b) + + for _, c := range integers[2:] { + result = LCM(result, c) + } + + return result +} diff --git a/day08/main_test.go b/day08/main_test.go new file mode 100644 index 0000000..70b0675 --- /dev/null +++ b/day08/main_test.go @@ -0,0 +1,72 @@ +package main + +import ( + "bufio" + "bytes" + "testing" + + _ "embed" + + "github.com/matryer/is" +) + +//go:embed example1.txt +var example1 []byte + +//go:embed example2.txt +var example2 []byte + +//go:embed example3.txt +var example3 []byte + +//go:embed input.txt +var input []byte + +func TestExample1(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example1)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result.stepsPT1) + is.Equal(result.stepsPT1, 2) +} + +func TestExample2(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example2)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result.stepsPT1) + is.Equal(result.stepsPT1, 6) +} + +func TestExample3(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(example3)) + + result, err := run(scan) + is.NoErr(err) + + t.Log(result.stepsPT2) + is.Equal(result.stepsPT2, uint64(6)) +} + +func TestInput(t *testing.T) { + is := is.New(t) + scan := bufio.NewScanner(bytes.NewReader(input)) + + result, err := run(scan) + is.NoErr(err) + + t.Log("part1 solution", result.stepsPT1) + is.Equal(result.stepsPT1, 14429) + t.Log("part2 solution", result.stepsPT2) + is.Equal(result.stepsPT2, uint64(10921547990923)) +} + +// first: 14429 +// second: 10921547990923 \ No newline at end of file