-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day10.ex
36 lines (32 loc) · 963 Bytes
/
Day10.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
32
33
34
35
36
defmodule Day10 do
use AOC
def execute("noop", {cycles, register}), do: {cycles ++ [register], register}
def execute("addx " <> value, {cycles, register}) do
updated_register = register + String.to_integer(value)
{cycles ++ [register, updated_register], updated_register}
end
def part1 do
input(10)
|> String.split("\n", trim: true)
|> Enum.reduce({[1], 1}, &execute/2)
|> elem(0)
|> (&Enum.slice(&1, 19..length(&1))).()
|> Enum.take_every(40)
|> Enum.zip(20..220//40)
|> Enum.reduce(0, fn {register, cycle}, signal -> signal + register * cycle end)
end
def part2 do
input(10)
|> String.trim
|> String.split("\n", trim: true)
|> Enum.reduce({[1], 1}, &execute/2)
|> elem(0)
|> Enum.with_index
|> Enum.map(fn
{register, position} when register - rem(position, 40) in -1..1 -> "#"
_ -> "."
end)
|> Enum.chunk_every(40)
|> Enum.map(&Enum.join/1)
end
end