A 5-post collection

Hash Keys Manipulations

Atoms, strings and numbers are three different things. But when type coercion enters the game, some languages can be confusing. Elixir: Atoms And Strings Unsurprisingly this map: %{foo: 10} is not the same as this map: %{"foo" => 10} This of course is easy to see here, but when maps are not defined in code it can be confusing. For example this code: input = IO.stream(:stdio, :line) |> Enum.at(0) |> String.trim Map.get(%{p: "Hello"}, input) |> IO.

Read more

Implementing BFS in Elixir

After 12 days of adventofcode I think I'm starting to understand Elixir's mindset, or at least I'm starting to write code I wasn't able to read 2 weeks ago. Let's try to use my BFS implementation below to explain some Elixir concepts. Graph Representation Before we can dive into the actual algorithm code for breadth first search we need to figure out a way to store the graph. The original problem input arrived in text format and looked like this: 0 <-> 2 1

Read more

Insights from Advent Of Code (Week 1)

My first week solving advent of code has almost ended, which makes it a good time to reflect on the quizzes, my solutions and other solutions I found online. tl;dr Advent of Code is a series of coding quizzes delivered daily during December. Project website is: https://adventofcode.com AOC turned out to be a very nice way to learn a new programming language (I'm learning Elixir). Most puzzles are very easy, as seen in the leaderboard times. Practising a short coding quiz every day

Read more

Solving 24 Puzzle with Elixir

A recent blog post I read reminded me how good computers are at solving puzzles. In the linked post the author solved a game called 24, in which you take 4 digits and need to find an arithmetic combinations of them to reach the number 24. Elixir's strong recursion and pattern matching capabilities are a good match to the challenge. The Gist Let's start with the code. Explanation follows: defmodule Game do def play(ops, [24]) do ops end def play(_ops, [_x]) do false end

Read more

Hello Elixir World

After spending too long trying to love lisp, I think I'm finally starting to understand the beauty in functional languages - and elixir is to blame. It's very different than most languages I know, but in an almost magical way. Here's my very first elixir program, which is a terminal based tic-tac-toe game. It's probably not that great elixir example regarding best practices, but I hope it'll show some of the magic I find in this language. A Finished Game The tic-tac-toe game should read a

Read more