Skip to content

Day01 and Day02 Community Solution #573

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

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/2024/puzzles/day01.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ end part2
- [Solution](https://scastie.scala-lang.org/Sporarum/jVlQBCvoQXCtlK4ryIn42Q/4) by [Quentin Bernet](https://github.com/Sporarum)
- [Solution](https://github.com/jnclt/adventofcode2024/blob/main/day01/historian-hysteria.sc) by [jnclt](https://github.com/jnclt)
- [Solution](https://github.com/YannMoisan/advent-of-code/blob/master/2024/src/main/scala/Day1.scala) by [YannMoisan](https://github.com/YannMoisan)
- [Solution](https://github.com/itsjoeoui/aoc2024/blob/main/src/day01.scala) by [itsjoeoui](https://github.com/itsjoeoui)

Share your solution to the Scala community by editing this page.
You can even write the whole article! [See here for the expected format](https://github.com/scalacenter/scala-advent-of-code/discussions/424)
9 changes: 5 additions & 4 deletions docs/2024/puzzles/day02.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ We need to check consecutive pairs of numbers in each report in 3 ways:
- to see if their differences are within given bounds.

So let's construct them only once, save it as a `val`, then reuse this value 3 times.
It's not the most efficient way (like traversing only once and keeping track of everything),
It's not the most efficient way (like traversing only once and keeping track of everything),
but it's very clean and simple:

```scala
Expand All @@ -61,7 +61,7 @@ def part1(input: String): Int = parse(input).count(_.isSafe)

### Part 2

Now we add new methods to `Report`.
Now we add new methods to `Report`.
We check if there exists a `Report` obtained by dropping one number, such that it's safe.
We do this by iterating over the index of each `Report`.
Then, a `Report` is safe, if it's safe as in Part 1, or one of the dampened reports is safe:
Expand All @@ -75,8 +75,8 @@ case class Report(levels: Seq[Long]):
def isDampenedSafe: Boolean = isSafe || checkDampenedReports
```

Again this is not the most efficient way (we are creating many new `Report` instances),
but our puzzle inputs are fairly short (there are at most 8 levels in each `Report`),
Again this is not the most efficient way (we are creating many new `Report` instances),
but our puzzle inputs are fairly short (there are at most 8 levels in each `Report`),
so it's a simple approach that reuses the `isSafe` method from Part 1.

Part 2 solver now counts the dampened safe reports:
Expand All @@ -95,6 +95,7 @@ def part2(input: String): Int = parse(input).count(_.isDampenedSafe)
- [Solution](https://github.com/Philippus/adventofcode/blob/main/src/main/scala/adventofcode2024/Day02.scala) by [Philippus Baalman](https://github.com/philippus)

- [Solution](https://github.com/nikiforo/aoc24/blob/main/src/main/scala/io/github/nikiforo/aoc24/D2T2.scala) by [Artem Nikiforov](https://github.com/nikiforo)
- [Solution](https://github.com/itsjoeoui/aoc2024/blob/main/src/day02.scala) by [itsjoeoui](https://github.com/itsjoeoui)

Share your solution to the Scala community by editing this page.
You can even write the whole article! [See here for the expected format](https://github.com/scalacenter/scala-advent-of-code/discussions/424)