Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
fix(day1-aoc-daniel.md): improve code readability and maintainability…
Browse files Browse the repository at this point in the history
… by refactoring the solution using pipe chains and reduce function

fix(list.html): update HTML structure and add hover effect to post items for better user experience
  • Loading branch information
Daniel-Boll committed Dec 1, 2023
1 parent 0a7c007 commit fbbed7e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
47 changes: 37 additions & 10 deletions content/projects/day1-aoc-daniel.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ description: "Using Elixir & OCaml"
draft: false
---

Uh, hi?
Here am I once again saving Christmas, duty's calling amirite?

The `Trebuchet` challenge from Advent of Code already got me started in the Pipe chain rabbit hole in Elixir, my first solution was simple

```elixir
defmodule Day1 do
@spec part_one(String.t()) :: integer()
def part_one(file) do
load_file(file)
|> Enum.map(fn calibration ->
load_file(file) # Load the file, LOL
|> Enum.map(fn calibration -> # Then I would map for each line
numbers =
calibration
|> String.split("")
|> Enum.filter(&Regex.match?(~r/\d/, &1))
|> String.split("") # Split the characters of each line
|> Enum.filter(&Regex.match?(~r/\d/, &1)) # So I can filter only the digits

# Resulting in something like ["1", "2", "5"], ["7"]

numbers
|> Kernel.hd()
|> Kernel.<>(List.last(numbers))
|> String.to_integer()
numbers # Now the plan was to concatenate the first and last digits of this list
|> Kernel.hd() # Taking the head
|> Kernel.<>(List.last(numbers)) # And concatenating into the last digit
|> String.to_integer() # Converting into integer
end)
|> Enum.sum()
|> Enum.sum() # And then summing in the end
end

@spec load_file(String.t()) :: [String.t()]
Expand All @@ -35,3 +39,26 @@ defmodule Day1 do
end
end
```

Of course not much later I would spend a bit more time to try to achieve through a full pipe chain, and I ended up with this:

```elixir
defmodule Day1 do
@spec part_one(String.t()) :: integer()
def part_one(file) do
File.read!(file)
|> String.trim_trailing()
|> String.split("\n")
|> Enum.reduce(0, fn calibration, acc -> # Instead of mapping then summing, a reduce fits better
~r/\d/
|> Regex.scan(calibration) # I know scan for the digits I want, but the scan returns me:
# A list of lists is returned, where each entry in the primary list represents a
# match and each entry in the secondary list represents the captured contents.
|> List.flatten()
|> (&(List.first(&1) <> List.last(&1))).() # From this list I again concatenate (<>) the first and last element
|> String.to_integer() # Convert to integer
|> Kernel.+(acc) # And increment the reduce accumulator
end)
end
end
```
41 changes: 29 additions & 12 deletions themes/tuna/layouts/_default/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@
{{ .Content }}
</p>
{{ range .Pages }}
<div class="post">
<div class="author-name">
[{{ .Params.author }}]
<a href="{{ .Permalink }}" class="post-link">
<div class="post">
<div class="author-name">
[{{ .Params.author }}]
</div>
<div class="headline">
<span>{{ .Title }}</span> :: {{ .Description }}
</div>
<div class="sub-sub-title">
<span class="date">
{{ .Date.Format "2006 January 2" }}
</span>
</div>
</div>
<div class="headline">
<a href="{{ .Permalink }}">{{ .Title }}</a> :: {{ .Description }}
</div>
<div class="sub-sub-title">
<span class="date">
{{ .Date.Format "2006 January 2" }}
</span>
</div>
</div>
</a>
{{ end }}
</div>
</div>

<style>
.post-link {
text-decoration: none;
}

.post {
display: flex;
flex-direction: column;
Expand All @@ -42,13 +49,23 @@
text-decoration: none;
border: 1px solid rgba(27,31,35,0.1);
max-width: 20rem;

transition: background 0.2s ease;
}

.post:hover {
background: rgba(27,31,35,0.1);
}

.post .sub-sub-title {
font-size: 0.8em;
color: #586069;
}

.headline span {
text-decoration: underline;
}

.author-name {
font-weight: bold;
font-size: 1.2em;
Expand Down

0 comments on commit fbbed7e

Please sign in to comment.