Skip to content

Commit

Permalink
advent of code 2024 day 01 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
wasi0013 committed Dec 1, 2024
1 parent 4bcba9d commit 489690f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.17.3-otp-27
erlang 27.1.2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Run all tests:
<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>
<td> :green_circle: :green_circle: </td>

<td> :white_circle: :white_circle: </td>


</tr>
Expand Down
11 changes: 7 additions & 4 deletions lib/y2024/day_01.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Aoc.Y2024.Day01 do
defmodule Aoc.Y2024.Day01 do
@moduledoc """
Solved https://adventofcode.com/2024/day/1
"""
Expand All @@ -8,14 +8,17 @@ defmodule Aoc.Y2024.Day01 do
def solve_part1(data) do
data
|> rotate90()
|> then (fn [list1, list2] -> [Enum.sort(list1), Enum.sort(list2)] |> Enum.zip() |> Enum.map(&Tuple.to_list/1) |> Enum.map(fn [a, b] -> abs(a - b) end) |> Enum.sum() end)

|> Enum.map(&Enum.sort/1)
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
|> Enum.map(fn [a, b] -> abs(a - b) end)
|> Enum.sum()
end

def solve_part2(data) do
data
|> rotate90()
|> then (fn [list1, list2] -> Enum.map(list1, fn a -> a * Enum.count(list2, &(a==&1)) end) |> Enum.sum() end)
|> then(fn [list1, list2] -> Enum.map(list1, fn a -> a * Enum.count(list2, &(a == &1)) end) |> Enum.sum() end)
end

def get_input() do
Expand Down
2 changes: 1 addition & 1 deletion test/y2024/day_01_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule AocTest.Y2024.Day01Test do
import Aoc.Y2024.Day01

test "Part 1 is solved" do
assert solve_part1(get_input()) == 1388114
assert solve_part1(get_input()) == 1_388_114
end

test "Part 2 is solved" do
Expand Down

0 comments on commit 489690f

Please sign in to comment.