2023 Day 15 Complete!

This commit is contained in:
Brian Buller 2023-12-15 07:20:13 -06:00
parent 0c611fdc3b
commit 5769c7e75c
4 changed files with 426 additions and 0 deletions

1
2023/day15/input Normal file

File diff suppressed because one or more lines are too long

128
2023/day15/main.go Normal file
View File

@ -0,0 +1,128 @@
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToString()
part1(inp)
fmt.Println()
part2(inp)
}
func part1(input string) {
var res int
pts := strings.Split(input, ",")
for i := range pts {
res += hash(pts[i])
}
fmt.Println("# Part 1")
fmt.Println(res)
}
func part2(input string) {
var boxes []*Box
for i := 1; i <= 256; i++ {
boxes = append(boxes, &Box{num: i})
}
for _, v := range strings.Split(input, ",") {
l := NewLens(v)
if l.focalLength == -1 {
// Remove
boxes[l.box].Remove(l)
} else {
// Add/Replace
boxes[l.box].Add(l)
}
}
var res int
for i := range boxes {
res += boxes[i].FocusingPower()
}
fmt.Println("# Part 2")
fmt.Println(res)
}
type Box struct {
num int
lenses []*Lens
}
func (b *Box) Add(l *Lens) {
for i := range b.lenses {
if b.lenses[i].label == l.label {
b.lenses[i] = l
return
}
}
b.lenses = append(b.lenses, l)
}
func (b *Box) Remove(l *Lens) {
for i := range b.lenses {
if b.lenses[i].label == l.label {
b.lenses = append(b.lenses[:i], b.lenses[i+1:]...)
return
}
}
}
func (b *Box) FocusingPower() int {
var res int
for i := range b.lenses {
var lensRes int
lensRes = b.num
lensRes *= i + 1
lensRes *= b.lenses[i].focalLength
res += lensRes
}
return res
}
type Lens struct {
label string
focalLength int
box int
}
func NewLens(inp string) *Lens {
if strings.Contains(inp, "=") {
pts := strings.Split(inp, "=")
return &Lens{
label: pts[0],
focalLength: h.Atoi(pts[1]),
box: hash(pts[0]),
}
} else {
lbl := strings.TrimSuffix(inp, "-")
return &Lens{
label: lbl,
focalLength: -1,
box: hash(lbl),
}
}
}
func hash(inp string) int {
var res int
for _, i := range inp {
res += int(i)
res *= 17
res = res % 256
}
return res
}
func focusingPower(lens string, boxNum int, boxes [][]string) int {
res := boxNum + 1
for i := 1; i <= len(boxes[boxNum]); i++ {
if boxes[boxNum][i-1] == lens {
res *= i
break
}
}
return res
}

296
2023/day15/problem Normal file
View File

@ -0,0 +1,296 @@
[1]Advent of Code
• [2][About]
• [3][Events]
• [4][Shop]
• [5][Settings]
• [6][Log Out]
br0xen [7](AoC++) 30*
  0.0.0.0:[8]2023
• [9][Calendar]
• [10][AoC++]
• [11][Sponsors]
• [12][Leaderboard]
• [13][Stats]
Our [14]sponsors help make Advent of Code possible:
[15]Honeycomb - On call for the holidays? Honeycomb adopters are happier
and less burned out because we make your complex systems easy to
understand.
--- Day 15: Lens Library ---
The newly-focused parabolic reflector dish is sending all of the collected
light to a point on the side of yet another mountain - the largest
mountain on Lava Island. As you approach the mountain, you find that the
light is being collected by the wall of a large facility embedded in the
mountainside.
You find a door under a large sign that says "Lava Production Facility"
and next to a smaller sign that says "Danger - Personal Protective
Equipment required beyond this point".
As you step inside, you are immediately greeted by a somewhat panicked
reindeer wearing goggles and a loose-fitting [16]hard hat. The reindeer
leads you to a shelf of goggles and hard hats (you quickly find some that
fit) and then further into the facility. At one point, you pass a button
with a faint snout mark and the label "PUSH FOR HELP". No wonder you were
loaded into that [17]trebuchet so quickly!
You pass through a final set of doors surrounded with even more warning
signs and into what must be the room that collects all of the light from
outside. As you admire the large assortment of lenses available to further
focus the light, the reindeer brings you a book titled "Initialization
Manual".
"Hello!", the book cheerfully begins, apparently unaware of the concerned
reindeer reading over your shoulder. "This procedure will let you bring
the Lava Production Facility online - all without burning or melting
anything unintended!"
"Before you begin, please be prepared to use the Holiday ASCII String
Helper algorithm (appendix 1A)." You turn to appendix 1A. The reindeer
leans closer with interest.
The HASH algorithm is a way to turn any [18]string of characters into a
single number in the range 0 to 255. To run the HASH algorithm on a
string, start with a current value of 0. Then, for each character in the
string starting from the beginning:
 Determine the [19]ASCII code for the current character of the string.
 Increase the current value by the ASCII code you just determined.
 Set the current value to itself multiplied by 17.
 Set the current value to the [20]remainder of dividing itself by 256.
After following these steps for each character in the string in order, the
current value is the output of the HASH algorithm.
So, to find the result of running the HASH algorithm on the string HASH:
 The current value starts at 0.
 The first character is H; its ASCII code is 72.
 The current value increases to 72.
 The current value is multiplied by 17 to become 1224.
 The current value becomes 200 (the remainder of 1224 divided by 256).
 The next character is A; its ASCII code is 65.
 The current value increases to 265.
 The current value is multiplied by 17 to become 4505.
 The current value becomes 153 (the remainder of 4505 divided by 256).
 The next character is S; its ASCII code is 83.
 The current value increases to 236.
 The current value is multiplied by 17 to become 4012.
 The current value becomes 172 (the remainder of 4012 divided by 256).
 The next character is H; its ASCII code is 72.
 The current value increases to 244.
 The current value is multiplied by 17 to become 4148.
 The current value becomes 52 (the remainder of 4148 divided by 256).
So, the result of running the HASH algorithm on the string HASH is 52.
The initialization sequence (your puzzle input) is a comma-separated list
of steps to start the Lava Production Facility. Ignore newline characters
when parsing the initialization sequence. To verify that your HASH
algorithm is working, the book offers the sum of the result of running the
HASH algorithm on each step in the initialization sequence.
For example:
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
This initialization sequence specifies 11 individual steps; the result of
running the HASH algorithm on each of the steps is as follows:
 rn=1 becomes 30.
 cm- becomes 253.
 qp=3 becomes 97.
 cm=2 becomes 47.
 qp- becomes 14.
 pc=4 becomes 180.
 ot=9 becomes 9.
 ab=5 becomes 197.
 pc- becomes 48.
 pc=6 becomes 214.
 ot=7 becomes 231.
In this example, the sum of these results is 1320. Unfortunately, the
reindeer has stolen the page containing the expected verification number
and is currently running around the facility with it excitedly.
Run the HASH algorithm on each step in the initialization sequence. What
is the sum of the results? (The initialization sequence is one long line;
be careful when copy-pasting it.)
Your puzzle answer was 515495.
--- Part Two ---
You convince the reindeer to bring you the page; the page confirms that
your HASH algorithm is working.
The book goes on to describe a series of 256 boxes numbered 0 through 255.
The boxes are arranged in a line starting from the point where light
enters the facility. The boxes have holes that allow light to pass from
one box to the next all the way down the line.
+-----+ +-----+ +-----+
Light | Box | | Box | ... | Box |
----------------------------------------->
| 0 | | 1 | ... | 255 |
+-----+ +-----+ +-----+
Inside each box, there are several lens slots that will keep a lens
correctly positioned to focus light passing through the box. The side of
each box has a panel that opens to allow you to insert or remove lenses as
necessary.
Along the wall running parallel to the boxes is a large library containing
lenses organized by focal length ranging from 1 through 9. The reindeer
also brings you a small handheld [21]label printer.
The book goes on to explain how to perform each step in the initialization
sequence, a process it calls the Holiday ASCII String Helper Manual
Arrangement Procedure, or HASHMAP for short.
Each step begins with a sequence of letters that indicate the label of the
lens on which the step operates. The result of running the HASH algorithm
on the label indicates the correct box for that step.
The label will be immediately followed by a character that indicates the
operation to perform: either an equals sign (=) or a dash (-).
If the operation character is a dash (-), go to the relevant box and
remove the lens with the given label if it is present in the box. Then,
move any remaining lenses as far forward in the box as they can go without
changing their order, filling any space made by removing the indicated
lens. (If no lens in that box has the given label, nothing happens.)
If the operation character is an equals sign (=), it will be followed by a
number indicating the focal length of the lens that needs to go into the
relevant box; be sure to use the label maker to mark the lens with the
label given in the beginning of the step so you can find it later. There
are two possible situations:
 If there is already a lens in the box with the same label, replace the
old lens with the new lens: remove the old lens and put the new lens
in its place, not moving any other lenses in the box.
 If there is not already a lens in the box with the same label, add the
lens to the box immediately behind any lenses already in the box.
Don't move any of the other lenses when you do this. If there aren't
any lenses in the box, the new lens goes all the way to the front of
the box.
Here is the contents of every box after each step in the example
initialization sequence above:
After "rn=1":
Box 0: [rn 1]
After "cm-":
Box 0: [rn 1]
After "qp=3":
Box 0: [rn 1]
Box 1: [qp 3]
After "cm=2":
Box 0: [rn 1] [cm 2]
Box 1: [qp 3]
After "qp-":
Box 0: [rn 1] [cm 2]
After "pc=4":
Box 0: [rn 1] [cm 2]
Box 3: [pc 4]
After "ot=9":
Box 0: [rn 1] [cm 2]
Box 3: [pc 4] [ot 9]
After "ab=5":
Box 0: [rn 1] [cm 2]
Box 3: [pc 4] [ot 9] [ab 5]
After "pc-":
Box 0: [rn 1] [cm 2]
Box 3: [ot 9] [ab 5]
After "pc=6":
Box 0: [rn 1] [cm 2]
Box 3: [ot 9] [ab 5] [pc 6]
After "ot=7":
Box 0: [rn 1] [cm 2]
Box 3: [ot 7] [ab 5] [pc 6]
All 256 boxes are always present; only the boxes that contain any lenses
are shown here. Within each box, lenses are listed from front to back;
each lens is shown as its label and focal length in square brackets.
To confirm that all of the lenses are installed correctly, add up the
focusing power of all of the lenses. The focusing power of a single lens
is the result of multiplying together:
 One plus the box number of the lens in question.
 The slot number of the lens within the box: 1 for the first lens, 2
for the second lens, and so on.
 The focal length of the lens.
At the end of the above example, the focusing power of each lens is as
follows:
 rn: 1 (box 0) * 1 (first slot) * 1 (focal length) = 1
 cm: 1 (box 0) * 2 (second slot) * 2 (focal length) = 4
 ot: 4 (box 3) * 1 (first slot) * 7 (focal length) = 28
 ab: 4 (box 3) * 2 (second slot) * 5 (focal length) = 40
 pc: 4 (box 3) * 3 (third slot) * 6 (focal length) = 72
So, the above example ends up with a total focusing power of 145.
With the help of an over-enthusiastic reindeer in a hard hat, follow the
initialization sequence. What is the focusing power of the resulting lens
configuration?
Your puzzle answer was 229349.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should [22]return to your Advent calendar and try
another puzzle.
If you still want to see it, you can [23]get your puzzle input.
You can also [Shareon [24]Twitter [25]Mastodon] this puzzle.
References
Visible links
1. https://adventofcode.com/
2. https://adventofcode.com/2023/about
3. https://adventofcode.com/2023/events
4. https://teespring.com/stores/advent-of-code
5. https://adventofcode.com/2023/settings
6. https://adventofcode.com/2023/auth/logout
7. Advent of Code Supporter
https://adventofcode.com/2023/support
8. https://adventofcode.com/2023
9. https://adventofcode.com/2023
10. https://adventofcode.com/2023/support
11. https://adventofcode.com/2023/sponsors
12. https://adventofcode.com/2023/leaderboard
13. https://adventofcode.com/2023/stats
14. https://adventofcode.com/2023/sponsors
15. https://www.honeycomb.io/product-overview
16. https://en.wikipedia.org/wiki/Hard_hat
17. https://adventofcode.com/2023/day/1
18. https://en.wikipedia.org/wiki/String_(computer_science)
19. https://en.wikipedia.org/wiki/ASCII#Printable_characters
20. https://en.wikipedia.org/wiki/Modulo
21. https://en.wikipedia.org/wiki/Label_printer
22. https://adventofcode.com/2023
23. https://adventofcode.com/2023/day/15/input
24. https://twitter.com/intent/tweet?text=I%27ve+completed+%22Lens+Library%22+%2D+Day+15+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F15&related=ericwastl&hashtags=AdventOfCode
25. javascript:void(0);

1
2023/day15/testinput Normal file
View File

@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7