Day 02
Input
input = Kino.Input.textarea("Problem Input:")
input_arr =
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.map(fn
"forward " <> num -> {:forward, num}
"down " <> num -> {:down, num}
"up " <> num -> {:up, num}
end)
|> Enum.map(fn {dir, n} -> {dir, String.to_integer(n)} end)
[
forward: 6,
down: 8,
down: 5,
down: 9,
forward: 2,
down: 5,
down: 5,
forward: 1,
forward: 7,
down: 8,
up: 2,
down: 4,
up: 8,
down: 8,
forward: 3,
forward: 4,
down: 1,
forward: 5,
up: 7,
down: 7,
down: 8,
forward: 2,
up: 3,
forward: 1,
forward: 6,
forward: 9,
forward: 7,
forward: 8,
forward: 2,
forward: 3,
up: 2,
up: 8,
down: 1,
forward: 7,
down: 7,
down: 2,
forward: 6,
down: 1,
forward: 5,
down: 3,
forward: 6,
down: 7,
up: 1,
up: 3,
forward: 7,
forward: 6,
forward: 8,
down: 4,
down: 2,
up: 5,
...
]
Problem 01
{x, y} =
input_arr
|> Enum.reduce(
{0, 0},
fn
{:forward, n}, {x, y} -> {x + n, y}
{:up, n}, {x, y} -> {x, y - n}
{:down, n}, {x, y} -> {x, y + n}
end
)
{1890, 1172}
Output
x * y
2215080
Problem 02
{x, y, aim} =
input_arr
|> Enum.reduce(
{0, 0, 0},
fn
{:forward, n}, {x, y, aim} -> {x + n, y + aim * n, aim}
{:up, n}, {x, y, aim} -> {x, y, aim - n}
{:down, n}, {x, y, aim} -> {x, y, aim + n}
end
)
{1890, 986622, 1172}
Output
x * y
1864715580