-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_02.ex
31 lines (24 loc) · 1.24 KB
/
day_02.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defmodule Aoc.Y2021.Day02 do
@moduledoc """
Solved https://adventofcode.com/2021/day/2
"""
import Aoc.Helper.IO
def solve_part1(data), do: data |> plan_course(0, 0)
def plan_course([], width, depth), do: width * depth
def plan_course([["forward", value] | rest], width, depth), do: plan_course(rest, width + value, depth)
def plan_course([["down", value] | rest], width, depth), do: plan_course(rest, width, depth + value)
def plan_course([["up", value] | rest], width, depth), do: plan_course(rest, width, depth - value)
def solve_part2(data), do: data |> process_aim(0, 0, 0)
def process_aim([], width, depth, _aim), do: width * depth
def process_aim([["forward", value] | rest], width, depth, aim),
do: process_aim(rest, width + value, depth + aim * value, aim)
def process_aim([["down", value] | rest], width, depth, aim), do: process_aim(rest, width, depth, aim + value)
def process_aim([["up", value] | rest], width, depth, aim), do: process_aim(rest, width, depth, aim - value)
def get_input(),
do:
get_string_input("2021", "02")
|> String.split("\n")
|> Enum.map(&String.split(&1, " "))
|> Enum.map(fn [ins, value] -> [ins, String.to_integer(value)] end)
def solved_status(), do: :solved
end