Skip to content

Commit 4c6aa0b

Browse files
committed
feat: Solving day 5
1 parent e02fab5 commit 4c6aa0b

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

day5/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
3+
---
4+
5+
# Day 5: Print Queue
6+
7+
Satisfied with their search on Ceres, the squadron of scholars suggests subsequently scanning the stationery stacks of sub-basement 17.
8+
9+
The North Pole printing department is busier than ever this close to Christmas, and while The Historians continue their search of this historically significant facility, an Elf operating a very familiar printer beckons you over.
10+
11+
The Elf must recognize you, because they waste no time explaining that the new sleigh launch safety manual updates won't print correctly. Failure to update the safety manuals would be dire indeed, so you offer your services.
12+
13+
Safety protocols clearly indicate that new pages for the safety manuals must be printed in a very specific 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`.
14+
15+
The Elf has for you both the page ordering rules and the pages to produce in each update (your puzzle input), but can't figure out whether each update has the pages in the right order.
16+
17+
For example:
18+
19+
```txt
20+
47|53
21+
97|13
22+
97|61
23+
97|47
24+
75|29
25+
61|13
26+
75|53
27+
29|13
28+
97|29
29+
53|29
30+
61|53
31+
97|53
32+
61|29
33+
47|13
34+
75|47
35+
97|75
36+
47|61
37+
75|61
38+
47|29
39+
75|13
40+
53|13
41+
42+
75,47,61,53,29
43+
97,61,53,29,13
44+
75,29,13
45+
75,97,47,61,53
46+
61,13,29
47+
97,13,75,29,47
48+
```
49+
50+
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.)
51+
52+
The second section specifies the page numbers of 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`.
53+
54+
To get the printers going as soon as possible, start by identifying which updates are already in the right order.
55+
56+
In the above example, the first update `(75,47,61,53,29)` is in the right order:
57+
58+
* `75` is correctly first because there are rules that put each other page after it: `75|47`, `75|61`, `75|53`, and `75|29`.
59+
* `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`.
60+
* `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`).
61+
* `53` is correctly fourth because it is before page number 29 (`53|29`).
62+
* `29` is the only page left and so is correctly last.
63+
64+
Because the first update does not include some page numbers, the ordering rules involving those missing page numbers are ignored.
65+
66+
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.
67+
68+
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`.
69+
70+
The fifth update, `61,13,29`, is also not in the correct order, since it breaks the rule `29|13`.
71+
72+
The last update, `97,13,75,29,47`, is not in the correct order due to breaking several rules.
73+
74+
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+
76+
```txt
77+
75,47,61,53,29
78+
97,61,53,29,13
79+
75,29,13
80+
```
81+
82+
These have middle page numbers of `61`, `53`, and `29` respectively. Adding these page numbers together gives `143`.
83+
84+
Of course, you'll need to be careful: the actual list of page ordering rules is bigger and more complicated than the above example.
85+
86+
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?

day5/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/truggeri/adventofcode2024/day5
2+
3+
go 1.23.4

day5/main.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type pageNumber uint
11+
type rule pageNumber
12+
type batch []pageNumber
13+
14+
func main() {
15+
input := `47|53
16+
97|13
17+
97|61
18+
97|47
19+
75|29
20+
61|13
21+
75|53
22+
29|13
23+
97|29
24+
53|29
25+
61|53
26+
97|53
27+
61|29
28+
47|13
29+
75|47
30+
97|75
31+
47|61
32+
75|61
33+
47|29
34+
75|13
35+
53|13
36+
37+
75,47,61,53,29
38+
97,61,53,29,13
39+
75,29,13
40+
75,97,47,61,53
41+
61,13,29
42+
97,13,75,29,47`
43+
fmt.Println("Solution:", Solve(input))
44+
}
45+
46+
func Solve(input string) uint {
47+
rules, batches := parseInput(input)
48+
var count uint = 0
49+
for _, b := range batches {
50+
count += uint(medianMatchingPage(rules, b))
51+
}
52+
return count
53+
}
54+
55+
func parseInput(input string) (map[pageNumber][]rule, []batch) {
56+
rules := make(map[pageNumber][]rule)
57+
batches := make([]batch, 0)
58+
parsingRules := true
59+
b := 0
60+
61+
for _, i := range strings.Split(input, "\n") {
62+
if i == "" {
63+
parsingRules = false
64+
} else if parsingRules {
65+
ruleValues := strings.Split(i, "|")
66+
if len(ruleValues) != 2 {
67+
continue
68+
}
69+
70+
x, err := strconv.Atoi(ruleValues[0])
71+
if err != nil {
72+
continue
73+
}
74+
y, err := strconv.Atoi(ruleValues[1])
75+
if err != nil {
76+
continue
77+
}
78+
rules[pageNumber(x)] = slices.Insert(rules[pageNumber(x)], len(rules[pageNumber(x)]), rule(y))
79+
} else {
80+
pageNumbers := strings.Split(i, ",")
81+
batches = slices.Insert(batches, b, make(batch, 0))
82+
for _, num := range pageNumbers {
83+
x, err := strconv.Atoi(num)
84+
if err != nil {
85+
continue
86+
}
87+
batches[b] = slices.Insert(batches[b], len(batches[b]), pageNumber(x))
88+
}
89+
b += 1
90+
}
91+
}
92+
93+
return rules, batches
94+
}
95+
96+
func medianMatchingPage(rules map[pageNumber][]rule, b batch) pageNumber {
97+
if !isBatchValid(rules, b) {
98+
return 0
99+
}
100+
101+
return b[len(b)/2]
102+
}
103+
104+
func isBatchValid(rules map[pageNumber][]rule, b batch) bool {
105+
pagesPrintedAlready := make(map[pageNumber]bool)
106+
for _, p := range b {
107+
for _, r := range rules[p] {
108+
if pagesPrintedAlready[pageNumber(r)] {
109+
return false
110+
}
111+
}
112+
pagesPrintedAlready[p] = true
113+
}
114+
return true
115+
}

day5/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSample(t *testing.T) {
8+
input := `47|53
9+
97|13
10+
97|61
11+
97|47
12+
75|29
13+
61|13
14+
75|53
15+
29|13
16+
97|29
17+
53|29
18+
61|53
19+
97|53
20+
61|29
21+
47|13
22+
75|47
23+
97|75
24+
47|61
25+
75|61
26+
47|29
27+
75|13
28+
53|13
29+
30+
75,47,61,53,29
31+
97,61,53,29,13
32+
75,29,13
33+
75,97,47,61,53
34+
61,13,29
35+
97,13,75,29,47`
36+
result := Solve(input)
37+
if result != 143 {
38+
t.Errorf("Calculated solution was not expected")
39+
}
40+
}

0 commit comments

Comments
 (0)