aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/adglent/src/adglent@day.erl
blob: b80368f2a14bd115da86fe1bbd26bb7cdd1d2fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
-module(adglent@day).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).

-export([main/0]).

-spec create_file_if_not_present(binary(), binary()) -> {ok, binary()} |
    {error, binary()}.
create_file_if_not_present(Content, Path) ->
    case simplifile:is_file(Path) of
        true ->
            {ok, <<Path/binary, " already exists - skipped"/utf8>>};

        false ->
            gleam@result:'try'(
                begin
                    _pipe = simplifile:create_file(Path),
                    priv@errors:map_messages(
                        _pipe,
                        <<"Created "/utf8, Path/binary>>,
                        <<"Could not create "/utf8, Path/binary>>
                    )
                end,
                fun(_) -> _pipe@1 = simplifile:write(Path, Content),
                    priv@errors:map_messages(
                        _pipe@1,
                        <<"Wrote "/utf8, Path/binary>>,
                        <<"Could not write to "/utf8, Path/binary>>
                    ) end
            )
    end.

-spec main() -> binary().
main() ->
    Day@1 = begin
        _pipe = case adglent:start_arguments() of
            [Day] ->
                {ok, Day};

            Args ->
                {error,
                    <<"Expected day - found: "/utf8,
                        (gleam@string:join(Args, <<", "/utf8>>))/binary>>}
        end,
        _pipe@1 = priv@errors:map_error(
            _pipe,
            <<"Error when parsing command args"/utf8>>
        ),
        _pipe@2 = priv@errors:print_error(_pipe@1),
        priv@errors:assert_ok(_pipe@2)
    end,
    Aoc_toml = begin
        _pipe@3 = simplifile:read(<<"aoc.toml"/utf8>>),
        _pipe@4 = priv@errors:map_error(
            _pipe@3,
            <<"Could not read aoc.toml"/utf8>>
        ),
        _pipe@5 = priv@errors:print_error(_pipe@4),
        priv@errors:assert_ok(_pipe@5)
    end,
    Aoc_toml_version = priv@toml:get_int(Aoc_toml, [<<"version"/utf8>>]),
    Year = begin
        _pipe@6 = priv@toml:get_string(Aoc_toml, [<<"year"/utf8>>]),
        _pipe@7 = priv@errors:map_error(
            _pipe@6,
            <<"Could not read \"year\" from aoc.toml"/utf8>>
        ),
        _pipe@8 = priv@errors:print_error(_pipe@7),
        priv@errors:assert_ok(_pipe@8)
    end,
    Session = begin
        _pipe@9 = priv@toml:get_string(Aoc_toml, [<<"session"/utf8>>]),
        _pipe@10 = priv@errors:map_error(
            _pipe@9,
            <<"Could not read \"session\" from aoc.toml"/utf8>>
        ),
        _pipe@11 = priv@errors:print_error(_pipe@10),
        priv@errors:assert_ok(_pipe@11)
    end,
    Showtime = case Aoc_toml_version of
        {ok, 2} ->
            _pipe@12 = priv@toml:get_bool(Aoc_toml, [<<"showtime"/utf8>>]),
            _pipe@13 = priv@errors:map_error(
                _pipe@12,
                <<"Could not read \"showtime\" from aoc.toml"/utf8>>
            ),
            _pipe@14 = priv@errors:print_error(_pipe@13),
            priv@errors:assert_ok(_pipe@14);

        _ ->
            _pipe@15 = priv@toml:get_string(Aoc_toml, [<<"showtime"/utf8>>]),
            _pipe@16 = gleam@result:map(
                _pipe@15,
                fun(Bool_string) -> case Bool_string of
                        <<"True"/utf8>> ->
                            true;

                        _ ->
                            false
                    end end
            ),
            _pipe@17 = priv@errors:map_error(
                _pipe@16,
                <<"Could not read \"showtime\" from aoc.toml"/utf8>>
            ),
            _pipe@18 = priv@errors:print_error(_pipe@17),
            priv@errors:assert_ok(_pipe@18)
    end,
    Test_folder = adglent:get_test_folder(Day@1),
    Test_file = <<<<<<Test_folder/binary, "/day"/utf8>>/binary, Day@1/binary>>/binary,
        "_test.gleam"/utf8>>,
    _pipe@19 = simplifile:create_directory_all(Test_folder),
    _pipe@20 = priv@errors:map_error(
        _pipe@19,
        <<<<"Could not create folder \""/utf8, Test_folder/binary>>/binary,
            "\""/utf8>>
    ),
    _pipe@21 = priv@errors:print_error(_pipe@20),
    priv@errors:assert_ok(_pipe@21),
    Testfile_template = case Showtime of
        true ->
            <<"
import gleam/list
import showtime/tests/should
import adglent.{type Example, Example}
import day{{ day }}/solve

type Problem1AnswerType =
  String

type Problem2AnswerType =
  String

/// Add examples for part 1 here:
/// ```gleam
///const part1_examples: List(Example(Problem1AnswerType)) = [Example(\"some input\", \"\")]
/// ```
const part1_examples: List(Example(Problem1AnswerType)) = []

/// Add examples for part 2 here:
/// ```gleam
///const part2_examples: List(Example(Problem2AnswerType)) = [Example(\"some input\", \"\")]
/// ```
const part2_examples: List(Example(Problem2AnswerType)) = []

pub fn part1_test() {
  part1_examples
  |> should.not_equal([])
  use example <- list.map(part1_examples)
  solve.part1(example.input)
  |> should.equal(example.answer)
}

pub fn part2_test() {
  part2_examples
  |> should.not_equal([])
  use example <- list.map(part2_examples)
  solve.part2(example.input)
  |> should.equal(example.answer)
}

"/utf8>>;

        false ->
            <<"
import gleam/list
import gleeunit/should
import adglent.{type Example, Example}
import day{{ day }}/solve

type Problem1AnswerType =
  String

type Problem2AnswerType =
  String

/// Add examples for part 1 here:
/// ```gleam
///const part1_examples: List(Example(Problem1AnswerType)) = [Example(\"some input\", \"\")]
/// ```
const part1_examples: List(Example(Problem1AnswerType)) = []

/// Add examples for part 2 here:
/// ```gleam
///const part2_examples: List(Example(Problem2AnswerType)) = [Example(\"some input\", \"\")]
/// ```
const part2_examples: List(Example(Problem2AnswerType)) = []

pub fn part1_test() {
  part1_examples
  |> should.not_equal([])
  use example <- list.map(part1_examples)
  solve.part1(example.input)
  |> should.equal(example.answer)
}

pub fn part2_test() {
  part2_examples
  |> should.not_equal([])
  use example <- list.map(part2_examples)
  solve.part2(example.input)
  |> should.equal(example.answer)
}

"/utf8>>
    end,
    _pipe@22 = priv@template:render(
        Testfile_template,
        [{<<"day"/utf8>>, Day@1}]
    ),
    _pipe@23 = create_file_if_not_present(_pipe@22, Test_file),
    _pipe@24 = priv@errors:print_result(_pipe@23),
    priv@errors:assert_ok(_pipe@24),
    Solutions_folder = <<"src/day"/utf8, Day@1/binary>>,
    Solution_file = <<Solutions_folder/binary, "/solve.gleam"/utf8>>,
    _pipe@25 = simplifile:create_directory_all(Solutions_folder),
    _pipe@26 = priv@errors:map_error(
        _pipe@25,
        <<<<"Could not create folder \""/utf8, Solutions_folder/binary>>/binary,
            "\""/utf8>>
    ),
    _pipe@27 = priv@errors:print_error(_pipe@26),
    priv@errors:assert_ok(_pipe@27),
    _pipe@28 = priv@template:render(
        <<"
import adglent.{First, Second}
import gleam/io

pub fn part1(input: String) {
  todo as \"Implement solution to part 1\"
}

pub fn part2(input: String) {
  todo as \"Implement solution to part 2\"
}

pub fn main() {
  let assert Ok(part) = adglent.get_part()
  let assert Ok(input) = adglent.get_input(\"{{ day }}\")
  case part {
    First ->
      part1(input)
      |> adglent.inspect
      |> io.println
    Second ->
      part2(input)
      |> adglent.inspect
      |> io.println
  }
}
"/utf8>>,
        [{<<"day"/utf8>>, Day@1}]
    ),
    _pipe@29 = create_file_if_not_present(_pipe@28, Solution_file),
    _pipe@30 = priv@errors:print_result(_pipe@29),
    priv@errors:assert_ok(_pipe@30),
    _pipe@31 = create_file_if_not_present(
        <<"input.txt"/utf8>>,
        <<Solutions_folder/binary, "/.gitignore"/utf8>>
    ),
    _pipe@32 = priv@errors:print_result(_pipe@31),
    priv@errors:assert_ok(_pipe@32),
    Input = begin
        _pipe@33 = priv@aoc_client:get_input(Year, Day@1, Session),
        _pipe@34 = priv@errors:map_error(
            _pipe@33,
            <<"Error when fetching input"/utf8>>
        ),
        _pipe@35 = priv@errors:print_error(_pipe@34),
        priv@errors:assert_ok(_pipe@35)
    end,
    _pipe@36 = Input,
    _pipe@37 = gleam@string:trim(_pipe@36),
    _pipe@38 = create_file_if_not_present(
        _pipe@37,
        <<Solutions_folder/binary, "/input.txt"/utf8>>
    ),
    _pipe@39 = priv@errors:print_result(_pipe@38),
    priv@errors:assert_ok(_pipe@39).