p1
This commit is contained in:
3
2024/05/go.mod
Normal file
3
2024/05/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
go 1.23.3
|
||||||
102
2024/05/main.go
Normal file
102
2024/05/main.go
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func contains(s []int, e int) bool {
|
||||||
|
for _, a := range s {
|
||||||
|
if a == e {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(nums []int, maps map[int][]int) bool {
|
||||||
|
correct := true
|
||||||
|
for i := 0; i < len(nums); i++ {
|
||||||
|
var checking = nums[i]
|
||||||
|
for j := 0; j < len(nums); j++ {
|
||||||
|
|
||||||
|
if !contains(maps[checking], nums[j]) {
|
||||||
|
if i < j {
|
||||||
|
correct = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return correct
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
lines, err := parse()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var p1 int = 0
|
||||||
|
var p2 int = 0
|
||||||
|
|
||||||
|
maps := make(map[int][]int)
|
||||||
|
|
||||||
|
for y := 0; y < len(lines); y++ {
|
||||||
|
if lines[y] != "" && lines[y][2] == '|' {
|
||||||
|
split := strings.Split(lines[y], "|")
|
||||||
|
l, err := strconv.Atoi(split[0])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
r, err := strconv.Atoi(split[1])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
maps[l] = append(maps[l], r)
|
||||||
|
}
|
||||||
|
|
||||||
|
if lines[y] != "" && lines[y][2] == ',' {
|
||||||
|
numsstr := strings.Split(lines[y], ",")
|
||||||
|
var nums []int
|
||||||
|
|
||||||
|
for i := 0; i < len(numsstr); i++ {
|
||||||
|
x, err := strconv.Atoi(numsstr[i])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
nums = append(nums, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
if check(nums, maps) {
|
||||||
|
p1 += nums[len(nums)/2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("p1: ", p1)
|
||||||
|
fmt.Println("p2: ", p2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse() ([]string, error) {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
return nil, fmt.Errorf("no file provided")
|
||||||
|
}
|
||||||
|
filePath := os.Args[1]
|
||||||
|
data, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error opening file: %v", err)
|
||||||
|
}
|
||||||
|
chunks := strings.Split(string(data), "\n")
|
||||||
|
var nonEmpthyChunks = []string{}
|
||||||
|
|
||||||
|
for _, chunk := range chunks {
|
||||||
|
if chunk != "" {
|
||||||
|
nonEmpthyChunks = append(nonEmpthyChunks, chunk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nonEmpthyChunks, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user