···
import gleam/io.{println}
import gleam/string.{trim}
8
-
const letter_value_list = [
37
-
const value_letter_list = [
67
-
LVDicts(value: dict.Dict(String, Int), letter: dict.Dict(Int, String))
fn has_three_letter_sequence(
71
-
letter_value_map lvm,
prev_letter_value prev_letter_value,
let letter = string.slice(input, i, 1)
78
-
let letter_value_wrap = dict.get(lvm, letter)
79
-
case letter_value_wrap {
17
+
let assert [codepoint] = string.to_utf_codepoints(letter)
18
+
let v = string.utf_codepoint_to_int(codepoint)
let in_sequence = prev_letter_value == v - 1
case in_sequence && line_length == 1 {
···
89
-
has_three_letter_sequence(lvm, input, i + 1, v, line_length)
26
+
has_three_letter_sequence(input, i + 1, v, line_length)
···
117
-
fn valid_password(letter_value_dict, input) {
118
-
letter_value_dict |> has_three_letter_sequence(input, 1, 50, 0)
54
+
fn valid_password(input) {
55
+
has_three_letter_sequence(input, 1, 50, 0)
|> list.fold_until(True, fn(_, letter) {
case !string.contains(input, letter) {
···
&& has_two_pairs(input, 0, "", 0)
129
-
fn increment_password(lvdicts: LVDicts, input) {
131
-
|> string.split("")
132
-
|> list.fold_right(#("", 0), fn(acc, letter) {
133
-
let #(acc_str, pass) = acc
134
-
let add = case acc_str == "" {
138
-
let value = { lvdicts.value |> dict.get(letter) |> result.unwrap(50) } + add
140
-
True -> #(string.append("a", acc_str), 1)
143
-
lvdicts.letter |> dict.get(value) |> result.unwrap("a"),
66
+
fn increment_password(input) {
69
+
|> string.to_utf_codepoints
70
+
|> list.fold_right(#([], 0), fn(acc, letter) {
71
+
let #(acc_str, pass) = acc
72
+
let add = case acc_str == [] {
76
+
let new_letter = string.utf_codepoint_to_int(letter) + add
77
+
let assert Ok(letter_codepoint) = string.utf_codepoint(new_letter)
78
+
case new_letter > 122 {
80
+
let assert Ok(a) = string.utf_codepoint(97)
81
+
#(list.prepend(acc_str, a), 1)
83
+
False -> #(list.prepend(acc_str, letter_codepoint), 0)
87
+
|> string.from_utf_codepoints
152
-
fn get_valid_password(lvdicts: LVDicts, input) {
153
-
case valid_password(lvdicts.value, input) {
154
-
False -> get_valid_password(lvdicts, increment_password(lvdicts, input).0)
90
+
fn get_valid_password(input) {
91
+
case valid_password(input) {
92
+
False -> get_valid_password(increment_password(input))
···
let input = read(from: "../input.txt") |> result.unwrap("") |> trim()
162
-
let letter_value_dict = dict.from_list(letter_value_list)
163
-
let value_letter_dict = dict.from_list(value_letter_list)
164
-
let lvdicts = LVDicts(letter_value_dict, value_letter_dict)
166
-
let part1 = get_valid_password(lvdicts, input)
100
+
let part1 = get_valid_password(input)
168
-
let part2 = get_valid_password(lvdicts, increment_password(lvdicts, part1).0)
102
+
let part2 = get_valid_password(increment_password(part1))