Skip to content

Commit

Permalink
advent of code 2024 day 02 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
wasi0013 committed Dec 2, 2024
1 parent 4c27532 commit 455ecb1
Show file tree
Hide file tree
Showing 4 changed files with 1,057 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ Run all tests:
<th>Status</th>
</tr>

<tr>
<td><a href="https://adventofcode.com/2024/day/2">2024</a></td>
<td><a href='https://github.com/wasi0013/advent_of_code/blob/master/lib/y2024/day_02.ex'> 2</a></td>
<td> :green_circle: :green_circle: </td>



</tr>

<tr>
<td><a href="https://adventofcode.com/2024/day/1">2024</a></td>
<td><a href='https://github.com/wasi0013/advent_of_code/blob/master/lib/y2024/day_01.ex'> 1</a></td>
Expand Down
35 changes: 35 additions & 0 deletions lib/y2024/day_02.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Aoc.Y2024.Day02 do
@moduledoc """
Solved https://adventofcode.com/2024/day/2
"""
import Aoc.Helper.IO

def solve_part1(data), do: data |> Enum.map(&process/1) |> Enum.sum()
def solve_part2(data), do: data |> Enum.map(&dampen/1) |> Enum.sum()

def process([], _prev, _flag, result), do: result

def process([h | list], prev, true, result) when h + 1 == prev or h + 2 == prev or h + 3 == prev,
do: process(list, h, true, result)

def process([h | list], prev, false, result) when h - 1 == prev or h - 2 == prev or h - 3 == prev,
do: process(list, h, false, result)

def process(_list, _prev, _flag, _result), do: 0
def process([h | list]), do: process(list, h, h > hd(list), 1)

def dampen(list),
do:
0..length(list)
|> Enum.map(fn x -> list |> List.delete_at(x) |> process() end)
|> Enum.sum()
|> then(fn x -> if x > 0, do: 1, else: 0 end)

def get_input(),
do:
get_string_input("2024", "02")
|> String.split("\n")
|> Enum.map(fn s -> String.split(s, " ") |> Enum.map(&String.to_integer/1) end)

def solved_status(), do: :solved
end
Loading

0 comments on commit 455ecb1

Please sign in to comment.