Compare commits
No commits in common. "f4b36f32e260f3068c14dc867d76e65fbc11a04b" and "54a72ab68535aa21cb16475e9ef0dbd488265f22" have entirely different histories.
f4b36f32e2
...
54a72ab685
7 changed files with 0 additions and 120 deletions
|
@ -1,4 +1,3 @@
|
||||||
[tools]
|
[tools]
|
||||||
elixir = "1.17.3-otp-27"
|
elixir = "1.17.3-otp-27"
|
||||||
erlang = "27.1.2"
|
erlang = "27.1.2"
|
||||||
gleam = "1.6.2"
|
|
||||||
|
|
4
2024/gleam/.gitignore
vendored
4
2024/gleam/.gitignore
vendored
|
@ -1,4 +0,0 @@
|
||||||
*.beam
|
|
||||||
*.ez
|
|
||||||
/build
|
|
||||||
erl_crash.dump
|
|
|
@ -1,24 +0,0 @@
|
||||||
# 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
|
|
||||||
```
|
|
|
@ -1,17 +0,0 @@
|
||||||
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"
|
|
|
@ -1,11 +0,0 @@
|
||||||
# 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" }
|
|
|
@ -1,5 +0,0 @@
|
||||||
import gleam/io
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
io.println("Hello from aoc!")
|
|
||||||
}
|
|
|
@ -1,58 +0,0 @@
|
||||||
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