[ADD] main: Added day 2 solution
This commit is contained in:
parent
1da4b15718
commit
eb79b7712f
3 changed files with 1121 additions and 0 deletions
1000
02/input.txt
Normal file
1000
02/input.txt
Normal file
File diff suppressed because it is too large
Load diff
115
02/main.go
Normal file
115
02/main.go
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
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) [][]int {
|
||||||
|
var result [][]int
|
||||||
|
var temp []int
|
||||||
|
var numb int
|
||||||
|
for _, line := range lines {
|
||||||
|
var numbs = strings.Split(line, " ")
|
||||||
|
temp = make([]int, len(numbs))
|
||||||
|
for i := 0; i < len(numbs); i++ {
|
||||||
|
numb, _ = strconv.Atoi(numbs[i])
|
||||||
|
temp[i] = numb
|
||||||
|
}
|
||||||
|
result = append(result, temp)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsLineSafe(input []int) bool {
|
||||||
|
var decreasing = input[0] > input[1]
|
||||||
|
var dif = 0
|
||||||
|
for i := 0; i < len(input)-1; i++ {
|
||||||
|
if input[i] > input[i+1] && !decreasing || input[i] < input[i+1] && decreasing {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
dif = int(math.Abs(float64(input[i] - input[i+1])))
|
||||||
|
if dif < 1 || dif > 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(input [][]int) int {
|
||||||
|
var result = 0
|
||||||
|
|
||||||
|
for _, line := range input {
|
||||||
|
if IsLineSafe(line) {
|
||||||
|
result++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input [][]int) int {
|
||||||
|
var result = 0
|
||||||
|
var temp []int
|
||||||
|
for _, line := range input {
|
||||||
|
if IsLineSafe(line) {
|
||||||
|
result++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(line); i++ {
|
||||||
|
temp = make([]int, 0)
|
||||||
|
temp = append(temp, line[:i]...)
|
||||||
|
temp = append(temp, line[i+1:]...)
|
||||||
|
if IsLineSafe(temp) {
|
||||||
|
result++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
02/sample.txt
Normal file
6
02/sample.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
7 6 4 2 1
|
||||||
|
1 2 7 8 9
|
||||||
|
9 7 6 2 1
|
||||||
|
1 3 2 4 5
|
||||||
|
8 6 4 4 1
|
||||||
|
1 3 6 7 9
|
||||||
Loading…
Reference in a new issue