Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 8

advent_of_code/2023/day-08.livemd

Day 8

Day 8: Haunted Wasteland

Day 8: Haunted Wasteland

Input

input = """
RL

AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)
"""

Part 1

defmodule Awesome do
  def run(input) do
    instructions = parse_instructions(input)
    nodes = parse_nodes(input)

    solve(instructions, nodes)
  end

  defp solve(instructions, nodes) do
    Stream.cycle(instructions)
    |> Enum.reduce_while({"AAA", 0}, fn instruction, {node, acc} ->
      new_node = get_in(nodes, [node, instruction])
      {halt_or_cont(new_node), {new_node, acc + 1}}
    end)
    |> elem(1)
  end

  defp halt_or_cont("ZZZ"), do: :halt
  defp halt_or_cont(_), do: :cont

  defp parse_instructions(input) do
    input
    |> String.split("\n\n", trim: true)
    |> Enum.at(0)
    |> String.to_charlist()
  end

  defp parse_nodes(input) do
    input
    |> String.split("\n\n", trim: true)
    |> Enum.at(1)
    |> String.split("\n", trim: true)
    |> Enum.reduce(%{}, fn s, acc ->
      key = String.slice(s, 0, 3)
      l = String.slice(s, 7, 3)
      r = String.slice(s, 12, 3)

      Map.merge(acc, %{key => %{?L => l, ?R => r}})
    end)
  end
end
Awesome.run(input)
puzzle_input = """
LRLRRLRLRRRLRRRLRRLRLLRLRLRRRLRLRRLLRRLLRRRLLRRRLRRRLRRLLRLRRRLRRLRLRLLRRLLRRRLLRLRRRLRRRLLRLRRRLLRLLRRLRLRRRLLRLRLLRRRLLRLRRRLLLRRRLLLRRLLLRRRLLRLRLRLRRLLRRRLRRLRRRLRRLRRRLRLRRLRLRRRLRLRRRLRRLRRRLRLLLRLRRRLRLLRLRRLRRRLRRLRLRLRLRRLRRLLRLLLRLRLRRRLRRRLLRLLRLRRLRRRLRRLRRRLRLRRRR

JKT = (KFV, CFQ)
SQQ = (RLK, TRV)
CVT = (KRT, NCV)
JVS = (GXT, QMF)
KPF = (GCH, FCH)
XTQ = (GFM, FSP)
QBJ = (PXR, FMK)
KXQ = (FMK, PXR)
LTM = (CML, TMP)
CMD = (DFJ, GXS)
VJK = (KGN, HBX)
QTP = (KBP, CNM)
MBT = (GTN, QXG)
HGQ = (PFJ, FXL)
MDV = (CVJ, PMR)
KJQ = (TQF, HTS)
CBB = (XRL, XSP)
AAA = (QXT, CDL)
RPS = (FXF, MQL)
BPH = (VBF, FBJ)
RQM = (GVB, RKD)
KNQ = (MPV, DLX)
XSS = (LBB, SVD)
VXB = (MGQ, GCD)
XBR = (JCG, LTM)
HGR = (MFL, GGF)
SJA = (KJB, VFS)
CBL = (JFX, BBF)
KXN = (XCL, VVG)
FKV = (HGQ, TVS)
HBS = (SSP, HPN)
PTG = (GJC, TCN)
DJG = (CSP, XCC)
CNM = (JFS, FXR)
KGD = (MXP, GFC)
PMR = (SXV, RSK)
RPR = (QLD, SBT)
NFK = (DTC, NTR)
JKH = (HSF, RXX)
TXG = (VNS, PVQ)
TRS = (MSQ, BRT)
MLF = (PLL, LMX)
CDD = (SHR, FKT)
CQM = (KSF, FPN)
KSL = (XPD, NMG)
LBB = (TBB, RVJ)
DQJ = (TNP, JLT)
KLC = (KGC, BNK)
CSL = (CVC, MVR)
NCG = (JVS, JTD)
JQL = (CBL, KCH)
FBF = (FKV, DLC)
JHM = (HMD, HCH)
DGP = (BGB, JGR)
SSH = (PHG, MVV)
CSG = (QTP, TPG)
KSF = (FPV, DKV)
DJS = (PCC, RDN)
XLB = (LKT, NCQ)
XDS = (LNG, SLS)
MFT = (KJT, LSH)
STD = (PSS, JQH)
LGM = (JMD, FLJ)
GPR = (KGQ, FFB)
KJT = (XRK, JPP)
FQN = (KNQ, PNH)
CRR = (KSR, DPR)
RRD = (NCG, KKD)
FLJ = (LNL, QPR)
MQL = (SKC, BFB)
FST = (XHS, QDN)
DRT = (MQL, FXF)
RDN = (HQB, HSD)
SQH = (HTS, TQF)
VBN = (BRD, TXB)
CXV = (VMM, TXG)
MSQ = (XSJ, BRG)
PLL = (QSM, PCS)
NTD = (PLN, GKV)
GHP = (NCQ, LKT)
CXG = (XGV, VVT)
CPZ = (MVB, MKR)
GSQ = (FXV, PPG)
BCG = (LMQ, JMM)
FMK = (RQD, QJG)
CJQ = (BFV, XDS)
DLC = (HGQ, TVS)
LML = (JFC, DNN)
XTC = (TDD, VPV)
QHP = (SKP, CTC)
TBB = (QBJ, KXQ)
GPG = (VNX, DPZ)
SSS = (CFM, HQK)
DFJ = (MBV, RPX)
DLX = (RGT, VSJ)
KLL = (JKG, TPV)
NKT = (GDK, LDD)
GCH = (MLF, BFM)
PPG = (BFJ, DCR)
KHF = (HGD, MNX)
SLK = (BXX, KBV)
TDL = (FLP, DLB)
GCS = (VPL, JVR)
FPN = (DKV, FPV)
NKL = (RCL, KQH)
NHS = (FDJ, DXC)
DHN = (RCK, KVD)
QJG = (GPV, XQK)
DKD = (MFB, PTB)
FDJ = (HHQ, BJS)
TPX = (GGF, MFL)
JKG = (HBB, HLJ)
BFV = (LNG, SLS)
XTL = (LCS, XNT)
NRM = (GNX, GKS)
XSJ = (JCT, CDD)
JXR = (CBL, KCH)
TRV = (QDC, HNH)
GXS = (MBV, RPX)
HPN = (HVF, XRJ)
SSD = (FST, NBJ)
KGQ = (DMM, GKG)
DDC = (XTQ, KVK)
XHL = (MKR, MVB)
HXD = (CGG, CGG)
DSH = (BVJ, BJM)
ZZZ = (CDL, QXT)
LQL = (FVD, KLR)
BNK = (VVN, BNP)
PLD = (GJV, HDP)
JCG = (TMP, CML)
VTK = (KQN, JHS)
MNX = (TTP, SVH)
VTC = (DKN, HXM)
JPP = (GPB, QHQ)
HQK = (GGP, FMV)
QDC = (MBB, FGH)
QRD = (JML, VGV)
SXV = (HGL, NGL)
BRJ = (FKV, DLC)
DCF = (PGX, VJK)
BGF = (BQX, PMT)
JML = (FXD, KTT)
GFC = (JBN, KVS)
FBB = (JQN, NQV)
SKC = (CSG, RQJ)
SLX = (NHS, JCS)
QDN = (KHF, VXD)
MBV = (DSC, RPD)
DMM = (MVD, NHX)
QMF = (CVM, HDJ)
JCT = (SHR, FKT)
RKL = (HNF, DKC)
SMV = (RDK, RDK)
TNP = (BHL, XNQ)
XNQ = (JQL, JXR)
GGF = (GDD, XTM)
RQD = (XQK, GPV)
LSJ = (BBC, PJS)
KCR = (VCL, JVJ)
KCH = (JFX, BBF)
QRT = (BTX, KFQ)
JCS = (FDJ, DXC)
GVB = (PXL, NDD)
BMC = (VNV, FSM)
PCS = (QQT, NXG)
BKQ = (QXV, DHP)
GTN = (DTS, DXQ)
KBV = (NBK, GSQ)
GKS = (QLJ, DSH)
MPG = (RMJ, DSM)
GBB = (KQH, RCL)
SGT = (CGG, MLZ)
TCP = (JCG, LTM)
RPD = (GNF, VFG)
PCB = (GMV, MCN)
GVX = (MRF, GXC)
XGF = (KCR, CDF)
PXS = (DLQ, DBS)
GDK = (JKB, MJF)
BPR = (TKH, DKD)
PXL = (XTL, JPJ)
CSP = (NRR, XDK)
RSK = (NGL, HGL)
BTX = (QBF, VPJ)
XPD = (QRD, FQG)
MJQ = (KQN, JHS)
BLC = (TDD, VPV)
TTP = (PTG, CQX)
XSG = (GNV, DMT)
NBJ = (QDN, XHS)
KFQ = (VPJ, QBF)
KNN = (XHL, CPZ)
JVK = (XPJ, NRC)
NRV = (MRD, VVM)
DGC = (MRN, RKL)
VMM = (VNS, PVQ)
HFK = (QHX, MNT)
SCQ = (VKL, XGR)
BXA = (MKR, MVB)
RDK = (LJP, GVX)
KXM = (RCF, CQR)
CMQ = (DJS, DHQ)
VNS = (DDF, SSS)
HJC = (CPM, CJM)
VNX = (VPC, LVQ)
DSM = (BSJ, GCS)
TTT = (FBJ, VBF)
LDN = (CVJ, PMR)
JTD = (QMF, GXT)
XSP = (BPR, SHS)
QXT = (LVG, PLD)
VFS = (XSS, CTN)
TFK = (FLP, DLB)
KLR = (FSQ, BNJ)
GDG = (QQJ, HBN)
QJK = (SHX, MCT)
LKT = (TCJ, XLP)
CMH = (VBB, GPG)
JDV = (BQX, BQX)
VPV = (SFH, BJD)
GFM = (KXM, CPG)
BDX = (LQX, QXJ)
HGL = (VTS, QCT)
GRN = (KJB, VFS)
HNF = (HBC, NFK)
VMN = (CJM, CPM)
GPV = (JQQ, JVH)
BCV = (RQM, FVV)
SSG = (JKH, XMT)
KBP = (FXR, JFS)
VVT = (XVP, LSF)
BPS = (BMK, JFK)
BXJ = (DJG, JRH)
VSS = (NBM, NLM)
PMH = (FLJ, JMD)
BSJ = (JVR, VPL)
GPS = (BKR, TGN)
BKM = (SLX, JCD)
LMM = (GTB, CMD)
KKP = (MJQ, VTK)
NTG = (HXD, HXD)
DHP = (DCD, BKM)
XGP = (DBJ, PKP)
GGP = (SCQ, QFS)
NXR = (DSK, XGP)
XNH = (BHP, SVB)
HLN = (XSP, XRL)
KJH = (SSH, QQC)
QSM = (NXG, QQT)
JHS = (PQH, CVR)
RLK = (HNH, QDC)
KGC = (VVN, BNP)
BPK = (JFC, DNN)
KJB = (CTN, XSS)
VCL = (MPG, XCX)
PKR = (NCG, KKD)
NMG = (QRD, FQG)
JVH = (HDK, NPX)
NBM = (STN, PXS)
MLX = (PNK, BCV)
LVG = (HDP, GJV)
DVN = (PTP, XGF)
XDK = (SQS, VMQ)
CVR = (GBB, NKL)
SKV = (HMD, HCH)
QTA = (HTQ, CLV)
TPG = (KBP, CNM)
VPL = (XVC, JCX)
BKR = (BPS, PJD)
NSP = (NBJ, FST)
HBC = (NTR, DTC)
TRB = (PLN, GKV)
HDK = (KDG, FBC)
CFM = (FMV, GGP)
RPX = (RPD, DSC)
HMD = (HVM, KKP)
SXX = (SHX, MCT)
QJV = (GRN, FPZ)
DKC = (NFK, HBC)
XCL = (MLD, CHG)
XRJ = (LMV, QTK)
HVM = (VTK, MJQ)
VFG = (VTC, PVX)
LQX = (NTG, NTG)
KQD = (GCD, MGQ)
PJD = (BMK, JFK)
FKT = (JDN, CMH)
BHL = (JXR, JQL)
NLM = (PXS, STN)
KDG = (XLB, GHP)
NQV = (GDV, NKS)
QDQ = (SJL, KGD)
DTC = (QNG, PPK)
QHQ = (KDJ, JPF)
BBF = (THF, JVF)
QQJ = (KJH, QDK)
JFC = (GLL, TXF)
KNX = (CVC, MVR)
VBF = (TJM, GQJ)
QQC = (MVV, PHG)
FXF = (BFB, SKC)
FPZ = (VFS, KJB)
PNH = (DLX, MPV)
TXB = (KCQ, QHP)
TRL = (KSH, XJT)
DKV = (MHK, JLV)
MKR = (DRT, RPS)
FPV = (MHK, JLV)
SLV = (MNV, NTC)
SRB = (GDN, CJQ)
BMK = (BBT, TVP)
NQK = (LQL, GCM)
CDF = (VCL, JVJ)
BBH = (SDD, GJR)
DLB = (QDQ, FHL)
XVC = (DXV, NXR)
KFC = (RQK, TJJ)
HDP = (MNM, HHJ)
QDK = (QQC, SSH)
JFS = (QHH, DVN)
FXR = (QHH, DVN)
TGN = (PJD, BPS)
FFB = (DMM, GKG)
DBK = (CJQ, GDN)
RQK = (CXV, FHP)
QTK = (PSM, RPR)
JFX = (THF, JVF)
HCA = (LJP, GVX)
VVN = (CQM, JDK)
CSH = (MBT, RHJ)
HGC = (HXD, SGT)
HNH = (MBB, FGH)
QXJ = (NTG, HGC)
PQH = (GBB, NKL)
SVB = (KJQ, SQH)
PPS = (BVL, KNN)
MVD = (FSK, LSJ)
CGG = (HTQ, CLV)
KSH = (SMV, SMV)
GNF = (VTC, PVX)
QXV = (DCD, BKM)
SPG = (NGG, MFT)
XGR = (XTC, BLC)
DPZ = (LVQ, VPC)
JQN = (NKS, GDV)
QQT = (NGP, CVV)
TQF = (KXN, MKK)
NHX = (FSK, LSJ)
XLT = (GDK, LDD)
KXC = (HJC, VMN)
SVD = (TBB, RVJ)
DXV = (DSK, XGP)
TDD = (BJD, SFH)
LMV = (PSM, RPR)
BBT = (LGT, LGT)
XLP = (FLX, RDG)
DDF = (HQK, CFM)
FSM = (BXJ, LXL)
KHH = (SVL, RSF)
SHR = (JDN, JDN)
GLV = (MDV, LDN)
MFB = (BSD, PGS)
HGD = (TTP, SVH)
KDJ = (NRV, FDM)
DNN = (TXF, GLL)
MFL = (XTM, GDD)
VRB = (JPD, DPH)
DLQ = (TNG, VBN)
PPK = (BPK, LML)
TPV = (HBB, HLJ)
GMV = (QVH, KLC)
VBB = (VNX, VNX)
VVH = (QQJ, HBN)
KTT = (BKS, XNH)
JQH = (QRT, LGC)
GNX = (QLJ, DSH)
DTS = (FBF, BRJ)
FTN = (DDC, NKN)
JRH = (XCC, CSP)
FFC = (NMG, XPD)
HLK = (PRR, TRL)
KRT = (LXC, NRM)
JLT = (XNQ, BHL)
HDJ = (KFN, CSH)
DCN = (LQX, QXJ)
MKK = (VVG, XCL)
FXL = (SPG, XPN)
TXF = (NSP, SSD)
JKN = (FFB, KGQ)
BJD = (GLV, XKH)
XGL = (GPR, JKN)
BJM = (DGP, BPN)
JCB = (DPH, JPD)
MLS = (DPF, XRH)
JCX = (DXV, NXR)
QLJ = (BVJ, BJM)
LJD = (CRR, QCX)
LCS = (JVK, GMN)
HLD = (XGL, LLR)
DSN = (GRN, GRN)
JLV = (HLN, CBB)
PMT = (DSN, QJV)
HVF = (QTK, LMV)
MNV = (KRD, BMC)
LNS = (SLK, KTF)
KKD = (JTD, JVS)
QXG = (DTS, DXQ)
QVH = (KGC, BNK)
PLJ = (TSK, ZZZ)
MRN = (HNF, DKC)
JMM = (JDX, GVG)
MHK = (CBB, HLN)
THF = (LMT, DGC)
TKH = (MFB, PTB)
FCH = (BFM, MLF)
HSF = (PHT, DSP)
MNT = (DFS, KHH)
GNR = (BKR, TGN)
MNM = (FTC, FTN)
BGB = (SXX, QJK)
NGG = (KJT, LSH)
HBN = (QDK, KJH)
TVX = (PMH, LGM)
GDV = (XNJ, BDQ)
TJJ = (FHP, CXV)
QCK = (JXV, NMT)
XTK = (PKR, RRD)
HHQ = (DBK, SRB)
CPG = (CQR, RCF)
XNJ = (LTK, KLL)
LMQ = (JDX, GVG)
DPR = (BQR, QFG)
GXC = (BRR, CXG)
CQR = (KNX, CSL)
GJV = (MNM, HHJ)
HTS = (KXN, MKK)
RSF = (SSG, NNG)
HCH = (HVM, KKP)
RCL = (SQQ, VBL)
NGL = (QCT, VTS)
MCT = (GBN, DCF)
QFS = (VKL, XGR)
BNP = (CQM, JDK)
BPN = (JGR, BGB)
BRG = (JCT, CDD)
PHN = (GNR, GPS)
XBX = (TVX, FMG)
PJS = (XCQ, PPS)
SNF = (NTC, MNV)
CLV = (TCP, XBR)
CVJ = (RSK, SXV)
NRC = (VRB, JCB)
BFM = (PLL, LMX)
GKG = (MVD, NHX)
JPF = (FDM, NRV)
GTB = (DFJ, GXS)
DMT = (BKQ, GGM)
DPH = (BCG, GFJ)
MVB = (DRT, RPS)
HTQ = (TCP, XBR)
MJF = (XTK, GCB)
GRG = (NLM, NBM)
VKL = (BLC, XTC)
NCV = (LXC, NRM)
KTF = (KBV, BXX)
KRD = (FSM, VNV)
DBJ = (NQK, VSB)
XCC = (NRR, XDK)
XRK = (QHQ, GPB)
PVR = (GDG, VVH)
BSD = (GRG, VSS)
PTP = (CDF, KCR)
FDM = (VVM, MRD)
BFB = (RQJ, CSG)
PVX = (HXM, DKN)
PHG = (DQJ, QVP)
GMN = (XPJ, NRC)
QPR = (JKT, QBG)
CQX = (GJC, TCN)
VTS = (HBS, LKG)
BRT = (XSJ, BRG)
LKG = (SSP, HPN)
KVS = (BVN, TRS)
NBK = (FXV, PPG)
XGV = (XVP, LSF)
SKP = (JHM, SKV)
MPV = (VSJ, RGT)
PNR = (XGL, LLR)
BDQ = (KLL, LTK)
JFK = (BBT, TVP)
PCC = (HQB, HSD)
TNL = (CMD, GTB)
JMD = (QPR, LNL)
FMV = (SCQ, QFS)
LMX = (PCS, QSM)
DXC = (BJS, HHQ)
PXR = (QJG, RQD)
FVV = (GVB, RKD)
TSK = (QXT, CDL)
QLD = (TPX, HGR)
VPB = (JDV, BGF)
GDN = (BFV, XDS)
MLZ = (CLV, HTQ)
XNM = (BCV, PNK)
GXT = (CVM, HDJ)
DPF = (XSG, RBT)
DQQ = (GPC, KPF)
BBC = (XCQ, XCQ)
NXG = (NGP, CVV)
RDG = (NKT, XLT)
HHJ = (FTN, FTC)
FTC = (DDC, NKN)
RCK = (LBN, QCK)
MGQ = (VMB, VPB)
LGC = (BTX, KFQ)
VHQ = (DMX, LJD)
JKB = (XTK, GCB)
DCR = (PVR, MGL)
MRF = (CXG, BRR)
XRL = (BPR, SHS)
LBN = (JXV, NMT)
MVV = (DQJ, QVP)
HPH = (DHQ, DJS)
FSP = (KXM, CPG)
GPB = (KDJ, JPF)
VGV = (KTT, FXD)
MCN = (KLC, QVH)
BNJ = (HMV, CVT)
PKG = (RQK, TJJ)
CCR = (PRR, TRL)
NCQ = (TCJ, XLP)
CHG = (PHN, XCG)
HLJ = (CCR, HLK)
VSJ = (LNS, LKP)
SQS = (DCN, BDX)
KFN = (MBT, RHJ)
LGT = (JJQ, JJQ)
MJM = (RDK, MTZ)
LXL = (DJG, JRH)
TMP = (PNX, PCB)
KVD = (LBN, QCK)
RQJ = (TPG, QTP)
XTM = (RPK, FBB)
LNG = (HPH, CMQ)
PKP = (NQK, VSB)
KGN = (LMM, TNL)
QBG = (CFQ, KFV)
NPX = (FBC, KDG)
LXC = (GKS, GNX)
LDD = (JKB, MJF)
QCT = (HBS, LKG)
KQN = (CVR, PQH)
JVJ = (MPG, XCX)
PLN = (PXP, DQQ)
QNG = (LML, BPK)
JGR = (QJK, SXX)
HBX = (TNL, LMM)
JJQ = (TSK, TSK)
XJT = (SMV, MJM)
BQX = (DSN, DSN)
RVJ = (KXQ, QBJ)
PHT = (XXV, DHN)
BFJ = (PVR, MGL)
BHP = (SQH, KJQ)
TJM = (TXM, HFK)
NMT = (HLD, PNR)
DHQ = (PCC, RDN)
PGS = (GRG, VSS)
DSP = (XXV, DHN)
MRD = (VXB, KQD)
LKP = (KTF, SLK)
FSQ = (CVT, HMV)
VVG = (MLD, CHG)
NGP = (NCD, PTT)
NTC = (KRD, BMC)
FHL = (SJL, KGD)
BHV = (JJQ, PLJ)
LJP = (MRF, GXC)
SBT = (HGR, TPX)
SVH = (CQX, PTG)
MVX = (NVV, BBH)
XCG = (GPS, GNR)
RTC = (NVV, BBH)
RGT = (LKP, LNS)
HMV = (KRT, NCV)
KCQ = (SKP, CTC)
FVD = (FSQ, BNJ)
QFG = (DMR, MLS)
SHX = (DCF, GBN)
DMR = (DPF, XRH)
VMB = (JDV, BGF)
TNG = (BRD, TXB)
JDK = (FPN, KSF)
GCB = (RRD, PKR)
XCQ = (BVL, BVL)
DBS = (TNG, VBN)
FXD = (BKS, XNH)
GQJ = (TXM, HFK)
NVV = (GJR, SDD)
MBB = (SNF, SLV)
RMJ = (BSJ, GCS)
JCD = (NHS, JCS)
GBN = (VJK, PGX)
VMQ = (DCN, BDX)
HXM = (BPH, TTT)
CDL = (LVG, PLD)
DCD = (SLX, JCD)
GNV = (GGM, BKQ)
TCN = (KSL, FFC)
SSP = (XRJ, HVF)
PNX = (GMV, MCN)
XCX = (DSM, RMJ)
DMX = (CRR, QCX)
GJC = (KSL, FFC)
RKD = (PXL, NDD)
FLP = (FHL, QDQ)
PTT = (XMF, STD)
TXM = (QHX, MNT)
JVF = (DGC, LMT)
NNG = (XMT, JKH)
NKN = (KVK, XTQ)
SLS = (CMQ, HPH)
MGL = (GDG, VVH)
VPC = (TKR, QNF)
XMT = (RXX, HSF)
QCX = (KSR, DPR)
FBC = (XLB, GHP)
CPM = (RTC, MVX)
FGH = (SNF, SLV)
SJL = (GFC, MXP)
KVK = (FSP, GFM)
VXD = (MNX, HGD)
NDD = (XTL, JPJ)
GJR = (NTD, TRB)
LSH = (XRK, JPP)
JQQ = (HDK, NPX)
THS = (DMX, LJD)
FSK = (BBC, PJS)
PVQ = (DDF, SSS)
RCF = (KNX, CSL)
LMT = (MRN, RKL)
HQB = (XNM, MLX)
QHX = (KHH, DFS)
GDD = (RPK, FBB)
LLR = (JKN, GPR)
XNT = (JVK, GMN)
JBN = (BVN, TRS)
MXP = (KVS, JBN)
FXV = (DCR, BFJ)
XQK = (JQQ, JVH)
TVP = (LGT, BHV)
GGM = (DHP, QXV)
BKS = (BHP, SVB)
DXQ = (FBF, BRJ)
RXX = (DSP, PHT)
GLL = (NSP, SSD)
CTN = (SVD, LBB)
NFX = (PNH, KNQ)
DKN = (BPH, TTT)
GFJ = (LMQ, JMM)
XVP = (KXC, VPD)
NCD = (XMF, STD)
LVQ = (TKR, QNF)
SDD = (TRB, NTD)
VSB = (LQL, GCM)
XMF = (JQH, PSS)
HSD = (XNM, MLX)
LDA = (VPC, LVQ)
RBT = (DMT, GNV)
NRR = (VMQ, SQS)
LTK = (TPV, JKG)
VBL = (RLK, TRV)
GKV = (PXP, DQQ)
JDN = (VBB, VBB)
CML = (PCB, PNX)
LNL = (QBG, JKT)
SFH = (GLV, XKH)
DFS = (RSF, SVL)
FBJ = (GQJ, TJM)
VPD = (HJC, VMN)
STN = (DLQ, DBS)
CVM = (CSH, KFN)
MVR = (NFX, FQN)
KFV = (TDL, TFK)
JQV = (FMG, TVX)
XPN = (MFT, NGG)
TKR = (THS, VHQ)
VPJ = (KFC, PKG)
PTB = (BSD, PGS)
PGX = (KGN, HBX)
XRH = (RBT, XSG)
QHH = (PTP, XGF)
BJS = (DBK, SRB)
KQH = (SQQ, VBL)
LSF = (VPD, KXC)
GVG = (JQV, XBX)
BXX = (NBK, GSQ)
JDX = (JQV, XBX)
VNV = (BXJ, LXL)
SVL = (NNG, SSG)
MLD = (XCG, PHN)
QNF = (THS, VHQ)
PRR = (KSH, KSH)
CFQ = (TDL, TFK)
JVR = (JCX, XVC)
CVV = (PTT, NCD)
CJM = (RTC, MVX)
XKH = (LDN, MDV)
XHS = (KHF, VXD)
MTZ = (GVX, LJP)
BVJ = (DGP, BPN)
VVM = (KQD, VXB)
NTR = (PPK, QNG)
FMG = (LGM, PMH)
GPC = (GCH, FCH)
RHJ = (GTN, QXG)
JPD = (GFJ, BCG)
PNK = (FVV, RQM)
PFJ = (XPN, SPG)
DSC = (VFG, GNF)
PSM = (QLD, SBT)
GCM = (FVD, KLR)
CTC = (SKV, JHM)
GCD = (VMB, VPB)
TVS = (FXL, PFJ)
BVN = (BRT, MSQ)
FLX = (NKT, XLT)
PXP = (KPF, GPC)
QVP = (TNP, JLT)
JXV = (HLD, PNR)
SHS = (DKD, TKH)
FHP = (TXG, VMM)
BRR = (XGV, VVT)
RPK = (JQN, NQV)
NKS = (XNJ, BDQ)
CVC = (NFX, FQN)
PSS = (LGC, QRT)
KSR = (BQR, QFG)
FQG = (VGV, JML)
QBF = (KFC, PKG)
BVL = (XHL, XHL)
JPJ = (LCS, XNT)
TCJ = (FLX, RDG)
BRD = (QHP, KCQ)
XXV = (KVD, RCK)
XPJ = (JCB, VRB)
DSK = (DBJ, PKP)
BQR = (MLS, DMR)
HBB = (CCR, HLK)
"""
Awesome.run(puzzle_input)

Part 2

defmodule Awesome2 do
  def run(input) do
    instructions = parse_instructions(input)
    nodes = parse_nodes(input)

    solve(instructions, nodes)
  end

  defp solve(instructions, nodes) do
    nodes
    |> Enum.filter(fn {node, _} -> String.ends_with?(node, "A") end)
    |> Enum.map(fn {node, _} ->
      Stream.cycle(instructions)
      |> Enum.reduce_while({node, 0}, fn instruction, {current_node, acc} ->
        new_node = get_in(nodes, [current_node, instruction])
        {halt_or_cont(new_node), {new_node, acc + 1}}
      end)
      |> elem(1)
    end)
    |> Enum.reduce(1, &lcm/2)
  end

  defp lcm(a, b), do: div(a * b, Integer.gcd(a, b))

  defp halt_or_cont(<<_, _, ?Z>>), do: :halt
  defp halt_or_cont(_), do: :cont

  defp parse_instructions(input) do
    input
    |> String.split("\n\n", trim: true)
    |> Enum.at(0)
    |> String.to_charlist()
  end

  defp parse_nodes(input) do
    input
    |> String.split("\n\n", trim: true)
    |> Enum.at(1)
    |> String.split("\n", trim: true)
    |> Enum.reduce(%{}, fn s, acc ->
      key = String.slice(s, 0, 3)
      l = String.slice(s, 7, 3)
      r = String.slice(s, 12, 3)

      Map.merge(acc, %{key => %{?L => l, ?R => r}})
    end)
  end
end
input = """
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)
"""
Awesome2.run(input)
Awesome2.run(puzzle_input)