From cde434804f3faf2664dab66baf8a0660d549d1ea Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Thu, 2 Dec 2021 09:30:21 +0100 Subject: [PATCH] Solve 2021 Day 2 Part 1 in elixir --- 2021/Day2/Elixir/dive.ex | 41 ++++++++++++++++++++++++++++++++++++++++ 2021/Day2/Python/day2.py | 2 +- README.md | 7 ++++--- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 2021/Day2/Elixir/dive.ex diff --git a/2021/Day2/Elixir/dive.ex b/2021/Day2/Elixir/dive.ex new file mode 100644 index 0000000..2224aec --- /dev/null +++ b/2021/Day2/Elixir/dive.ex @@ -0,0 +1,41 @@ +# iex()> Day1.first + +defmodule Dive do + def load_file() do + File.read!("../input.txt") + |> String.replace(" ", "\n") + |> String.split("\n", trim: true) + end + + def dive(input) do + dive(input, {0, 0}) + end + + def dive([], {depth, horizontal}), do: depth * horizontal + + def dive(["forward", amount_str | tail], {depth, horizontal}) do + amount = String.to_integer amount_str + dive(tail, {depth, horizontal + amount}) + end + + def dive(["down", amount_str | tail], {depth, horizontal}) do + amount = String.to_integer amount_str + dive(tail, {depth + amount, horizontal}) + end + + def dive(["up", amount_str | tail], {depth, horizontal}) do + amount = String.to_integer amount_str + dive(tail, {depth - amount, horizontal}) + end + + @spec first() :: Integer + def first() do + Dive.load_file + |> dive + end + + @spec second([Integer]) :: Integer + def second(input) do + :second + end +end diff --git a/2021/Day2/Python/day2.py b/2021/Day2/Python/day2.py index 314adc5..1f80062 100644 --- a/2021/Day2/Python/day2.py +++ b/2021/Day2/Python/day2.py @@ -19,7 +19,7 @@ with open("input.txt") as f: print(f"Product: {horizontal * depth}") # Part 2 -with open("input.txt") as f: +with open("../input.txt") as f: lines = map(lambda l: l.strip(), f.readlines()) aim = 0 depth = 0 diff --git a/README.md b/README.md index 93384dc..6d14bdc 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,12 @@ # Advent of Code -This code may contain spoilers, if you haven't finished the puzzles over on https://adventofcode.com. +This code may contain spoilers, if you haven't finished the puzzles over on +https://adventofcode.com. ## Running -Paste your input in a file named `input.txt` in the root of the project of the -desired language. After that, follow the instructions for each language: +Paste your input in a file named `input.txt` in the root of the project. After +that, follow the instructions for each language: ### Rust