Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions 2015/2/2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"slices"

"github.com/willie/advent/aoc"
)
Expand All @@ -15,14 +16,14 @@ func wrap(in string) (paper int) {
l, w, h := parse(in)
sides := []int{l * w, w * h, h * l}

return 2*aoc.Sum(sides...) + aoc.Min(sides...)
return 2*aoc.Sum(sides...) + slices.Min(sides)
}

func ribbon(in string) (ribbon int) {
l, w, h := parse(in)
perimeter := []int{2 * (l + w), 2 * (w + h), 2 * (h + l)}

return aoc.Min(perimeter...) + (l * w * h)
return slices.Min(perimeter) + (l * w * h)
}

func part1(in []string) (total int) {
Expand Down
5 changes: 3 additions & 2 deletions 2020/10/10.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"slices"
"sort"

"github.com/willie/advent/aoc"
)

func combined(in aoc.Ints) (first, second int) {
func combined(in []int) (first, second int) {
sort.Ints(in)

last := 0
Expand All @@ -24,7 +25,7 @@ func combined(in aoc.Ints) (first, second int) {
}

first = differences[1] * differences[3]
second = perms[aoc.Max(in...)]
second = perms[slices.Max(in)]

return
}
Expand Down
21 changes: 11 additions & 10 deletions 2020/15/15.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@ import (
)

func part1(in string, turns int) (result [2]int) {
starting := aoc.Ints{}
starting := []int{}

for _, i := range strings.Split(in, ",") {
starting = append(starting, aoc.AtoI(i))
}

spoken := aoc.Ints{}
spoken = append(spoken, starting...)

last := spoken.Last()
spoken := append([]int{}, starting...)
last := spoken[len(spoken)-1]

for turn := len(spoken) + 1; turn <= turns; turn++ {
this := 0

all := spoken.AllIndex(last)
switch {
case len(all) <= 1: // never been spoken
this = 0
// Find all indices where last was spoken
var all []int
for i, v := range spoken {
if v == last {
all = append(all, i)
}
}

default:
if len(all) > 1 {
this = all[len(all)-1] - all[len(all)-2]
}

Expand Down
18 changes: 9 additions & 9 deletions 2020/16/16.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/willie/advent/aoc"
)

func parseTickets(in string) (tickets []aoc.Ints) {
func parseTickets(in string) (tickets [][]int) {
for _, tix := range strings.Split(in, "\n")[1:] {
if tix == "" {
continue
}

ticket := aoc.Ints{}
ticket := []int{}
for _, v := range strings.Split(tix, ",") {
if strings.Contains(v, ":") || (v == "") {
continue
Expand All @@ -30,18 +30,18 @@ func parseTickets(in string) (tickets []aoc.Ints) {
func part1(in string) (result [2]int) {
parts := strings.Split(in, "\n\n")

fieldNames := aoc.StringSet{}
fieldNames := aoc.Set[string]{}
validation := map[int][]string{}
// parse fields, ranges
for _, p := range strings.Split(parts[0], "\n") {
row := strings.Split(p, ": ")

field, ranges := row[0], aoc.IntSet{}
field, ranges := row[0], aoc.Set[int]{}
fieldNames.Add(field)

for _, valid := range strings.Split(row[1], " or ") {
r := strings.Split(valid, "-")
ranges.AddMany(aoc.Series(aoc.AtoI(r[0]), aoc.AtoI(r[1])))
ranges.AddSlice(aoc.Series(aoc.AtoI(r[0]), aoc.AtoI(r[1])))

for _, i := range aoc.Series(aoc.AtoI(r[0]), aoc.AtoI(r[1])) {
validation[i] = append(validation[i], field)
Expand All @@ -54,7 +54,7 @@ func part1(in string) (result [2]int) {
var first int

nearbyTickets := parseTickets(parts[2])
var validTickets []aoc.Ints
var validTickets [][]int

// check invalid fields
for _, ticket := range nearbyTickets {
Expand Down Expand Up @@ -96,9 +96,9 @@ func part1(in string) (result [2]int) {
}
}

fieldNameSorter := map[int]aoc.StringSet{}
fieldNameSorter := map[int]aoc.Set[string]{}
for i := range validTickets[0] {
fieldNameSorter[i] = aoc.StringSet{}
fieldNameSorter[i] = aoc.Set[string]{}
}

for i, nameCount := range fieldSort {
Expand All @@ -111,7 +111,7 @@ func part1(in string) (result [2]int) {
fmt.Println("fieldNameSorter", fieldNameSorter, len(allTickets))

fieldNameOrder := map[int]string{}
namesFound := aoc.StringSet{}
namesFound := aoc.Set[string]{}
for len(fieldNameOrder) < len(fieldNameSorter) {
for n, names := range fieldNameSorter {
if len(names) == 1 {
Expand Down
11 changes: 4 additions & 7 deletions 2020/5/5.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -61,7 +62,7 @@ func part2(in []string) (seatID int) {
sort.Ints(seatIDs)
// fmt.Println(seatIDs)

assigned := aoc.NewIntSet(seatIDs...)
assigned := aoc.NewSet(seatIDs...)
for i := seatIDs[1]; i < seatIDs[len(seatIDs)-2]; i++ {
if assigned.Contains(i-1) && !assigned.Contains(i) && assigned.Contains(i+1) {
return i
Expand All @@ -82,7 +83,7 @@ func main() {

fmt.Println("------- binary solution after some sleep, damnit")

seatIDs := aoc.Ints{}
seatIDs := []int{}

// for _, pass := range aoc.Strings("test") {
for _, pass := range aoc.Strings(day) {
Expand All @@ -108,10 +109,6 @@ exit:

// sum version
println("------- sum version")
fmt.Println("part2", aoc.Sum(aoc.Series(aoc.Min(seatIDs...), aoc.Max(seatIDs...))...)-aoc.Sum(seatIDs...))

// test Ints
println("------- sum Ints version")
fmt.Println("part2", aoc.Series(seatIDs.Min(), seatIDs.Max()).Sum()-seatIDs.Sum())
fmt.Println("part2", aoc.Sum(aoc.Series(slices.Min(seatIDs), slices.Max(seatIDs))...)-aoc.Sum(seatIDs...))

}
8 changes: 4 additions & 4 deletions 2020/6/6.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

func part1(in string) (count int) {
groups := []aoc.StringSet{}
groups := []aoc.Set[string]{}
in = strings.TrimSpace(in)
in = strings.ReplaceAll(in, "\n", "|")
in = strings.ReplaceAll(in, "||", "\n")
in = strings.ReplaceAll(in, "|", "")
for _, p := range strings.Split(in, "\n") {
g := aoc.NewStringSet()
g := aoc.NewSet[string]()

for _, c := range p {
letter := string(c)
Expand Down Expand Up @@ -100,12 +100,12 @@ func part2set(in string) (total int) {
// groups
for _, i := range strings.Split(in, "\n\n") {
people := 0
commonAnswers := aoc.StringSet{}
commonAnswers := aoc.Set[string]{}

// per user
scanner := bufio.NewScanner(strings.NewReader(i))
for scanner.Scan() {
answers := aoc.StringSet{}
answers := aoc.Set[string]{}

for _, c := range scanner.Text() {
answers.Add(string(c))
Expand Down
6 changes: 3 additions & 3 deletions 2020/7/7.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/willie/advent/aoc"
)

func contains(m map[string][]string, color string) (out aoc.StringSet) {
out = aoc.StringSet{}
func contains(m map[string][]string, color string) (out aoc.Set[string]) {
out = aoc.Set[string]{}

if colors, has := m[color]; has {
out.AddMany(colors)
out.AddSlice(colors)
for _, c := range colors {
out.AddSet(contains(m, c))
}
Expand Down
8 changes: 5 additions & 3 deletions 2020/9/9.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"slices"

"github.com/willie/advent/aoc"
)

func combined(in aoc.Ints, window int) (first, second int) {
func combined(in []int, window int) (first, second int) {
for i, next := range in[window:] {
preamble := in[i : i+window]

Expand All @@ -31,8 +33,8 @@ func combined(in aoc.Ints, window int) (first, second int) {
for start := 0; start < len(in); start++ {
for end := start + 1; end < len(in); end++ {
candidate := in[start:end]
if first == candidate.Sum() {
second = candidate.Min() + candidate.Max()
if first == aoc.Sum(candidate...) {
second = slices.Min(candidate) + slices.Max(candidate)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions 2021/12/12.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func part1(in []string) (result int) {
}

for k, v := range g {
g[k] = aoc.NewStringSet(v...).Remove("start").Values()
g[k] = aoc.NewSet(v...).Remove("start").Values()
}

p := follow(g, path{}, "start")
Expand Down Expand Up @@ -109,7 +109,7 @@ func part2(in []string) (result int) {
}

for k, v := range g {
g[k] = aoc.NewStringSet(v...).Remove("start").Values()
g[k] = aoc.NewSet(v...).Remove("start").Values()
}

p := follow2(g, path{}, "start")
Expand Down
5 changes: 3 additions & 2 deletions 2021/14/14.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"slices"
"strings"

"github.com/willie/advent/aoc"
Expand Down Expand Up @@ -134,7 +135,7 @@ func part2(in string, iteration int) (result int64) {

// fmt.Println(letters)

max, min := aoc.Max(counts...), aoc.Min(counts...)
max, min := slices.Max(counts), slices.Min(counts)
result = max - min
// result = (max - min) / 2
// if (max-min)%2 == 1 {
Expand All @@ -150,7 +151,7 @@ func main() {
println(day)

aoc.Test("test1", part1(aoc.String("test"), 10), 1588)
aoc.Test64("test2", part2(aoc.String("test"), 10), 1588)
aoc.Test("test2", part2(aoc.String("test"), 10), int64(1588))

println("-------")

Expand Down
6 changes: 4 additions & 2 deletions 2021/16/16.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"slices"

"github.com/willie/advent/aoc"
)

Expand Down Expand Up @@ -133,10 +135,10 @@ func (p *packet) Value() (out int) {
out = aoc.Product(p.Values()...)

case minimumType:
out = aoc.Min(p.Values()...)
out = slices.Min(p.Values())

case maximumType:
out = aoc.Max(p.Values()...)
out = slices.Max(p.Values())

case lessThanType:
if p.subpackets[0].Value() < p.subpackets[1].Value() {
Expand Down
10 changes: 5 additions & 5 deletions 2021/17/17.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func part1(in string) (maxY int, velocityCount int) {
var x1, x2, y1, y2 int
fmt.Sscanf(in, "%d %d %d %d", &x1, &x2, &y1, &y2)

for i := 1; i <= aoc.Max(aoc.Abs(x1), aoc.Abs(x2)); i++ {
for j := aoc.Min(y1, y2); j <= aoc.Max(aoc.Abs(y1), aoc.Abs(y2)); j++ {
for i := 1; i <= max(aoc.Abs(x1), aoc.Abs(x2)); i++ {
for j := min(y1, y2); j <= max(aoc.Abs(y1), aoc.Abs(y2)); j++ {

probe := image.Pt(0, 0)
velocity := image.Pt(i, j)
height := 0

for probe.X <= aoc.Max(aoc.Abs(x1), aoc.Abs(x2)) && probe.Y >= aoc.Min(y1, y2) {
for probe.X <= max(aoc.Abs(x1), aoc.Abs(x2)) && probe.Y >= min(y1, y2) {
probe = probe.Add(velocity)

if probe.Y > height {
Expand All @@ -36,8 +36,8 @@ func part1(in string) (maxY int, velocityCount int) {
velocity.Y--

// is it in the box? we dont use image.Rect because it doesn't test max coords (off by one)
if aoc.Min(x1, x2) <= probe.X && probe.X <= aoc.Max(x1, x2) &&
aoc.Min(y1, y2) <= probe.Y && probe.Y <= aoc.Max(y1, y2) {
if min(x1, x2) <= probe.X && probe.X <= max(x1, x2) &&
min(y1, y2) <= probe.Y && probe.Y <= max(y1, y2) {
if height > maxY {
maxY = height
}
Expand Down
6 changes: 2 additions & 4 deletions 2021/18/18.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ func (p *pair) addPair(v *pair) {
}
}

func (p *pair) maxdepth() (max int) {
func (p *pair) maxdepth() int {
if p == nil {
return 0
}

max = aoc.Max(p.depth(), p.left.maxdepth(), p.left.maxdepth())
return
return max(p.depth(), p.left.maxdepth(), p.left.maxdepth())
}

func part1(in []string) (result int) {
Expand Down
Loading