diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..12179ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where 3rd-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez diff --git a/README.md b/README.md new file mode 100644 index 0000000..b7b85bb --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# AdventOfCode + +Run a specific day by executing e.g. `mix run AdventOfCode.Day01.exs` diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..13ce1b1 --- /dev/null +++ b/config/config.exs @@ -0,0 +1,30 @@ +# This file is responsible for configuring your application +# and its dependencies with the aid of the Mix.Config module. +use Mix.Config + +# This configuration is loaded before any dependency and is restricted +# to this project. If another project depends on this project, this +# file won't be loaded nor affect the parent project. For this reason, +# if you want to provide default values for your application for +# 3rd-party users, it should be done in your "mix.exs" file. + +# You can configure your application as: +# +# config :advent_of_code, key: :value +# +# and access this configuration in your application as: +# +# Application.get_env(:advent_of_code, :key) +# +# You can also configure a 3rd-party app: +# +# config :logger, level: :info +# + +# It is also possible to import configuration files, relative to this +# directory. For example, you can emulate configuration per environment +# by uncommenting the line below and defining dev.exs, test.exs and such. +# Configuration from the imported file will override the ones defined +# here (which is why it is important to import them last). +# +# import_config "#{Mix.env}.exs" diff --git a/lib/advent_of_code.ex b/lib/advent_of_code.ex new file mode 100644 index 0000000..168fa2f --- /dev/null +++ b/lib/advent_of_code.ex @@ -0,0 +1,2 @@ +defmodule AdventOfCode do +end diff --git a/lib/day01.exs b/lib/day01.exs new file mode 100644 index 0000000..a40c271 --- /dev/null +++ b/lib/day01.exs @@ -0,0 +1,34 @@ +defmodule AdventOfCode.Day01 do + def run do + IO.puts "Inverse Captcha: #{solve()}" + end + + def solve do + input() + |> String.codepoints() + |> add_first_last() + |> sum_if_adjacent(0) + end + + def sum_if_adjacent([_], sum), do: sum + def sum_if_adjacent([a, a | rest] = list, sum) do + {int, ""} = Integer.parse(a) + [a | rest] + |> sum_if_adjacent(sum + int) + end + def sum_if_adjacent([a, b | rest] = list, sum) do + [b | rest] + |> sum_if_adjacent(sum) + end + + defp add_first_last([head | _tail] = list) do + tsil = list |> Enum.reverse() + [head | tsil] |> Enum.reverse() + end + + defp input do + "lib/inputs/day1_0.txt" |> File.read!() |> String.strip() + end +end + +AdventOfCode.Day01.run() diff --git a/lib/inputs/day1_0.txt b/lib/inputs/day1_0.txt new file mode 100644 index 0000000..038bb68 --- /dev/null +++ b/lib/inputs/day1_0.txt @@ -0,0 +1 @@ +237369991482346124663395286354672985457326865748533412179778188397835279584149971999798512279429268727171755461418974558538246429986747532417846157526523238931351898548279549456694488433438982744782258279173323381571985454236569393975735715331438256795579514159946537868358735936832487422938678194757687698143224139243151222475131337135843793611742383267186158665726927967655583875485515512626142935357421852953775733748941926983377725386196187486131337458574829848723711355929684625223564489485597564768317432893836629255273452776232319265422533449549956244791565573727762687439221862632722277129613329167189874939414298584616496839223239197277563641853746193232543222813298195169345186499866147586559781523834595683496151581546829112745533347796213673814995849156321674379644323159259131925444961296821167483628812395391533572555624159939279125341335147234653572977345582135728994395631685618135563662689854691976843435785879952751266627645653981281891643823717528757341136747881518611439246877373935758151119185587921332175189332436522732144278613486716525897262879287772969529445511736924962777262394961547579248731343245241963914775991292177151554446695134653596633433171866618541957233463548142173235821168156636824233487983766612338498874251672993917446366865832618475491341253973267556113323245113845148121546526396995991171739837147479978645166417988918289287844384513974369397974378819848552153961651881528134624869454563488858625261356763562723261767873542683796675797124322382732437235544965647934514871672522777378931524994784845817584793564974285139867972185887185987353468488155283698464226415951583138352839943621294117262483559867661596299753986347244786339543174594266422815794658477629829383461829261994591318851587963554829459353892825847978971823347219468516784857348649693185172199398234123745415271222891161175788713733444497592853221743138324235934216658323717267715318744537689459113188549896737581637879552568829548365738314593851221113932919767844137362623398623853789938824592 diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..a523dea --- /dev/null +++ b/mix.exs @@ -0,0 +1,23 @@ +defmodule AdventOfCode.Mixfile do + use Mix.Project + + def project do + [ + app: :advent_of_code, + version: "0.1.0", + elixir: "~> 1.5", + start_permanent: Mix.env == :prod, + deps: deps() + ] + end + + def application do + [ + extra_applications: [:logger] + ] + end + + defp deps do + [] + end +end diff --git a/test/advent_of_code_test.exs b/test/advent_of_code_test.exs new file mode 100644 index 0000000..484afe8 --- /dev/null +++ b/test/advent_of_code_test.exs @@ -0,0 +1,4 @@ +defmodule AdventOfCodeTest do + use ExUnit.Case + doctest AdventOfCode +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()