Browse Source

Solve 2021 Day 2 Part 1 in elixir

master
Garrit Franke 9 months ago
parent
commit
cde434804f
Signed by: garrit
GPG Key ID: 65586C4DDA55EA2C
  1. 41
      2021/Day2/Elixir/dive.ex
  2. 2
      2021/Day2/Python/day2.py
  3. 7
      README.md

41
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

2
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

7
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

Loading…
Cancel
Save