Started 2022 with go

This commit is contained in:
Karma Riuk 2023-08-02 11:50:29 +02:00
parent 567c8487b5
commit 7098b525f0
26 changed files with 6810 additions and 0 deletions

93
2022/go/01/answer.go Normal file
View File

@ -0,0 +1,93 @@
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
log "github.com/sirupsen/logrus"
)
type Input struct {
calories [][]int
}
func input(filename string) *Input {
file, err := os.Open(filename)
check(err, fmt.Sprintf("Couldn't open %q", filename))
defer file.Close()
input := &Input{}
input.calories = append(input.calories, make([]int, 0))
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
var line string = scanner.Text()
if len(line) == 0 {
input.calories = append(input.calories, make([]int, 0))
} else {
cal, err := strconv.Atoi(line)
check(err, fmt.Sprintf("Couldn't convert %q to int", line))
input.calories[len(input.calories)-1] = append(input.calories[len(input.calories)-1], cal)
}
}
return input
}
func map_slice[S, T any](ss []S, mapper func(S) T) []T {
var ret []T = make([]T, 0, len(ss))
for _, s := range ss {
ret = append(ret, mapper(s))
}
return ret
}
func max(arr []int) int {
var max int = 0
for _, e := range arr {
if e > max {
max = e
}
}
return max
}
func sum(arr []int) int {
res := 0
for _, e := range arr {
res += e
}
return res
}
func result(inp *Input, part int8) string {
var res string
if part == 1 {
var max int = max(
map_slice(inp.calories, func(s []int) int { return sum(s) }),
)
res = fmt.Sprint(max)
} else {
sums := map_slice(inp.calories, func(s []int) int { return sum(s) })
sort.Ints(sums)
res = fmt.Sprint(sum(sums[len(sums)-3:]))
}
return res
}
func check(e error, message string) {
if e != nil {
log.Error(message)
panic(e)
}
}
func main() {
fmt.Println(result(input("sample1"), 1))
}

44
2022/go/01/answer_test.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"reflect"
"testing"
)
func TestInputSample1(t *testing.T) {
filename := "sample1"
expected := &Input{
calories: [][]int{
{1000, 2000, 3000},
{4000},
{5000, 6000},
{7000, 8000, 9000},
{10000},
},
}
var got *Input = input(filename)
if !reflect.DeepEqual(*got, *expected) {
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
}
}
func TestResult(t *testing.T) {
tests := []struct {
part int8
filename string
expected string
}{
{1, "sample1", "24000"},
{1, "input", "71924"},
{2, "sample1", "45000"},
{2, "input", "210406"},
}
for _, test := range tests {
var got string = result(input(test.filename), test.part)
if got != test.expected {
t.Errorf("result = %q, want %q", got, test.expected)
}
}
}

7
2022/go/01/go.mod Normal file
View File

@ -0,0 +1,7 @@
module aoc/1
go 1.20
require github.com/sirupsen/logrus v1.9.3
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

15
2022/go/01/go.sum Normal file
View File

@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2251
2022/go/01/input Normal file

File diff suppressed because it is too large Load Diff

14
2022/go/01/sample1 Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

91
2022/go/02/answer.go Normal file
View File

@ -0,0 +1,91 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
const (
ROCK int = iota
PAPER
SCISSOR
)
const (
LOSS int = 3 * iota
DRAW
WIN
)
type Turn struct {
them int
me int
}
type Input struct {
turns []Turn
}
var str_to_RPS = map[string]int{
"A": ROCK,
"B": PAPER,
"C": SCISSOR,
"X": ROCK,
"Y": PAPER,
"Z": SCISSOR,
}
func input(filename string) *Input {
file, err := os.Open(filename)
check(err)
defer file.Close()
input := &Input{
turns: []Turn{},
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
hands := strings.Split(line, " ")
input.turns = append(input.turns, Turn{str_to_RPS[hands[0]], str_to_RPS[hands[1]]})
}
return input
}
func result(inp *Input, part int8) string {
var res string
score := 0
for _, turn := range inp.turns {
score += turn.me + 1
if turn.me == (turn.them+1)%3 {
score += WIN
} else if turn.me == turn.them {
score += DRAW
}
}
res = fmt.Sprint(score)
return res
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
result := result(input("sample1"), 1)
log.SetFlags(log.Lshortfile)
log.Println(result)
fmt.Println(result)
}

44
2022/go/02/answer_test.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"reflect"
"testing"
)
func TestInputSample1(t *testing.T) {
filename := "sample1"
expected := &Input{
turns: []Turn{
{ROCK, PAPER},
{PAPER, ROCK},
{SCISSOR, SCISSOR},
},
}
var got *Input = input(filename)
if !reflect.DeepEqual(expected, got) {
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
}
}
func TestResult(t *testing.T) {
tests := []struct {
part int8
filename string
expected string
}{
{1, "sample1", "15"},
{1, "input", "12855"},
// {2, "sample1", ""},
// {2, "input", ""},
}
for _, test := range tests {
var got string = result(input(test.filename), test.part)
if got != test.expected {
t.Errorf("result = %q, want %q", got, test.expected)
}
}
}

3
2022/go/02/go.mod Normal file
View File

@ -0,0 +1,3 @@
module aoc/2
go 1.20

0
2022/go/02/go.sum Normal file
View File

2500
2022/go/02/input Normal file

File diff suppressed because it is too large Load Diff

3
2022/go/02/sample1 Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

95
2022/go/03/answer.go Normal file
View File

@ -0,0 +1,95 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
type Sack struct {
first string
second string
}
type Input struct {
sacks []Sack
}
func input(filename string) *Input {
file, err := os.Open(filename)
check(err)
defer file.Close()
input := &Input{
sacks: []Sack{},
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := strings.Trim(scanner.Text(), "\n")
input.sacks = append(input.sacks, Sack{
line[:len(line)/2],
line[len(line)/2:],
})
}
return input
}
func priority(c rune) int {
score := 0
if c >= 'a' && c <= 'z' {
score += int(c-'a') + 1
} else if c >= 'A' && c <= 'Z' {
score += int(c-'A') + 27
}
return score
}
func result(inp *Input, part int8) string {
var res string
score := 0
if part == 1 {
for _, sack := range inp.sacks {
for _, char := range sack.first {
if strings.Contains(sack.second, string(char)) {
score += priority(char)
break
}
}
}
} else {
for i := 0; i < len(inp.sacks); i += 3 {
sack := inp.sacks[i]
for _, char := range sack.first + sack.second {
if strings.Contains(inp.sacks[i+1].first+inp.sacks[i+1].second, string(char)) &&
strings.Contains(inp.sacks[i+2].first+inp.sacks[i+2].second, string(char)) {
score += priority(char)
break
}
}
}
}
res = fmt.Sprint(score)
return res
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
result := result(input("sample1"), 1)
log.Println(result)
fmt.Println(result)
}

71
2022/go/03/answer_test.go Normal file
View File

@ -0,0 +1,71 @@
package main
import (
"reflect"
"testing"
)
func TestPriority(t *testing.T) {
tests := []struct {
c rune
expected int
}{
{'a', 1},
{'b', 2},
{'p', 16},
{'L', 38},
{'P', 42},
{'v', 22},
{'t', 20},
{'s', 19},
}
for _, test := range tests {
var got int = priority(test.c)
if got != test.expected {
t.Errorf("priority(%q) = %v, want %v", test.c, got, test.expected)
}
}
}
func TestInputSample1(t *testing.T) {
filename := "sample1"
expected := &Input{
sacks: []Sack{
{"vJrwpWtwJgWr", "hcsFMMfFFhFp"},
{"jqHRNqRjqzjGDLGL", "rsFMfFZSrLrFZsSL"},
{"PmmdzqPrV", "vPwwTWBwg"},
{"wMqvLMZHhHMvwLH", "jbvcjnnSBnvTQFn"},
{"ttgJtRGJ", "QctTZtZT"},
{"CrZsJsPPZsGz", "wwsLwLmpwMDw"},
},
}
var got *Input = input(filename)
if !reflect.DeepEqual(expected, got) {
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
}
}
func TestResult(t *testing.T) {
tests := []struct {
part int8
filename string
expected string
}{
{1, "sample1", "157"},
{1, "input", "7727"},
{2, "sample1", "70"},
{2, "input", "2609"},
}
for _, test := range tests {
var got string = result(input(test.filename), test.part)
if got != test.expected {
t.Errorf("result = %q, want %q", got, test.expected)
}
}
}

3
2022/go/03/go.mod Normal file
View File

@ -0,0 +1,3 @@
module aoc/3
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SvvDtntCRfwzGCgdzzFjG
TfdZgtmfDgqgvlLFFsFHvcvZ
pphWQMVjQVVBWWjRlHlHnlcLDDhcnF
JQwwWVPBwMJpJwpWwGBWNzrDzSSzfgTPqTSTTtSPgt

6
2022/go/03/sample1 Normal file
View File

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

90
2022/go/04/answer.go Normal file
View File

@ -0,0 +1,90 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type Range struct {
low, high int
}
func (r *Range) contians(other *Range) bool {
return r.low <= other.low && other.high <= r.high
}
func (r *Range) overlaps(other *Range) bool {
return (r.low <= other.low && other.low <= r.high) ||
(r.low <= other.high && other.high <= r.high)
}
func fromString(s string) Range {
parts := strings.Split(s, "-")
low, err := strconv.Atoi(parts[0])
check(err, "Can't convert %q to int", parts[0])
high, err := strconv.Atoi(parts[1])
check(err, "Can't convert %q to int", parts[1])
return Range{low, high}
}
type Input struct {
ranges []struct{ a, b Range }
}
func input(filename string) *Input {
file, err := os.Open(filename)
check(err, "Couldn't open %q", filename)
defer file.Close()
input := &Input{}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
sets := strings.Split(line, ",")
input.ranges = append(input.ranges, struct{ a, b Range }{fromString(sets[0]), fromString(sets[1])})
}
return input
}
func result(inp *Input, part int8) string {
var res string
score := 0
for _, pair := range inp.ranges {
if part == 1 {
if pair.a.contians(&pair.b) || pair.b.contians(&pair.a) {
score++
}
} else {
if pair.a.overlaps(&pair.b) || pair.b.overlaps(&pair.a) {
score++
}
}
}
res = fmt.Sprint(score)
return res
}
func check(e error, msg string, vals ...any) {
if e != nil {
log.Printf("ERROR: "+msg, vals)
panic(e)
}
}
func main() {
result := result(input("sample1"), 1)
log.Println(result)
fmt.Println(result)
}

47
2022/go/04/answer_test.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"reflect"
"testing"
)
func TestInputSample1(t *testing.T) {
filename := "sample1"
expected := &Input{
ranges: []struct{ a, b Range }{
{Range{2, 4}, Range{6, 8}},
{Range{2, 3}, Range{4, 5}},
{Range{5, 7}, Range{7, 9}},
{Range{2, 8}, Range{3, 7}},
{Range{6, 6}, Range{4, 6}},
{Range{2, 6}, Range{4, 8}},
},
}
var got *Input = input(filename)
if !reflect.DeepEqual(expected, got) {
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
}
}
func TestResult(t *testing.T) {
tests := []struct {
part int8
filename string
expected string
}{
{1, "sample1", "2"},
{1, "input", "530"},
{2, "sample1", "4"},
{2, "input", "903"},
}
for _, test := range tests {
var got string = result(input(test.filename), test.part)
if got != test.expected {
t.Errorf("result = %q, want %q", got, test.expected)
}
}
}

3
2022/go/04/go.mod Normal file
View File

@ -0,0 +1,3 @@
module aoc/4
go 1.20

1000
2022/go/04/input Normal file

File diff suppressed because it is too large Load Diff

6
2022/go/04/sample1 Normal file
View File

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

31
2022/go/Makefile Normal file
View File

@ -0,0 +1,31 @@
COOKIE = 53616c7465645f5fb99dda36ae761920e1f0d561f3761dbaab58f2dcfe046a477f89881d95a683f92a0ac924a6e42c9ab614591a5716915c5de56fe977aa0881
.ONESHELL:
.PHONY = new create init get_input get_sample
NUMBER := $$(($(shell ls -D | grep "^[0-2][0-9]$$" | wc -l) + 1))
NUMBER_DIR := $(shell printf "%02d" $(NUMBER))
# NUMBER := $(shell ls -D | wc -l)
# https://adventofcode.com/2022/day/1/input
new: create init get_input get_sample
create:
@echo "Creating $(NUMBER)..."
@cp -r template/ $(NUMBER_DIR)/
init:
@echo "Initiliazing go module..."
@cd $(NUMBER_DIR)
@rm go.mod
@go mod init aoc/$(NUMBER)
@go mod tidy
get_input:
@echo "Getting input..."
@wget -q -O $(NUMBER_DIR)/input --header "Cookie: session=$(COOKIE)" https://adventofcode.com/2022/day/$(NUMBER)/input
get_sample:
@echo "Getting sample... Is it in your clipboard? I surely hope so..."
@xclip -o > $(NUMBER_DIR)/sample1

View File

@ -0,0 +1,52 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
)
type Input struct{}
func input(filename string) *Input {
file, err := os.Open(filename)
check(err, "Couldn't open %q", filename)
defer file.Close()
input := &Input{}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
}
return input
}
func result(inp *Input, part int8) string {
var res string
if part == 1 {
} else {
}
return res
}
func check(e error, msg string, vals ...any) {
if e != nil {
log.Printf("ERROR: "+msg, vals)
panic(e)
}
}
func main() {
result := result(input("sample1"), 1)
log.Println(result)
fmt.Println(result)
}

View File

@ -0,0 +1,38 @@
package main
import (
"reflect"
"testing"
)
func TestInputSample1(t *testing.T) {
filename := "sample1"
expected := &Input{}
var got *Input = input(filename)
if !reflect.DeepEqual(expected, got) {
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
}
}
func TestResult(t *testing.T) {
tests := []struct {
part int8
filename string
expected string
}{
{1, "sample1", ""},
{1, "input", ""},
// {2, "sample1", ""},
// {2, "input", ""},
}
for _, test := range tests {
var got string = result(input(test.filename), test.part)
if got != test.expected {
t.Errorf("result = %q, want %q", got, test.expected)
}
}
}

3
2022/go/template/go.mod Normal file
View File

@ -0,0 +1,3 @@
module aoc/template
go 1.20