-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solve Day 18 #31
Merged
Merged
Solve Day 18 #31
Conversation
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
Codecov Report
@@ Coverage Diff @@
## main #31 +/- ##
==========================================
+ Coverage 95.45% 95.51% +0.05%
==========================================
Files 24 26 +2
Lines 374 401 +27
Branches 28 28
==========================================
+ Hits 357 383 +26
- Misses 9 10 +1
Partials 8 8
Continue to review full report at Codecov.
|
manuphatak
added a commit
that referenced
this pull request
Dec 26, 2020
manuphatak
added a commit
that referenced
this pull request
Jan 1, 2021
manuphatak
added a commit
that referenced
this pull request
Jan 10, 2021
* origin/main: Update dependencies (#41) Remove hspec skip rules from hlint (#39) Add hlint rule (#37) Solve Day 21 (#35) Refactor: Use LANGUAGE TypeApplications (#36) Solve Day 12 (#21) Solve Day 20 (#34) Solve Day 10 (#18) Harvest parseInts util (#33) Solve Day 18 (#31) Solve Day 17 (#30) Create Advent.Parser for shared parsers (#29) Solve Day 16 (#26) Add hspec-discover to PATH (#28)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Day 18: Operation Order
As you look out the window and notice a heavily-forested continent slowly appear over the horizon, you are interrupted by the child sitting next to you. They're curious if you could help them with their math homework.
Unfortunately, it seems like this "math" follows different rules than you remember.
The homework (your puzzle input) consists of a series of expressions that consist of addition (
+
), multiplication (*
), and parentheses ((...)
). Just like normal math, parentheses indicate that the expression inside must be evaluated before it can be used by the surrounding expression. Addition still finds the sum of the numbers on both sides of the operator, and multiplication still finds the product.However, the rules of operator precedence have changed. Rather than evaluating multiplication before addition, the operators have the same precedence , and are evaluated left-to-right regardless of the order in which they appear.
For example, the steps to evaluate the expression
1 + 2 * 3 + 4 * 5 + 6
are as follows:Parentheses can override this order; for example, here is what happens if parentheses are added to form
1 + (2 * 3) + (4 * (5 + 6))
:Here are a few more examples:
2 * 3 + (4 * 5)
becomes26
.5 + (8 * 3 + 9 + 3 * 4 * 3)
becomes437
.5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))
becomes12240
.((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2
becomes13632
.Before you can help with the homework, you need to understand it yourself. Evaluate the expression on each line of the homework; what is the sum of the resulting values?
Part Two
You manage to answer the child's questions and they finish part 1 of their homework, but get stuck when they reach the next section: advanced math.
Now, addition and multiplication have different precedence levels, but they're not the ones you're familiar with. Instead, addition is evaluated before multiplication.
For example, the steps to evaluate the expression
1 + 2 * 3 + 4 * 5 + 6
are now as follows:Here are the other examples from above:
1 + (2 * 3) + (4 * (5 + 6))
still becomes51
.2 * 3 + (4 * 5)
becomes46
.5 + (8 * 3 + 9 + 3 * 4 * 3)
becomes1445
.5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))
becomes669060
.((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2
becomes23340
.What do you get if you add up the results of evaluating the homework problems using these new rules?
Link
https://adventofcode.com/2020/day/18