mirror of
https://github.com/rjNemo/ai_advent_code_2024
synced 2026-06-06 02:26:44 +00:00
test: add test for examples
This commit is contained in:
parent
2d73d1bb51
commit
901ad6c645
2 changed files with 174 additions and 4 deletions
|
|
@ -1,7 +1,84 @@
|
||||||
# Day 5: Title
|
# Day 5: Print Queue
|
||||||
|
|
||||||
[Read the problem](https://adventofcode.com/2024/day/5)
|
|
||||||
|
|
||||||
## Part 1
|
## Part 1
|
||||||
|
|
||||||
## Part 2
|
Print the safety manual pages in the correct order.
|
||||||
|
The notation X|Y means that if both page number X and page number Y are to be produced as part of an update, page number X must be printed at some point before page number Y.
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
The input contains both the page ordering rules and the pages to produce in each update.
|
||||||
|
|
||||||
|
```txt
|
||||||
|
47|53
|
||||||
|
97|13
|
||||||
|
97|61
|
||||||
|
97|47
|
||||||
|
75|29
|
||||||
|
61|13
|
||||||
|
75|53
|
||||||
|
29|13
|
||||||
|
97|29
|
||||||
|
53|29
|
||||||
|
61|53
|
||||||
|
97|53
|
||||||
|
61|29
|
||||||
|
47|13
|
||||||
|
75|47
|
||||||
|
97|75
|
||||||
|
47|61
|
||||||
|
75|61
|
||||||
|
47|29
|
||||||
|
75|13
|
||||||
|
53|13
|
||||||
|
|
||||||
|
75,47,61,53,29
|
||||||
|
97,61,53,29,13
|
||||||
|
75,29,13
|
||||||
|
75,97,47,61,53
|
||||||
|
61,13,29
|
||||||
|
97,13,75,29,47
|
||||||
|
```
|
||||||
|
|
||||||
|
The first section specifies the page ordering rules, one per line. The first rule, `47|53`, means that if an update includes both page number 47 and page number 53, then page number 47 must be printed at some point before page number 53. (47 doesn't necessarily need to be immediately before 53; other pages are allowed to be between them.)
|
||||||
|
|
||||||
|
The second section specifies the page numbers included in each update. Because most safety manuals are different, the pages needed in the updates are different too. The first update, `75,47,61,53,29`, means that the update consists of page numbers 75, 47, 61, 53, and 29.
|
||||||
|
|
||||||
|
To get the printers going as soon as possible, start by identifying which updates are already in the right order.
|
||||||
|
|
||||||
|
In the above example, the first update (75,47,61,53,29) is in the right order:
|
||||||
|
|
||||||
|
- 75 is correctly first because there are rules that put each other page after it: 75|47, 75|61, 75|53, and 75|29.
|
||||||
|
- 47 is correctly second because 75 must be before it (75|47) and every other page must be after it according to 47|61, 47|53, and 47|29.
|
||||||
|
- 61 is correctly in the middle because 75 and 47 are before it (75|61 and 47|61) and 53 and 29 are after it (61|53 and 61|29).
|
||||||
|
- 53 is correctly fourth because it is before page number 29 (53|29).
|
||||||
|
- 29 is the only page left and so is correctly last.
|
||||||
|
|
||||||
|
Because the first update does not include some page numbers, the ordering rules involving those missing page numbers are ignored.
|
||||||
|
|
||||||
|
The second and third updates are also in the correct order according to the rules. Like the first update, they also do not include every page number, and so only some of the ordering rules apply - within each update, the ordering rules that involve missing page numbers are not used.
|
||||||
|
|
||||||
|
The fourth update, 75,97,47,61,53, is not in the correct order: it would print 75 before 97, which violates the rule 97|75.
|
||||||
|
|
||||||
|
The fifth update, 61,13,29, is also not in the correct order, since it breaks the rule 29|13.
|
||||||
|
|
||||||
|
The last update, 97,13,75,29,47, is not in the correct order due to breaking several rules.
|
||||||
|
|
||||||
|
For some reason, the Elves also need to know the middle page number of each update being printed. Because you are currently only printing the correctly-ordered updates, you will need to find the middle page number of each correctly-ordered update. In the above example, the correctly-ordered updates are:
|
||||||
|
|
||||||
|
```
|
||||||
|
75,47,61,53,29
|
||||||
|
97,61,53,29,13
|
||||||
|
75,29,13
|
||||||
|
```
|
||||||
|
|
||||||
|
These have middle page numbers of 61, 53, and 29 respectively. Adding these page numbers together gives `143`.
|
||||||
|
|
||||||
|
Of course, you'll need to be careful: the actual list of page ordering rules is bigger and more complicated than the above example.
|
||||||
|
|
||||||
|
Determine which updates are already in the correct order. What do you get if you add up the middle page number from those correctly-ordered updates?
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Part 2
|
||||||
93
test/advent_code2024/solutions/day05/day5_test.exs
Normal file
93
test/advent_code2024/solutions/day05/day5_test.exs
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
defmodule AdventCode2024.Solutions.Day5Test do
|
||||||
|
use ExUnit.Case
|
||||||
|
alias AdventCode2024.Solutions.Day5
|
||||||
|
|
||||||
|
@example_input """
|
||||||
|
47|53
|
||||||
|
97|13
|
||||||
|
97|61
|
||||||
|
97|47
|
||||||
|
75|29
|
||||||
|
61|13
|
||||||
|
75|53
|
||||||
|
29|13
|
||||||
|
97|29
|
||||||
|
53|29
|
||||||
|
61|53
|
||||||
|
97|53
|
||||||
|
61|29
|
||||||
|
47|13
|
||||||
|
75|47
|
||||||
|
97|75
|
||||||
|
47|61
|
||||||
|
75|61
|
||||||
|
47|29
|
||||||
|
75|13
|
||||||
|
53|13
|
||||||
|
|
||||||
|
75,47,61,53,29
|
||||||
|
97,61,53,29,13
|
||||||
|
75,29,13
|
||||||
|
75,97,47,61,53
|
||||||
|
61,13,29
|
||||||
|
97,13,75,29,47
|
||||||
|
"""
|
||||||
|
|
||||||
|
describe "solve/1" do
|
||||||
|
test "correctly identifies valid updates and sums middle pages" do
|
||||||
|
assert {:ok, 143} = Day5.solve_content(@example_input)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns error for empty input" do
|
||||||
|
assert Day5.solve("") == {:error, :no_input}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns error for invalid file" do
|
||||||
|
assert Day5.solve("priv/nonexistent.txt") == {:error, :enoent}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "validates first update is in correct order" do
|
||||||
|
input = """
|
||||||
|
75|47
|
||||||
|
75|61
|
||||||
|
75|53
|
||||||
|
75|29
|
||||||
|
47|61
|
||||||
|
47|53
|
||||||
|
47|29
|
||||||
|
61|53
|
||||||
|
61|29
|
||||||
|
53|29
|
||||||
|
|
||||||
|
75,47,61,53,29
|
||||||
|
"""
|
||||||
|
assert {:ok, 61} = Day5.solve_content(input)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "validates second update is in correct order" 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
|
||||||
|
"""
|
||||||
|
assert {:ok, 53} = Day5.solve_content(input)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "identifies invalid update due to rule violation" do
|
||||||
|
input = """
|
||||||
|
97|75
|
||||||
|
|
||||||
|
75,97,47,61,53
|
||||||
|
"""
|
||||||
|
assert {:ok, 0} = Day5.solve_content(input)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue