this repo has no description
1<!-- vim:set ft=markdown: -->
2
3<!-- livebook:{"persist_outputs":true} -->
4
5# Day 1
6
7## Load input
8
9```elixir
10stream =
11 File.stream!("day1.txt")
12 |> Stream.map(&String.to_integer(String.trim(&1)))
13```
14
15```output
16#Stream<[
17 enum: %File.Stream{
18 line_or_bytes: :line,
19 modes: [:raw, :read_ahead, :binary],
20 path: "day1.txt",
21 raw: true
22 },
23 funs: [#Function<47.58486609/1 in Stream.map/2>]
24]>
25```
26
27## Task 1
28
29Compute count of consecutive increases
30
31```elixir
32stream
33|> Stream.chunk_every(2, 1, :discard)
34|> Enum.count(fn [a, b] -> a < b end)
35```
36
37```output
381688
39```
40
41## Task 2
42
43Compute count of consecutive increases of sums of trigrams.
44
45However we can notice, that if we have list like:
46
47$$
48[a, b, c, d]
49$$
50
51Then when we want to compare consecutive trigrams then we compare:
52
53$$
54a + b + c < b + c + d \\
55a < d
56$$
57
58So we can traverse each 4 elements and then just compare first and last one
59instead of summing and then traversing it again.
60
61```elixir
62stream
63|> Stream.chunk_every(4, 1, :discard)
64|> Enum.count(fn [a, _, _, b] -> a < b end)
65```
66
67```output
681728
69```