1
1
mirror of https://github.com/theoludwig/advent_of_code_2023.git synced 2024-07-17 07:20:11 +02:00

Compare commits

...

3 Commits

20 changed files with 3640 additions and 87 deletions

View File

@ -10,5 +10,5 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.{yml,yaml,md}]
[*.{yml,yaml,json,md}]
indent_size = 2

View File

@ -29,26 +29,6 @@ jobs:
- run: "cargo clippy --verbose -- -D warnings"
- run: "cargo fmt -- --check"
lint-markdown:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4.1.1"
- uses: "DavidAnson/markdownlint-cli2-action@v15.0.0"
with:
config: ".markdownlint-cli2.jsonc"
globs: "**/*.md"
lint-commit:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4.1.1"
- uses: "wagoid/commitlint-github-action@v5.4.5"
with:
configFile: ".commitlintrc.json"
failOnWarnings: true
lint-general:
runs-on: "ubuntu-latest"
steps:
@ -57,10 +37,11 @@ jobs:
- name: "Setup Node.js"
uses: "actions/setup-node@v4.0.1"
with:
node-version: "20.11.0"
node-version: "20.x"
- run: "npm install --global editorconfig-checker@5.1.2"
- run: "npm install --global prettier@3.2.4"
- run: "npm clean-install"
- run: "editorconfig-checker"
- run: "prettier . --check"
- run: 'npm run lint:commit -- --to "${{ github.sha }}"'
- run: "npm run lint:editorconfig"
- run: "npm run lint:markdown"
- run: "npm run lint:prettier"

3
.gitignore vendored
View File

@ -1 +1,2 @@
/target
target
node_modules

View File

@ -1,33 +1,12 @@
{
"config": {
"extends": "markdownlint/style/prettier",
"default": true,
"no-inline-html": false,
"relative-links": true,
"no-duplicate-heading": false,
/* Disables rules that may conflict with Prettier */
/* Reference: https://github.com/DavidAnson/markdownlint/blob/main/style/prettier.json */
"blanks-around-fences": false,
"blanks-around-headings": false,
"blanks-around-lists": false,
"code-fence-style": false,
"emphasis-style": false,
"heading-start-left": false,
"hr-style": false,
"line-length": false,
"list-indent": false,
"list-marker-space": false,
"no-blanks-blockquote": false,
"no-hard-tabs": false,
"no-missing-space-atx": false,
"no-missing-space-closed-atx": false,
"no-multiple-blanks": false,
"no-multiple-space-atx": false,
"no-multiple-space-blockquote": false,
"no-multiple-space-closed-atx": false,
"no-trailing-spaces": false,
"ol-prefix": false,
"strong-style": false,
"ul-indent": false,
"no-inline-html": false,
},
"globs": ["**/*.md"],
"ignores": ["**/node_modules", "**/target"],
"customRules": ["markdownlint-rule-relative-links"],
}

4
Cargo.lock generated
View File

@ -104,6 +104,10 @@ dependencies = [
"array-init",
]
[[package]]
name = "day_8"
version = "1.0.0"
[[package]]
name = "either"
version = "1.9.0"

View File

@ -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"

View File

@ -14,6 +14,17 @@
<a href="https://conventionalcommits.org"><img src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg" alt="Conventional Commits" /></a>
</p>
## Days Progress
- [Day 1 (`**`)](./day_1)
- [Day 2 (`**`)](./day_2)
- [Day 3 (`**`)](./day_3)
- [Day 4 (`**`)](./day_4)
- [Day 5 (`**`)](./day_5)
- [Day 6 (`**`)](./day_6)
- [Day 7 (`*`)](./day_7)
- [Day 8 (`*`)](./day_8)
## Getting Started
### Prerequisites
@ -29,14 +40,13 @@ The project is **tested** against the following Rust versions:
#### External Linting Tools (optional)
**External linting tools** are used to ensure a consistent code style and commit message format. They are **used in the Continuous Integration (CI)** pipeline and **can be optionally used locally**.
**External linting tools** are used to ensure a consistent code style (external to Rust) and commit message format. They are **used in the Continuous Integration (CI)** pipeline and **can be optionally used locally**.
They have to be installed using [Node.js](https://nodejs.org/) >= v20.0.0 and [npm](https://www.npmjs.com/) >= v10.0.0.
- [editorconfig-checker](https://editorconfig-checker.github.io/) (`npm install --global editorconfig-checker@5.1.2`)
- [Prettier](https://prettier.io/) v3.2.4 (`npm install --global prettier@3.2.4`)
- [markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2) v0.12.1 (`npm install --global markdownlint-cli2@0.12.1`)
- [commitlint](https://commitlint.js.org/#/) v18.6.0 (`npm install --global @commitlint/cli@18.6.0 @commitlint/config-conventional@18.6.0`)
```sh
npm clean-install
```
### Usage
@ -51,10 +61,10 @@ cargo clippy --verbose -- -D warnings
cargo fmt -- --check
# External Linting Tools Usage (optional)
editorconfig-checker
prettier . --check
markdownlint-cli2
echo 'chore: try commitlint' | commitlint
echo 'chore: try commitlint' | npm run lint:commit
npm run lint:editorconfig
npm run lint:markdown
npm run lint:prettier
```
## 💡 Contributing

1
day_8/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

5
day_8/Cargo.toml Normal file
View File

@ -0,0 +1,5 @@
[package]
name = "day_8"
version.workspace = true
edition.workspace = true
rust-version.workspace = true

78
day_8/README.md Normal file
View File

@ -0,0 +1,78 @@
# - Day 8: Haunted Wasteland -
Source: <https://adventofcode.com/2023/day/8>
## 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`?**
## Instructions - Part 2
The sandstorm is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the instructions, but you've barely left your starting position. It's going to take **significantly more steps** to escape!
What if the map isn't for people - what if the map is for **ghosts**? Are ghosts even bound by the laws of spacetime? Only one way to find out.
After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in `A` is equal to the number ending in `Z`! If you were a ghost, you'd probably just **start at every node that ends with `A`** and follow all of the paths at the same time until they all simultaneously end up at nodes that end with `Z`.
For example:
```txt
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)
```
Here, there are two starting nodes, `11A` and `22A` (because they both end with `A`). As you follow each left/right instruction, use that instruction to **simultaneously** navigate away from both nodes you're currently on. Repeat this process until **all** of the nodes you're currently on end with `Z`. (If only some of the nodes you're on end with `Z`, they act like any other node and you continue as normal.) In this example, you would proceed as follows:
- Step 0: You are at `11A` and `22A`.
- Step 1: You choose all of the **left** paths, leading you to `11B` and `22B`.
- Step 2: You choose all of the **right** paths, leading you to **`11Z`** and `22C`.
- Step 3: You choose all of the **left** paths, leading you to `11B` and **`22Z`**.
- Step 4: You choose all of the **right** paths, leading you to **`11Z`** and `22B`.
- Step 5: You choose all of the **left** paths, leading you to `11B` and `22C`.
- Step 6: You choose all of the **right** paths, leading you to **`11Z`** and **`22Z`**.
So, in this example, you end up entirely on nodes that end in `Z` after **`6`** steps.
Simultaneously start on every node that ends with `A`. **How many steps does it take before you're only on nodes that end with `Z`?**

732
day_8/input.txt Normal file
View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1,5 @@
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)

146
day_8/src/lib.rs Normal file
View File

@ -0,0 +1,146 @@
use core::str::FromStr;
use std::collections::HashMap;
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct DesertMap {
pub directions: Vec<HorizontalDirection>,
pub nodes: HashMap<String, (String, String)>,
}
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<Self, Self::Err> {
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<char> 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(&current_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);
}
}

8
day_8/src/main.rs Normal file
View File

@ -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));
}

2570
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "advent_of_code_2023",
"private": true,
"type": "module",
"engines": {
"node": ">=20.0.0",
"npm": ">=10.0.0"
},
"scripts": {
"lint:commit": "commitlint",
"lint:editorconfig": "editorconfig-checker",
"lint:markdown": "markdownlint-cli2",
"lint:prettier": "prettier . --check"
},
"devDependencies": {
"@commitlint/cli": "18.6.0",
"@commitlint/config-conventional": "18.6.0",
"editorconfig-checker": "5.1.3",
"markdownlint": "0.33.0",
"markdownlint-cli2": "0.12.1",
"markdownlint-rule-relative-links": "2.3.1",
"prettier": "3.2.5"
}
}