my solutions to advent of code
aoc advent-of-code

use recursion instead of yielder in day 4 2015

aylac.top ecf8623a 05ba45c0

verified
Changed files
+9 -30
2015
-1
2015/4/gleam/gleam.toml
···
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
simplifile = ">= 2.3.0 and < 3.0.0"
gleam_crypto = ">= 1.5.1 and < 2.0.0"
-
gleam_yielder = ">= 1.1.0 and < 2.0.0"
[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
-2
2015/4/gleam/manifest.toml
···
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
{ name = "gleam_crypto", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "50774BAFFF1144E7872814C566C5D653D83A3EBF23ACC3156B757A1B6819086E" },
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
-
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
{ name = "gleeunit", version = "1.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "CD701726CBCE5588B375D157B4391CFD0F2F134CD12D9B6998A395484DE05C58" },
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
]
···
[requirements]
gleam_crypto = { version = ">= 1.5.1 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
-
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
simplifile = { version = ">= 2.3.0 and < 3.0.0" }
+9 -27
2015/4/gleam/src/main.gleam
···
import gleam/io.{println}
import gleam/result.{unwrap}
import gleam/string.{trim}
-
import gleam/yielder.{Next, find, unfold}
import simplifile.{read}
-
// claude just helped me with understanding how yield works. didn't want to add that 1 manually because it's ugly. here's my pure mine attempt
-
// let res =
-
// yielder.unfold(0, fn(v) {
-
// let hash =
-
// crypto.hash(Md5, <<{ input <> to_string(v) }:utf8>>)
-
// |> bit_array.base16_encode
-
//
-
// case hash {
-
// "00000" <> _ -> yielder.Done
-
// ______ -> yielder.Next(v, v + 1)
-
// }
-
// })
-
// |> yielder.last()
-
// |> unwrap(0)
-
// |> int.add(1)
-
// |> to_string
-
-
pub fn find_first_with_leading(input: String, leading: String) {
-
unfold(0, fn(v) { Next(v, v + 1) })
-
|> find(fn(v) {
-
hash(Md5, <<{ input <> to_string(v) }:utf8>>)
+
pub fn find_first_with_leading(input: String, leading: String, i: Int) {
+
let result =
+
hash(Md5, <<{ input <> to_string(i) }:utf8>>)
|> bit_array.base16_encode
|> string.starts_with(leading)
-
})
-
|> unwrap(0)
-
|> to_string
+
case result {
+
True -> i
+
False -> find_first_with_leading(input, leading, i + 1)
+
}
}
pub fn main() {
let input = read(from: "../input.txt") |> unwrap("") |> trim()
-
println(find_first_with_leading(input, "00000"))
-
println(find_first_with_leading(input, "000000"))
+
println(find_first_with_leading(input, "00000", 0) |> to_string)
+
println(find_first_with_leading(input, "000000", 0) |> to_string)
}