Skip to content

Commit cde4348

Browse files
committed
Solve 2021 Day 2 Part 1 in elixir
1 parent f7bb61b commit cde4348

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

2021/Day2/Elixir/dive.ex

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# iex()> Day1.first
2+
3+
defmodule Dive do
4+
def load_file() do
5+
File.read!("../input.txt")
6+
|> String.replace(" ", "\n")
7+
|> String.split("\n", trim: true)
8+
end
9+
10+
def dive(input) do
11+
dive(input, {0, 0})
12+
end
13+
14+
def dive([], {depth, horizontal}), do: depth * horizontal
15+
16+
def dive(["forward", amount_str | tail], {depth, horizontal}) do
17+
amount = String.to_integer amount_str
18+
dive(tail, {depth, horizontal + amount})
19+
end
20+
21+
def dive(["down", amount_str | tail], {depth, horizontal}) do
22+
amount = String.to_integer amount_str
23+
dive(tail, {depth + amount, horizontal})
24+
end
25+
26+
def dive(["up", amount_str | tail], {depth, horizontal}) do
27+
amount = String.to_integer amount_str
28+
dive(tail, {depth - amount, horizontal})
29+
end
30+
31+
@spec first() :: Integer
32+
def first() do
33+
Dive.load_file
34+
|> dive
35+
end
36+
37+
@spec second([Integer]) :: Integer
38+
def second(input) do
39+
:second
40+
end
41+
end

2021/Day2/Python/day2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
print(f"Product: {horizontal * depth}")
2020

2121
# Part 2
22-
with open("input.txt") as f:
22+
with open("../input.txt") as f:
2323
lines = map(lambda l: l.strip(), f.readlines())
2424
aim = 0
2525
depth = 0

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Advent of Code
22

3-
This code may contain spoilers, if you haven't finished the puzzles over on https://adventofcode.com.
3+
This code may contain spoilers, if you haven't finished the puzzles over on
4+
https://adventofcode.com.
45

56
## Running
67

7-
Paste your input in a file named `input.txt` in the root of the project of the
8-
desired language. After that, follow the instructions for each language:
8+
Paste your input in a file named `input.txt` in the root of the project. After
9+
that, follow the instructions for each language:
910

1011
### Rust
1112

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy