Skip to content

Commit

Permalink
Finally solve part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
samjowen committed Dec 23, 2023
1 parent e8e8e7f commit 58aedec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
26 changes: 14 additions & 12 deletions lib/advent_of_code2023.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ defmodule AdventOfCode2023 do
def day1_solve_part_2(input_file_path) do
# Variables that we need:
replacement_map = %{

Check warning on line 21 in lib/advent_of_code2023.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "replacement_map" is unused (if the variable is not meant to be used, prefix it with an underscore)
"zero" => "0",
"one" => "1",
"two" => "2",
"three" => "3",
"four" => "4",
"five" => "5",
"six" => "6",
"seven" => "7",
"eight" => "8",
"nine" => "9"
"zero" => "zero0zero",
"one" => "one1one",
"two" => "two2two",
"three" => "three3three",
"four" => "four4four",
"five" => "five5five",
"six" => "six6six",
"seven" => "seven7seven",
"eight" => "eight8eight",
"nine" => "nine9nine"
}

string_list = Day1.parse_file(input_file_path)
IO.inspect(string_list)

replaced_number_list =
Enum.map(string_list, &Day1.replace_substrings(&1, replacement_map))
Enum.map(string_list, &Day1.convert_substrings_to_integer_strings(&1))

IO.inspect(replaced_number_list)
number_list = Enum.map(replaced_number_list, &Day1.combine_first_and_last_number/1)

IO.inspect(number_list)
Enum.sum(number_list)
end
end
40 changes: 20 additions & 20 deletions lib/day1/day1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,32 @@ defmodule Day1 do

def convert_word_to_integer_string(word) do
case word do
"zero" -> "0"
"one" -> "1"
"two" -> "2"
"three" -> "3"
"four" -> "4"
"five" -> "5"
"six" -> "6"
"seven" -> "7"
"eight" -> "8"
"nine" -> "9"
"zero" -> "zero0zero"
"one" -> "one1one"
"two" -> "two2two"
"three" -> "three3three"
"four" -> "four4four"
"five" -> "five5five"
"six" -> "six6six"
"seven" -> "seven7seven"
"eight" -> "eight8eight"
"nine" -> "nine9nine"
_ -> word
end
end

def convert_substrings_to_integer_strings(string) do
replacement_map = %{
"zero" => "0",
"one" => "1",
"two" => "2",
"three" => "3",
"four" => "4",
"five" => "5",
"six" => "6",
"seven" => "7",
"eight" => "8",
"nine" => "9"
"zero" => "zero0zero",
"one" => "one1one",
"two" => "two2two",
"three" => "three3three",
"four" => "four4four",
"five" => "five5five",
"six" => "six6six",
"seven" => "seven7seven",
"eight" => "eight8eight",
"nine" => "nine9nine"
}

sorted_map =
Expand Down
4 changes: 4 additions & 0 deletions test/day1/day1_solves_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ defmodule Day1SolvesTest do
test "solves the day 1 part II puzzle correctly" do
assert AdventOfCode2023.day1_solve_part_2("./test/day1/mocks/part2-test-case.txt") == 281
end

test "solves the day 1 part II puzzle correctly 2" do
assert AdventOfCode2023.day1_solve_part_2("./test/day1/mocks/part2-test-case-2.txt") == 79
end
end
1 change: 1 addition & 0 deletions test/day1/mocks/part2-test-case-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sevenine

0 comments on commit 58aedec

Please sign in to comment.