adamu
Advent of Code 2023 - Day 1
Nobody’s doing Advent of Code this year? ![]()
I might do the first week or so.
For Day 1, first I solved it using regular expressions and String.replace, which took about 10ms for each part.
Then I rewrote it using binaries and recursion, which got each part under 1ms.
def filter_digits2(<<>>), do: <<>>
def filter_digits2(<<x, rest::binary>>) when x in ?1..?9, do: <<x>> <> filter_digits2(rest)
def filter_digits2(<<"one", rest::binary>>), do: "1" <> filter_digits2("e" <> rest)
def filter_digits2(<<"two", rest::binary>>), do: "2" <> filter_digits2("o" <> rest)
def filter_digits2(<<"three", rest::binary>>), do: "3" <> filter_digits2("e" <> rest)
def filter_digits2(<<"four", rest::binary>>), do: "4" <> filter_digits2("r" <> rest)
def filter_digits2(<<"five", rest::binary>>), do: "5" <> filter_digits2("e" <> rest)
def filter_digits2(<<"six", rest::binary>>), do: "6" <> filter_digits2("x" <> rest)
def filter_digits2(<<"seven", rest::binary>>), do: "7" <> filter_digits2("n" <> rest)
def filter_digits2(<<"eight", rest::binary>>), do: "8" <> filter_digits2("t" <> rest)
def filter_digits2(<<"nine", rest::binary>>), do: "9" <> filter_digits2("e" <> rest)
def filter_digits2(<<_, rest::binary>>), do: filter_digits2(rest)
https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2023/day1.exs
Most Liked
Aetherus
Just tried solving Day 1 puzzles using the low-code platform developed by my company ![]()
There’s no function to split a string into lines, so I have to heavily rely on regular expressions.
By the way, the whole platform is powered by Elixir and Phoenix ![]()
kip
benchee is the go-to for benchmarking in Elixir. You can see my configuration in the bench directory in my advent_of_code repo.
To use it you would do mix run ./bench/bench.exs then sit back and relax ![]()
APB9785
My first approach didn’t handle overlaps properly in part 2, so I ended up with a somewhat funky pattern match syntax:
defp process_line(["o", "n", "e" | _] = [_h | t], ...),
do: process_line(t, ...)
There’s probably a cleaner way to do this, but it works!
Full solution here: https://github.com/APB9785/AoC-2023-elixir/blob/master/lib/day_01.ex
Popular in Challenges
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance











