From fbc39fe430e4ddbcf6a4b794b3a371c23be95a7c Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 5 Dec 2024 11:42:53 +0100 Subject: [PATCH] test: add tests for part2 example --- .../solutions/day05/day5_test.exs | 72 ++++++++++++++++++- test/fixtures/day05/part2_first_update.txt | 5 ++ test/fixtures/day05/part2_ignore_valid.txt | 12 ++++ test/fixtures/day05/part2_second_update.txt | 5 ++ test/fixtures/day05/part2_third_update.txt | 5 ++ 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/day05/part2_first_update.txt create mode 100644 test/fixtures/day05/part2_ignore_valid.txt create mode 100644 test/fixtures/day05/part2_second_update.txt create mode 100644 test/fixtures/day05/part2_third_update.txt diff --git a/test/advent_code2024/solutions/day05/day5_test.exs b/test/advent_code2024/solutions/day05/day5_test.exs index 481c66f..c326cba 100644 --- a/test/advent_code2024/solutions/day05/day5_test.exs +++ b/test/advent_code2024/solutions/day05/day5_test.exs @@ -74,7 +74,6 @@ defmodule AdventCode2024.Solutions.Day05Test do 75,47,61,53,29 """ - path = "test/fixtures/day05/first_update.txt" :ok = File.write(path, input) assert {:ok, 61} = Day05.solve(path) @@ -95,7 +94,6 @@ defmodule AdventCode2024.Solutions.Day05Test do 97,61,53,29,13 """ - path = "test/fixtures/day05/second_update.txt" :ok = File.write(path, input) assert {:ok, 53} = Day05.solve(path) @@ -108,11 +106,79 @@ defmodule AdventCode2024.Solutions.Day05Test do 75,97,47,61,53 """ - path = "test/fixtures/day05/invalid_update.txt" :ok = File.write(path, input) assert {:ok, 0} = Day05.solve(path) File.rm!(path) end end + + describe "solve_part2/1" do + test "correctly orders invalid updates and sums middle pages", %{test_file: path} do + assert {:ok, 123} = Day05.solve_part2(path) + end + + test "correctly orders first invalid update" do + input = """ + 75|47 + 75|29 + 47|29 + + 75,47,29 + """ + path = "test/fixtures/day05/part2_first_update.txt" + :ok = File.write(path, input) + assert {:ok, 47} = Day05.solve_part2(path) + File.rm!(path) + end + + test "correctly orders second invalid update" do + input = """ + 97|13 + 97|29 + 29|13 + + 97,29,13 + """ + path = "test/fixtures/day05/part2_second_update.txt" + :ok = File.write(path, input) + assert {:ok, 29} = Day05.solve_part2(path) + File.rm!(path) + end + + test "correctly orders third invalid update" do + input = """ + 97|75 + 97|47 + 75|47 + + 97,75,47 + """ + path = "test/fixtures/day05/part2_third_update.txt" + :ok = File.write(path, input) + assert {:ok, 47} = Day05.solve_part2(path) + File.rm!(path) + end + + test "ignores already correctly ordered updates" do + input = """ + 97|13 + 97|61 + 97|53 + 97|29 + 61|13 + 61|53 + 61|29 + 53|29 + 29|13 + + 97,61,53,29,13 + 61,53,29,13 + """ + path = "test/fixtures/day05/part2_ignore_valid.txt" + :ok = File.write(path, input) + assert {:ok, 0} = Day05.solve_part2(path) + File.rm!(path) + end + end end diff --git a/test/fixtures/day05/part2_first_update.txt b/test/fixtures/day05/part2_first_update.txt new file mode 100644 index 0000000..62a7374 --- /dev/null +++ b/test/fixtures/day05/part2_first_update.txt @@ -0,0 +1,5 @@ +75|47 +75|29 +47|29 + +75,47,29 diff --git a/test/fixtures/day05/part2_ignore_valid.txt b/test/fixtures/day05/part2_ignore_valid.txt new file mode 100644 index 0000000..5ef6426 --- /dev/null +++ b/test/fixtures/day05/part2_ignore_valid.txt @@ -0,0 +1,12 @@ +97|13 +97|61 +97|53 +97|29 +61|13 +61|53 +61|29 +53|29 +29|13 + +97,61,53,29,13 +61,53,29,13 diff --git a/test/fixtures/day05/part2_second_update.txt b/test/fixtures/day05/part2_second_update.txt new file mode 100644 index 0000000..396e11f --- /dev/null +++ b/test/fixtures/day05/part2_second_update.txt @@ -0,0 +1,5 @@ +97|13 +97|29 +29|13 + +97,29,13 diff --git a/test/fixtures/day05/part2_third_update.txt b/test/fixtures/day05/part2_third_update.txt new file mode 100644 index 0000000..c2eca98 --- /dev/null +++ b/test/fixtures/day05/part2_third_update.txt @@ -0,0 +1,5 @@ +97|75 +97|47 +75|47 + +97,75,47