2024 Day 5 Complete!
This commit is contained in:
parent
b852b3951a
commit
58e989b26f
1373
2024/day05/input
Normal file
1373
2024/day05/input
Normal file
File diff suppressed because it is too large
Load Diff
168
2024/day05/main.go
Normal file
168
2024/day05/main.go
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
inp := helpers.StdinToStringSlice()
|
||||||
|
part1(inp)
|
||||||
|
fmt.Println()
|
||||||
|
part2(inp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(inp []string) {
|
||||||
|
rules, updates := processInput(inp)
|
||||||
|
var result int
|
||||||
|
for i := range updates {
|
||||||
|
if rules.Validate(updates[i]) {
|
||||||
|
result = result + updates[i][len(updates[i])/2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("# Part 1")
|
||||||
|
fmt.Printf("Valid Updates: %d\n", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(inp []string) {
|
||||||
|
rules, updates := processInput(inp)
|
||||||
|
var result int
|
||||||
|
for i := range updates {
|
||||||
|
var iter int
|
||||||
|
var wasIncorrect bool
|
||||||
|
for !rules.Validate(updates[i]) {
|
||||||
|
if iter > 100 {
|
||||||
|
panic(errors.New("Naive correction won't work..."))
|
||||||
|
}
|
||||||
|
updates[i] = rules.Correct(updates[i])
|
||||||
|
iter++
|
||||||
|
wasIncorrect = true
|
||||||
|
}
|
||||||
|
if wasIncorrect {
|
||||||
|
result = result + updates[i][len(updates[i])/2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("# Part 2")
|
||||||
|
fmt.Printf("Corrected Updates: %d\n", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func processInput(inp []string) (*PageOrderingRules, [][]int) {
|
||||||
|
var rawRules []string
|
||||||
|
var strUpdates []string
|
||||||
|
var inRules bool
|
||||||
|
inRules = true
|
||||||
|
for i := range inp {
|
||||||
|
if inp[i] == "" {
|
||||||
|
inRules = false
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if inRules {
|
||||||
|
rawRules = append(rawRules, inp[i])
|
||||||
|
} else {
|
||||||
|
strUpdates = append(strUpdates, inp[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var updates [][]int
|
||||||
|
rules := PageOrderingRules{Rules: parseOrderingRules(rawRules)}
|
||||||
|
for i := range strUpdates {
|
||||||
|
updates = append(updates, parseUpdate(strUpdates[i]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &rules, updates
|
||||||
|
}
|
||||||
|
|
||||||
|
type PageOrderingRules struct {
|
||||||
|
Rules []Rule
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageOrderingRules) Correct(update []int) []int {
|
||||||
|
if p.Validate(update) {
|
||||||
|
return update
|
||||||
|
}
|
||||||
|
for i := range p.Rules {
|
||||||
|
update = p.Rules[i].Correct(update)
|
||||||
|
}
|
||||||
|
return update
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageOrderingRules) Validate(update []int) bool {
|
||||||
|
for i := range p.Rules {
|
||||||
|
if !p.Rules[i].Validates(update) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PageOrderingRules) String() string {
|
||||||
|
var ret string
|
||||||
|
for i := range p.Rules {
|
||||||
|
ret = fmt.Sprintf("%s%s\n", ret, p.Rules[i])
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rule struct {
|
||||||
|
first, second int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Rule) Validates(update []int) bool {
|
||||||
|
for i := range update {
|
||||||
|
if update[i] == r.first {
|
||||||
|
for j := range update {
|
||||||
|
if update[j] == r.second {
|
||||||
|
return i < j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Rule) Correct(update []int) []int {
|
||||||
|
if r.Validates(update) {
|
||||||
|
return update
|
||||||
|
}
|
||||||
|
var idx, jdx int
|
||||||
|
for i := range update {
|
||||||
|
if update[i] == r.first {
|
||||||
|
idx = i
|
||||||
|
}
|
||||||
|
if update[i] == r.second {
|
||||||
|
jdx = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
update[idx], update[jdx] = update[jdx], update[idx]
|
||||||
|
return update
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Rule) String() string {
|
||||||
|
return fmt.Sprintf("%d|%d", r.first, r.second)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOrderingRules(inp []string) []Rule {
|
||||||
|
var ret []Rule
|
||||||
|
for i := range inp {
|
||||||
|
if inp[i] == "" {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
rule := Rule{}
|
||||||
|
fmt.Sscanf(inp[i], "%d|%d", &rule.first, &rule.second)
|
||||||
|
ret = append(ret, rule)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseUpdate(inp string) []int {
|
||||||
|
var ret []int
|
||||||
|
pts := strings.Split(inp, ",")
|
||||||
|
for i := range pts {
|
||||||
|
var wrk int
|
||||||
|
fmt.Sscanf(pts[i], "%d", &wrk)
|
||||||
|
ret = append(ret, wrk)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
169
2024/day05/problem
Normal file
169
2024/day05/problem
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
[1]Advent of Code
|
||||||
|
|
||||||
|
• [2][About]
|
||||||
|
• [3][Events]
|
||||||
|
• [4][Shop]
|
||||||
|
• [5][Settings]
|
||||||
|
• [6][Log Out]
|
||||||
|
br0xen [7](AoC++) 8*
|
||||||
|
|
||||||
|
{'year':[8]2024}
|
||||||
|
|
||||||
|
• [9][Calendar]
|
||||||
|
• [10][AoC++]
|
||||||
|
• [11][Sponsors]
|
||||||
|
• [12][Leaderboard]
|
||||||
|
• [13][Stats]
|
||||||
|
Our [14]sponsors help make Advent of Code possible:
|
||||||
|
[15]Cloudsmith - Code shapes reality; now solve your next puzzle: global
|
||||||
|
artifact management. Join our journey to shape, secure, and be the world's
|
||||||
|
software supply chain. We overcame the impossible and became the
|
||||||
|
improbable - now, it's inevitable.
|
||||||
|
|
||||||
|
--- Day 5: Print Queue ---
|
||||||
|
|
||||||
|
Satisfied with their search on Ceres, the squadron of scholars suggests
|
||||||
|
subsequently scanning the stationery stacks of sub-basement 17.
|
||||||
|
|
||||||
|
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 [16]very familiar
|
||||||
|
printer beckons you over.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
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 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.
|
||||||
|
|
||||||
|
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?
|
||||||
|
|
||||||
|
To begin, [17]get your puzzle input.
|
||||||
|
|
||||||
|
Answer: [18]_____________________ [19][ [Submit] ]
|
||||||
|
|
||||||
|
You can also [Shareon [20]Bluesky [21]Twitter [22]Mastodon] this puzzle.
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
1. https://adventofcode.com/
|
||||||
|
2. https://adventofcode.com/2024/about
|
||||||
|
3. https://adventofcode.com/2024/events
|
||||||
|
4. https://cottonbureau.com/people/advent-of-code
|
||||||
|
5. https://adventofcode.com/2024/settings
|
||||||
|
6. https://adventofcode.com/2024/auth/logout
|
||||||
|
7. Advent of Code Supporter
|
||||||
|
https://adventofcode.com/2024/support
|
||||||
|
8. https://adventofcode.com/2024
|
||||||
|
9. https://adventofcode.com/2024
|
||||||
|
10. https://adventofcode.com/2024/support
|
||||||
|
11. https://adventofcode.com/2024/sponsors
|
||||||
|
12. https://adventofcode.com/2024/leaderboard
|
||||||
|
13. https://adventofcode.com/2024/stats
|
||||||
|
14. https://adventofcode.com/2024/sponsors
|
||||||
|
15. https://adventofcode.com/2024/sponsors/redirect?url=https%3A%2F%2Fcloudsmith%2Ecom%3Futm%5Fsource%3Daoc%26utm%5Fmedium%3Dsponsor%26utm%5Fcampaign%3Daoc2024
|
||||||
|
16. https://adventofcode.com/2017/day/1
|
||||||
|
17. https://adventofcode.com/2024/day/5/input
|
||||||
|
20. https://bsky.app/intent/compose?text=%22Print+Queue%22+%2D+Day+5+%2D+Advent+of+Code+2024+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F5
|
||||||
|
21. https://twitter.com/intent/tweet?text=%22Print+Queue%22+%2D+Day+5+%2D+Advent+of+Code+2024&url=https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F5&related=ericwastl&hashtags=AdventOfCode
|
||||||
|
22. javascript:void(0);
|
28
2024/day05/testinput
Normal file
28
2024/day05/testinput
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
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
|
Loading…
Reference in New Issue
Block a user