Skip to content

Commit 1a37568

Browse files
committed
Day 5: Just before the buzzer!
1 parent 7ae6601 commit 1a37568

File tree

4 files changed

+395
-0
lines changed

4 files changed

+395
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Each day will be setup as a separate folder.
1111
- [Day 2](/day-2/) - Red-Nosed Reports
1212
- [Day 3](/day-3/) - Mull It Over
1313
- [Day 4](/day-4/) - Ceres Search
14+
- [Day 5](/day-5/) - Print Queue
1415

1516
## Environment Setup
1617

day-5/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Day 5 - Print Queue
2+
3+
## Part 1
4+
5+
Satisfied with their search on Ceres, the squadron of scholars suggests subsequently scanning the stationery stacks of sub-basement 17.
6+
7+
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.
8+
9+
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.
10+
11+
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.
12+
13+
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.
14+
15+
For example:
16+
17+
```
18+
47|53
19+
97|13
20+
97|61
21+
97|47
22+
75|29
23+
61|13
24+
75|53
25+
29|13
26+
97|29
27+
53|29
28+
61|53
29+
97|53
30+
61|29
31+
47|13
32+
75|47
33+
97|75
34+
47|61
35+
75|61
36+
47|29
37+
75|13
38+
53|13
39+
40+
75,47,61,53,29
41+
97,61,53,29,13
42+
75,29,13
43+
75,97,47,61,53
44+
61,13,29
45+
97,13,75,29,47
46+
```
47+
48+
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.)
49+
50+
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.
51+
52+
To get the printers going as soon as possible, start by identifying which updates are already in the right order.
53+
54+
In the above example, the first update (75,47,61,53,29) is in the right order:
55+
56+
- 75 is correctly first because there are rules that put each other page after it: 75|47, 75|61, 75|53, and 75|29.
57+
- 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.
58+
- 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).
59+
- 53 is correctly fourth because it is before page number 29 (53|29).
60+
- 29 is the only page left and so is correctly last.
61+
62+
Because the first update does not include some page numbers, the ordering rules involving those missing page numbers are ignored.
63+
64+
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.
65+
66+
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.
67+
68+
The fifth update, 61,13,29, is also not in the correct order, since it breaks the rule 29|13.
69+
70+
The last update, 97,13,75,29,47, is not in the correct order due to breaking several rules.
71+
72+
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:
73+
74+
```
75+
75,47,61,53,29
76+
97,61,53,29,13
77+
75,29,13
78+
```
79+
80+
These have middle page numbers of 61, 53, and 29 respectively. Adding these page numbers together gives 143.
81+
82+
Of course, you'll need to be careful: the actual list of page ordering rules is bigger and more complicated than the above example.
83+
84+
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?
85+
86+
## Part 2
87+
88+
While the Elves get to work printing the correctly-ordered updates, you have a little time to fix the rest of them.
89+
90+
For each of the incorrectly-ordered updates, use the page ordering rules to put the page numbers in the right order. For the above example, here are the three incorrectly-ordered updates and their correct orderings:
91+
92+
- 75,97,47,61,53 becomes 97,75,47,61,53.
93+
- 61,13,29 becomes 61,29,13.
94+
- 97,13,75,29,47 becomes 97,75,47,29,13.
95+
96+
After taking only the incorrectly-ordered updates and ordering them correctly, their middle page numbers are 47, 29, and 47. Adding these together produces 123.
97+
98+
Find the updates which are not in the correct order. What do you get if you add up the middle page numbers after correctly ordering just those updates?

day-5/main.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"slices"
9+
"strconv"
10+
"strings"
11+
12+
file "github.com/shaunburdick/advent-of-code-2024/lib"
13+
)
14+
15+
var input string
16+
17+
func init() {
18+
// do this in init (not main) so test file has same input
19+
inputFile, err := file.LoadRelativeFile("input.txt")
20+
if err != nil {
21+
log.Println(err)
22+
}
23+
24+
input = strings.TrimRight(inputFile, "\n")
25+
}
26+
27+
func main() {
28+
var part int
29+
flag.IntVar(&part, "part", 1, "part 1 or 2")
30+
flag.Parse()
31+
fmt.Println("Running part", part)
32+
33+
if part == 1 {
34+
ans := part1(input)
35+
fmt.Println("Output:", ans)
36+
} else {
37+
ans := part2(input)
38+
fmt.Println("Output:", ans)
39+
}
40+
}
41+
42+
func part1(input string) int {
43+
rules, updates := parseInput(input)
44+
45+
middlePages := 0
46+
por := PageOrderingRules{rules}
47+
for _, update := range updates {
48+
if por.ValidateUpdate(update) {
49+
// truncates toward zero
50+
middleIndex := len(update) / 2
51+
pageNum, err := strconv.Atoi(update[middleIndex])
52+
if err != nil {
53+
log.Fatalf("Unable to parse %s", update[middleIndex])
54+
}
55+
56+
middlePages += pageNum
57+
}
58+
}
59+
60+
return middlePages
61+
}
62+
63+
func part2(input string) int {
64+
rules, updates := parseInput(input)
65+
66+
middlePages := 0
67+
por := PageOrderingRules{rules}
68+
for _, update := range updates {
69+
if !por.ValidateUpdate(update) {
70+
newUpdate := por.FixUpdate(update)
71+
72+
// truncates toward zero
73+
middleIndex := len(newUpdate) / 2
74+
pageNum, err := strconv.Atoi(newUpdate[middleIndex])
75+
if err != nil {
76+
log.Fatalf("Unable to parse %s", newUpdate[middleIndex])
77+
}
78+
79+
middlePages += pageNum
80+
}
81+
}
82+
83+
return middlePages
84+
}
85+
86+
type PageOrderRule struct {
87+
Before string
88+
After string
89+
}
90+
91+
type PageOrderingRules struct {
92+
Rules []PageOrderRule
93+
}
94+
95+
func (p PageOrderingRules) RuleFindIndex(rule PageOrderRule, pages []string) (int, int) {
96+
beforeIndex := -1
97+
afterIndex := -1
98+
for i, page := range pages {
99+
if page == rule.Before {
100+
beforeIndex = i
101+
} else if page == rule.After {
102+
afterIndex = i
103+
}
104+
105+
if beforeIndex != -1 && afterIndex != -1 {
106+
// we found both, stop looking
107+
break
108+
}
109+
}
110+
111+
return beforeIndex, afterIndex
112+
}
113+
114+
func (p PageOrderingRules) ValidateUpdate(pages []string) bool {
115+
for _, rule := range p.Rules {
116+
beforeIndex, afterIndex := p.RuleFindIndex(rule, pages)
117+
118+
// if we found both values, and they break the rule
119+
// we can stop
120+
if beforeIndex != -1 && afterIndex != -1 && beforeIndex > afterIndex {
121+
return false
122+
}
123+
}
124+
125+
return true
126+
}
127+
128+
func (p PageOrderingRules) FixUpdate(pages []string) []string {
129+
slices.SortStableFunc(pages, func(a, b string) int {
130+
for _, rule := range p.Rules {
131+
if rule.Before == a && rule.After == b {
132+
return -1
133+
} else if rule.After == a && rule.Before == b {
134+
return 1
135+
}
136+
}
137+
138+
return 0
139+
})
140+
141+
return pages
142+
}
143+
144+
func parseInput(input string) ([]PageOrderRule, [][]string) {
145+
rows := strings.Split(input, "\n")
146+
rules := []PageOrderRule{}
147+
updates := [][]string{}
148+
149+
for _, row := range rows {
150+
if strings.Contains(row, "|") {
151+
items := strings.Split(row, "|")
152+
rules = append(rules, PageOrderRule{items[0], items[1]})
153+
} else if strings.Contains(row, ",") {
154+
pages := strings.Split(row, ",")
155+
updates = append(updates, pages)
156+
}
157+
}
158+
159+
return rules, updates
160+
}

0 commit comments

Comments
 (0)