generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package day15p1 | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
steps := strings.Split(lines[0], ",") | ||
|
||
var sum uint64 | ||
for _, s := range steps { | ||
sum += uint64(elfHash(s)) | ||
} | ||
return sum | ||
} | ||
|
||
func elfHash(s string) byte { | ||
var res byte | ||
|
||
for _, r := range s { | ||
res += byte(r) | ||
res *= 17 | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package day15p1 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer uint64 | ||
}{ | ||
{testInput, 1320}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(uint64) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} | ||
|
||
func TestHash(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer byte | ||
}{ | ||
{"HASH", 52}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
result := elfHash(test.input) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package day15p2 | ||
|
||
import ( | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
steps := strings.Split(lines[0], ",") | ||
|
||
boxes := make(map[byte][]Lens) | ||
for _, s := range steps { | ||
if strings.Contains(s, "=") { | ||
parts := strings.Split(s, "=") | ||
fl, err := strconv.Atoi(parts[1]) | ||
utils.Check(err, "unable to convert %s to integer", parts[1]) | ||
|
||
h := elfHash(parts[0]) | ||
|
||
lenses := boxes[h] | ||
found := false | ||
for i, l := range lenses { | ||
if l.Label == parts[0] { | ||
found = true | ||
l.FocalLength = fl | ||
lenses[i] = l | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
newlenses := make([]Lens, len(lenses)+1) | ||
copy(newlenses, lenses) | ||
newlenses[len(lenses)] = Lens{parts[0], fl} | ||
boxes[h] = newlenses | ||
} | ||
} else { | ||
label, _ := strings.CutSuffix(s, "-") | ||
|
||
h := elfHash(label) | ||
|
||
lenses := boxes[h] | ||
|
||
newlenses := make([]Lens, 0, len(lenses)) | ||
for _, l := range lenses { | ||
if l.Label != label { | ||
newlenses = append(newlenses, l) | ||
} | ||
} | ||
|
||
boxes[h] = newlenses | ||
} | ||
} | ||
|
||
// Find focusing power | ||
var sum uint64 | ||
|
||
for i := 0; i < 256; i++ { | ||
lenses := boxes[byte(i)] | ||
if len(lenses) != 0 { | ||
for j, l := range lenses { | ||
boxpower := (uint64(i) + 1) * uint64((j+1)*l.FocalLength) | ||
sum += boxpower | ||
} | ||
} | ||
} | ||
return sum | ||
} | ||
|
||
func elfHash(s string) byte { | ||
var res byte | ||
|
||
for _, r := range s { | ||
res += byte(r) | ||
res *= 17 | ||
} | ||
|
||
return res | ||
} | ||
|
||
type Lens struct { | ||
Label string | ||
FocalLength int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package day15p2 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer uint64 | ||
}{ | ||
{testInput, 145}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(uint64) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} | ||
|
||
func TestHash(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer byte | ||
}{ | ||
{"HASH", 52}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
result := elfHash(test.input) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
Submodule inputs
updated
from 053b21 to 834921