[INI] main: Initial commit
This commit is contained in:
commit
1da4b15718
3 changed files with 1128 additions and 0 deletions
1000
01/input.txt
Normal file
1000
01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
122
01/main.go
Normal file
122
01/main.go
Normal file
|
|
@ -0,0 +1,122 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const FILE_PATH = "./input.txt"
|
||||||
|
|
||||||
|
//const FILE_PATH = "./sample.txt"
|
||||||
|
|
||||||
|
func LoadInput(filename string) ([]string, error) {
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TransformInput(lines []string) [2][]int {
|
||||||
|
var result [2][]int
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
var numbs = strings.Split(line, " ")
|
||||||
|
var numb int
|
||||||
|
numb, _ = strconv.Atoi(numbs[0])
|
||||||
|
result[0] = append(result[0], numb)
|
||||||
|
numb, _ = strconv.Atoi(numbs[1])
|
||||||
|
result[1] = append(result[1], numb)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Sort(input []int) []int {
|
||||||
|
var result []int = make([]int, len(input))
|
||||||
|
var temp int
|
||||||
|
copy(result, input)
|
||||||
|
|
||||||
|
for i := 0; i < len(result)-1; i++ {
|
||||||
|
for j := 0; j < len(result)-i-1; j++ {
|
||||||
|
if result[j] > result[j+1] {
|
||||||
|
temp = result[j]
|
||||||
|
result[j] = result[j+1]
|
||||||
|
result[j+1] = temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(input [2][]int) int {
|
||||||
|
var result = 0
|
||||||
|
var sorted [2][]int = [2][]int{Sort(input[0]), Sort(input[1])}
|
||||||
|
for i := 0; i < len(sorted[0]); i++ {
|
||||||
|
result += int(math.Abs((float64)(sorted[0][i] - sorted[1][i])))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input [2][]int) int {
|
||||||
|
var sorted [2][]int = [2][]int{Sort(input[0]), Sort(input[1])}
|
||||||
|
var result = 0
|
||||||
|
var r = 0
|
||||||
|
var l = 0
|
||||||
|
var count_l = 0
|
||||||
|
var count_r = 0
|
||||||
|
|
||||||
|
for r < len(sorted[1]) && l < len(sorted[0]) {
|
||||||
|
count_r = 0
|
||||||
|
count_l = 1
|
||||||
|
|
||||||
|
for r < len(sorted[1]) && sorted[1][r] < sorted[0][l] {
|
||||||
|
r++
|
||||||
|
}
|
||||||
|
for r < len(sorted[1]) && sorted[1][r] == sorted[0][l] {
|
||||||
|
count_r++
|
||||||
|
r++
|
||||||
|
}
|
||||||
|
|
||||||
|
l++
|
||||||
|
|
||||||
|
for l < len(sorted[0]) && sorted[0][l-1] == sorted[0][l] {
|
||||||
|
count_l++
|
||||||
|
l++
|
||||||
|
}
|
||||||
|
result += count_l * count_r * sorted[0][l-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
input, err := LoadInput(FILE_PATH)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error loading input:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
structuredInput := TransformInput(input)
|
||||||
|
fmt.Println("Structured Input:", structuredInput)
|
||||||
|
|
||||||
|
fmt.Println("Solution Part 1: ", Part1(structuredInput))
|
||||||
|
fmt.Println("Solution Part 2: ", Part2(structuredInput))
|
||||||
|
}
|
||||||
6
01/sample.txt
Normal file
6
01/sample.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
||||||
Loading…
Reference in a new issue