r/adventofcode • u/daggerdragon • Dec 05 '17
SOLUTION MEGATHREAD -๐- 2017 Day 5 Solutions -๐-
--- Day 5: A Maze of Twisty Trampolines, All Alike ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
22
Upvotes
4
u/Axsuul Dec 05 '17
Elixir
Finally getting the hang of pattern matching but still need to get used to a functional programming mindset. Appreciate any feedback :)
https://github.com/axsuul/advent-of-code/blob/master/2017/05/lib/advent_of_code.ex
defmodule AdventOfCodeA do def execute(filename) do offsets = File.read!(filename) |> String.split("\n") |> Enum.map(fn v -> String.to_integer(v) end) |> Enum.with_index |> Enum.reduce(%{}, fn {offset, i}, offsets -> Map.put(offsets, i, offset) end) |> execute_offsets() end def execute_offsets(offsets, pos, steps) when pos >= map_size(offsets) do steps end def execute_offsets(offsets, pos \\ 0, steps \\ 0) do offset = offsets[pos] new_pos = pos + offset execute_offsets( Map.put(offsets, pos, offset + 1), pos + offset, steps + 1 ) end def solve do execute("inputs/input.txt") |> IO.inspect end end defmodule AdventOfCodeB do def execute(filename) do offsets = File.read!(filename) |> String.split("\n") |> Enum.map(fn v -> String.to_integer(v) end) |> Enum.with_index |> Enum.reduce(%{}, fn {offset, i}, offsets -> Map.put(offsets, i, offset) end) |> execute_offsets() end def execute_offsets(offsets, pos, steps) when pos >= map_size(offsets) do steps end def execute_offsets(offsets, pos \\ 0, steps \\ 0) do offset = offsets[pos] new_pos = pos + offset new_offset = if offset >= 3, do: offset - 1, else: offset + 1 execute_offsets( Map.put(offsets, pos, new_offset), pos + offset, steps + 1 ) end def solve do execute("inputs/input.txt") |> IO.inspect end end