generated from EllaKaye/advent-of-code-website-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d16d137
commit d833d60
Showing
10 changed files
with
2,353 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
title: "2024" | ||
listing: | ||
contents: "2024/day" | ||
sort: "date" | ||
type: table | ||
categories: true | ||
sort-ui: false | ||
filter-ui: false | ||
fields: [title, categories] | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
--- | ||
title: "2024: Day 01 Historian Hysteria" | ||
date: 2024-12-02 | ||
categories: | ||
- loop | ||
- grepl | ||
- vectors | ||
draft: false | ||
--- | ||
|
||
## Setup | ||
|
||
[The original challenge](https://adventofcode.com/2024/day/01) | ||
|
||
Chief historian is lost, gotta try to find him. Searched his office, found several lists of places (ID's) we should look, but the lists don't align. Need to find out how close they are. | ||
|
||
```{r} | ||
library(aochelpers) | ||
library(dplyr) | ||
# other options: aoc_input_data_frame(), aoc_input_matrix() | ||
input <- aoc_input_vector(01, 2024) | ||
head(input) | ||
``` | ||
|
||
# TLDR; Solutions | ||
|
||
## Part 1 ⭐ | ||
|
||
::: {.callout-danger} | ||
### ❓ What is the total distance between your lists? | ||
::: | ||
|
||
```{r} | ||
p1 <- input |> lines_to_matrix(split = " ") | ||
c1 <- sort(as.numeric(p1[,1])) | ||
c2 <- sort(as.numeric(p1[,2])) | ||
sum(abs(c1-c2)) | ||
``` | ||
|
||
|
||
|
||
## Part 2 ⭐⭐ | ||
|
||
::: {.callout-danger} | ||
### ❓ Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list. | ||
::: | ||
|
||
```{r} | ||
multiplier <- rep(0, NROW(p1)) | ||
for(i in 1:NROW(p1)){ | ||
multiplier[i] <- grepl(c1[i], c2) |> sum() | ||
} | ||
sum(c1*multiplier) | ||
``` | ||
|
||
|
||
|
||
# Walkthrough / Explainer | ||
|
||
## Part 1 | ||
|
||
:::{.callout-exa icon=true} | ||
**Example Data** | ||
|
||
Data comes in as space separated lists, so I used the `lines_to_matrix` function from `aochelpers` | ||
```{r} | ||
(exa <- c("3 4", "4 3", "2 5", "1 3", "3 9", "3 3") |> lines_to_matrix()) | ||
``` | ||
::: | ||
|
||
_Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left number with the second-smallest right number, and so on._ | ||
|
||
So, take the separation even further and detach the lists into separate vectors, sorted ascendingly. | ||
|
||
```{r} | ||
ex1 <- sort(as.numeric(exa[,1])) | ||
ex2 <- sort(as.numeric(exa[,5])) | ||
``` | ||
|
||
_Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances._ | ||
|
||
Sum the absolute differences | ||
```{r} | ||
sum(abs(ex1-ex2)) | ||
``` | ||
|
||
Matches the example solution. | ||
|
||
## Part 2 | ||
The two lists are pretty different. The Historians can't agree on where the problem is at, but noticed that some of the ID"s are duplicated. So let's calculate a similarity score. | ||
|
||
_ Calculate a total similarity score by adding up each number in the left list after multiplying it by the number of times that number appears in the right list._ | ||
|
||
So, use `grepl` to identify the number of times each element in the first vector appears in the second vector. Since it returns a TRUE/FALSE for ever element, add up the trues using `sum`. Then loop over all rows, add that as a third vector. | ||
```{r} | ||
mult <- rep(0, NROW(exa)) | ||
for(i in 1:NROW(exa)){ | ||
mult[i] <- grepl(ex1[i], ex2) |> sum() | ||
} | ||
``` | ||
Now take the first column, and multiply it by the multiplier, then add the result | ||
```{r} | ||
sum(ex1*mult) | ||
``` | ||
|
||
Matches example. | ||
|
||
##### Session info {.appendix} | ||
|
||
<details><summary>Toggle</summary> | ||
|
||
|
||
```{r} | ||
#| echo: false | ||
sessioninfo::session_info(pkgs = "attached") | ||
``` | ||
|
||
|
||
</details> | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.