Compare commits
2 commits
54a72ab685
...
f4b36f32e2
Author | SHA1 | Date | |
---|---|---|---|
f4b36f32e2 | |||
417ed605f6 |
7 changed files with 120 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
|||
[tools]
|
||||
elixir = "1.17.3-otp-27"
|
||||
erlang = "27.1.2"
|
||||
gleam = "1.6.2"
|
||||
|
|
4
2024/gleam/.gitignore
vendored
Normal file
4
2024/gleam/.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
*.beam
|
||||
*.ez
|
||||
/build
|
||||
erl_crash.dump
|
24
2024/gleam/README.md
Normal file
24
2024/gleam/README.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
# aoc
|
||||
|
||||
[](https://hex.pm/packages/aoc)
|
||||
[](https://hexdocs.pm/aoc/)
|
||||
|
||||
```sh
|
||||
gleam add aoc@1
|
||||
```
|
||||
```gleam
|
||||
import aoc
|
||||
|
||||
pub fn main() {
|
||||
// TODO: An example of the project in use
|
||||
}
|
||||
```
|
||||
|
||||
Further documentation can be found at <https://hexdocs.pm/aoc>.
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
gleam run # Run the project
|
||||
gleam test # Run the tests
|
||||
```
|
17
2024/gleam/gleam.toml
Normal file
17
2024/gleam/gleam.toml
Normal file
|
@ -0,0 +1,17 @@
|
|||
name = "aoc"
|
||||
version = "1.0.0"
|
||||
|
||||
# Fill out these fields if you intend to generate HTML documentation or publish
|
||||
# your project to the Hex package manager.
|
||||
#
|
||||
# description = ""
|
||||
# licences = ["Apache-2.0"]
|
||||
# repository = { type = "github", user = "", repo = "" }
|
||||
# links = [{ title = "Website", href = "" }]
|
||||
#
|
||||
# For a full reference of all the available options, you can have a look at
|
||||
# https://gleam.run/writing-gleam/gleam-toml/.
|
||||
|
||||
[dependencies]
|
||||
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
|
||||
stdin = ">= 1.1.4 and < 2.0.0"
|
11
2024/gleam/manifest.toml
Normal file
11
2024/gleam/manifest.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
# This file was generated by Gleam
|
||||
# You typically do not need to edit this file
|
||||
|
||||
packages = [
|
||||
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
|
||||
{ name = "stdin", version = "1.1.4", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "stdin", source = "hex", outer_checksum = "04C04035F2A4CEEFB023837249649CD25F9A9CF5A45F9947C4D0462428A4677D" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
|
||||
stdin = { version = ">= 1.1.4 and < 2.0.0" }
|
5
2024/gleam/src/aoc.gleam
Normal file
5
2024/gleam/src/aoc.gleam
Normal file
|
@ -0,0 +1,5 @@
|
|||
import gleam/io
|
||||
|
||||
pub fn main() {
|
||||
io.println("Hello from aoc!")
|
||||
}
|
58
2024/gleam/src/aoc/day_two.gleam
Normal file
58
2024/gleam/src/aoc/day_two.gleam
Normal file
|
@ -0,0 +1,58 @@
|
|||
import gleam/int
|
||||
import gleam/io
|
||||
import gleam/iterator
|
||||
import gleam/list
|
||||
import gleam/pair
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
import stdin.{stdin}
|
||||
|
||||
type Report =
|
||||
List(Int)
|
||||
|
||||
pub fn main() {
|
||||
let reports = stdin() |> iterator.to_list() |> list.map(with: parse_report)
|
||||
|
||||
let part1 = list.count(reports, is_safe) |> int.to_string()
|
||||
let part2 = list.count(reports, is_safe_with_damper) |> int.to_string()
|
||||
|
||||
io.print("Part 1: ")
|
||||
io.println(part1)
|
||||
io.print("Part 2: ")
|
||||
io.println(part2)
|
||||
}
|
||||
|
||||
fn is_safe(report: Report) -> Bool {
|
||||
let diffs =
|
||||
report
|
||||
|> list.zip(with: list.drop(report, 1))
|
||||
|> list.map(fn(p) { pair.first(p) - pair.second(p) })
|
||||
|
||||
list.all(diffs, fn(n) { 1 <= n && n <= 3 })
|
||||
|| list.all(diffs, fn(n) { -3 <= n && n <= -1 })
|
||||
}
|
||||
|
||||
fn is_safe_with_damper(report: Report) -> Bool {
|
||||
is_safe(report)
|
||||
|| report
|
||||
|> reports_with_a_single_level_removed()
|
||||
|> list.any(is_safe)
|
||||
}
|
||||
|
||||
fn reports_with_a_single_level_removed(report: Report) -> List(Report) {
|
||||
let len = list.length(report)
|
||||
let idxs = list.range(0, len - 1)
|
||||
|
||||
use i <- list.map(idxs)
|
||||
let assert #(head, [_, ..tail]) = list.split(report, i)
|
||||
list.append(head, tail)
|
||||
}
|
||||
|
||||
fn parse_report(str: String) -> Report {
|
||||
str
|
||||
|> string.trim()
|
||||
|> string.split(" ")
|
||||
|> list.map(int.parse)
|
||||
|> result.all()
|
||||
|> result.unwrap([])
|
||||
}
|
Loading…
Reference in a new issue