2023.15
This commit is contained in:
parent
d3387fba19
commit
7d08649833
5 changed files with 165 additions and 0 deletions
3
challenges/2023/15-lensLibrary/README.md
Normal file
3
challenges/2023/15-lensLibrary/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# [Day 15: Lens Library](https://adventofcode.com/2023/day/15)
|
||||
|
||||
* 2 not 315986
|
146
challenges/2023/15-lensLibrary/main.go
Normal file
146
challenges/2023/15-lensLibrary/main.go
Normal file
|
@ -0,0 +1,146 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func hash(x string) int {
|
||||
var res int
|
||||
for _, char := range x {
|
||||
res += int(char)
|
||||
res *= 17
|
||||
res = res % 256
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func parse(instr string) []string {
|
||||
return strings.Split(instr, ",")
|
||||
}
|
||||
|
||||
func one(instr string) int {
|
||||
var (
|
||||
sequence = parse(instr)
|
||||
acc int
|
||||
)
|
||||
|
||||
for _, section := range sequence {
|
||||
acc += hash(section)
|
||||
}
|
||||
|
||||
return acc
|
||||
}
|
||||
|
||||
type lens struct {
|
||||
Label string
|
||||
FocalLength int
|
||||
}
|
||||
|
||||
func parseLens(x string) (*lens, rune) {
|
||||
var (
|
||||
op rune
|
||||
labelRunes []rune
|
||||
foundFocalLength bool
|
||||
focalLengthRunes []rune
|
||||
)
|
||||
|
||||
for _, char := range x {
|
||||
if char == '-' {
|
||||
op = char
|
||||
break
|
||||
}
|
||||
|
||||
if char == '=' {
|
||||
op = char
|
||||
foundFocalLength = true
|
||||
continue
|
||||
}
|
||||
|
||||
if foundFocalLength {
|
||||
focalLengthRunes = append(focalLengthRunes, char)
|
||||
} else {
|
||||
labelRunes = append(labelRunes, char)
|
||||
}
|
||||
}
|
||||
|
||||
res := new(lens)
|
||||
|
||||
if foundFocalLength {
|
||||
var err error
|
||||
res.FocalLength, err = strconv.Atoi(string(focalLengthRunes))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
res.Label = string(labelRunes)
|
||||
|
||||
return res, op
|
||||
}
|
||||
|
||||
func two(instr string) int {
|
||||
sequence := parse(instr)
|
||||
|
||||
boxes := make([][]*lens, 256)
|
||||
|
||||
for _, part := range sequence {
|
||||
var (
|
||||
lens, op = parseLens(part)
|
||||
labelHash = hash(lens.Label)
|
||||
added bool
|
||||
)
|
||||
|
||||
var n int
|
||||
for _, x := range boxes[labelHash] {
|
||||
if labelMatches := x.Label == lens.Label; labelMatches && op == '=' {
|
||||
added = true
|
||||
boxes[labelHash][n] = lens
|
||||
n += 1
|
||||
} else if !labelMatches {
|
||||
boxes[labelHash][n] = x
|
||||
n += 1
|
||||
}
|
||||
}
|
||||
boxes[labelHash] = boxes[labelHash][:n]
|
||||
|
||||
if !added && op == '=' {
|
||||
boxes[labelHash] = append(boxes[labelHash], lens)
|
||||
}
|
||||
}
|
||||
|
||||
var acc int
|
||||
for i, box := range boxes {
|
||||
for j, lens := range box {
|
||||
acc += (i + 1) * (j + 1) * lens.FocalLength
|
||||
}
|
||||
}
|
||||
return acc
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 || !(os.Args[1] == "1" || os.Args[1] == "2") {
|
||||
debug("Missing day argument")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
inp, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
inpStr := strings.TrimSpace(string(inp))
|
||||
|
||||
switch os.Args[1] {
|
||||
case "1":
|
||||
fmt.Println(one(inpStr))
|
||||
case "2":
|
||||
fmt.Println(two(inpStr))
|
||||
}
|
||||
}
|
||||
|
||||
func debug(f string, sub ...any) {
|
||||
fmt.Fprintf(os.Stderr, f, sub...)
|
||||
}
|
14
challenges/2023/15-lensLibrary/tests.json
Normal file
14
challenges/2023/15-lensLibrary/tests.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"1": [
|
||||
{
|
||||
"is": "1320",
|
||||
"input": "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7\n"
|
||||
}
|
||||
],
|
||||
"2": [
|
||||
{
|
||||
"is": "145",
|
||||
"input": "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7\n"
|
||||
}
|
||||
]
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 43 KiB |
|
@ -25,3 +25,5 @@
|
|||
{"day": 13, "part": 2, "runner": "py", "min": 0.06211209297180176, "max": 0.12923550605773926, "avg": 0.07918766498565674, "n": 100}
|
||||
{"day": 14, "part": 1, "runner": "py", "min": 0.01713418960571289, "max": 0.02086782455444336, "avg": 0.01824690341949463, "n": 50}
|
||||
{"day": 14, "part": 2, "runner": "py", "min": 0.48926782608032227, "max": 0.6927425861358643, "avg": 0.5653452634811401, "n": 50}
|
||||
{"day": 15, "part": 1, "runner": "go", "min": 0.0005846023559570312, "max": 0.0011608600616455078, "avg": 0.0007052874565124512, "n": 100}
|
||||
{"day": 15, "part": 2, "runner": "go", "min": 0.0012998580932617188, "max": 0.0028612613677978516, "avg": 0.0014359498023986817, "n": 100}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue