Skip to content
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

updates + improvements #4

Merged
merged 1 commit into from
Nov 30, 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
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ SOLVING PART 2: 209ms
---
```

This package assumes you have a [roc-lang/basic-cli](https://github.com/roc-lang/basic-cli) app (although that is not strictly necessary).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may confuse the new people


A starter solution might look like this:
A starter solution:

```roc
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.16.0/O00IPk-Krg_diNS2dVWlI0ZQP794Vctxzv0ha96mK0E.tar.br",
aoc: "https://github.com/lukewilliamboswell/aoc-template/releases/download/0.1.0/DcTQw_U67F22cX7pgx93AcHz_ShvHRaFIFjcijF3nz0.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br",
aoc: "https://github.com/lukewilliamboswell/aoc-template/releases/download/0.2.0/tlS1ZkwSKSB87_3poSOXcwHyySe0WxWOWQbPmp7rxBw.tar.br",
}

import pf.Stdin
Expand All @@ -60,10 +58,42 @@ main =
part2,
}

## Implement your part1 and part1 solutions here
## Implement your part1 and part2 solutions here
part1 : Str -> Result Str _
part1 = \_ -> Err TODO

part2 : Str -> Result Str _
part2 = \_ -> Err TODO
```

Example implementation:
```
part1 : Str -> Result Str Str
part1 = \input ->
numbers = parseNumbers input

combined =
List.joinMap numbers \x ->
List.map numbers \y ->
{ x, y, sum: x + y, mul: x * y }

when List.keepIf combined \c -> c.sum == 2020 is
[first, ..] -> Ok "$(Num.toStr first.x) * $(Num.toStr first.y) = $(Num.toStr first.mul)"
_ -> Err "expected at least one pair to have sum of 2020"

part2 : Str -> Result Str Str
part2 = \input ->
numbers = parseNumbers input

combined =
List.joinMap numbers \x ->
List.joinMap numbers \y ->
List.map numbers \z ->
{ x, y, z, sum: x + y + z, mul: x * y * z }

when List.keepIf combined \c -> c.sum == 2020 is
[first, ..] -> Ok "$(Num.toStr first.x) * $(Num.toStr first.y) * $(Num.toStr first.z) = $(Num.toStr first.mul)"
_ -> Err "expected at least one triple to have sum of 2020"

parseNumbers = \input -> input |> Str.splitOn "\n" |> List.keepOks Str.toU64
```
Comment on lines +68 to +99
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems helpful for first timers

4 changes: 2 additions & 2 deletions examples/2020/01.roc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br",
aoc: "../../package/main.roc",
}

Expand Down Expand Up @@ -48,7 +48,7 @@ part2 = \input ->
[first, ..] -> Ok "$(Num.toStr first.x) * $(Num.toStr first.y) * $(Num.toStr first.z) = $(Num.toStr first.mul)"
_ -> Err "expected at least one triple to have sum of 2020"

parseNumbers = \input -> input |> Str.split "\n" |> List.keepOks Str.toU64
parseNumbers = \input -> input |> Str.splitOn "\n" |> List.keepOks Str.toU64

expect
result = part1 example
Expand Down