2025 day 1 and 2
This commit is contained in:
94
2025/Day-01/day01.go
Normal file
94
2025/Day-01/day01.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func partOne() int {
|
||||
file, err := os.Open("input.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file: %s", err)
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
var startPos int = 50
|
||||
code := 0
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
dir := string(line[0])
|
||||
num, _ := strconv.Atoi(string(line[1:]))
|
||||
for num > 99 {
|
||||
num -= 100
|
||||
}
|
||||
switch dir {
|
||||
case "L":
|
||||
if (startPos - num) < 0 {
|
||||
startPos = 99 - (num - startPos - 1)
|
||||
} else {
|
||||
startPos -= num
|
||||
}
|
||||
case "R":
|
||||
if (startPos + num) > 99 {
|
||||
startPos = 0 + (num - (100 - startPos))
|
||||
} else {
|
||||
startPos += num
|
||||
}
|
||||
}
|
||||
if startPos == 0 {
|
||||
code++
|
||||
}
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
func partTwo() int {
|
||||
file, err := os.Open("input.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file: %s", err)
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
var startPos int = 50
|
||||
code := 0
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
dir := string(line[0])
|
||||
num, _ := strconv.Atoi(string(line[1:]))
|
||||
for num > 99 {
|
||||
num -= 100
|
||||
code += 1
|
||||
}
|
||||
switch dir {
|
||||
case "L":
|
||||
for i := 0; i<num; i++ {
|
||||
startPos--
|
||||
if startPos < 0 {
|
||||
startPos = 99
|
||||
}
|
||||
if startPos == 0 {
|
||||
code++
|
||||
}
|
||||
}
|
||||
case "R":
|
||||
for i := 0; i<num; i++ {
|
||||
startPos++
|
||||
if startPos > 99 {
|
||||
startPos = 0
|
||||
}
|
||||
if startPos == 0 {
|
||||
code++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(partOne())
|
||||
fmt.Println(partTwo())
|
||||
}
|
||||
97
2025/Day-02/day02.go
Normal file
97
2025/Day-02/day02.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func partOne() int {
|
||||
file, err := os.ReadFile("input.txt")
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
tot := 0
|
||||
for _, ids := range strings.Split(string(file), ",") {
|
||||
r := strings.Split(ids, "-")
|
||||
s, _ := strconv.Atoi(r[0])
|
||||
e, _ := strconv.Atoi(r[1])
|
||||
|
||||
for i := s; i <= e; i++ {
|
||||
curId := strconv.Itoa(i)
|
||||
if len(curId)%2 != 0 {
|
||||
continue
|
||||
}
|
||||
half := (len(curId) / 2)
|
||||
a := ""
|
||||
b := ""
|
||||
if len(curId) == 2 {
|
||||
a = string(curId[0])
|
||||
b = string(curId[1])
|
||||
} else {
|
||||
a = string(curId[:half])
|
||||
b = string(curId[half:])
|
||||
}
|
||||
if a == b {
|
||||
tot += i
|
||||
}
|
||||
}
|
||||
}
|
||||
return tot
|
||||
}
|
||||
|
||||
func toChunks(in string, l int) []string {
|
||||
nChunks := len(in) / l
|
||||
ret := make([]string, nChunks)
|
||||
for i := 0; i < nChunks; i++ {
|
||||
ret[i] = in[(l * i):(l*i + l)]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func allEqual(a []string) bool {
|
||||
for i := 1; i < len(a); i++ {
|
||||
if a[i] != a[0] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func partTwo() int {
|
||||
file, err := os.ReadFile("input.txt")
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
tot := 0
|
||||
for _, ids := range strings.Split(string(file), ",") {
|
||||
r := strings.Split(ids, "-")
|
||||
s, _ := strconv.Atoi(r[0])
|
||||
e, _ := strconv.Atoi(r[1])
|
||||
|
||||
for i := s; i <= e; i++ {
|
||||
curId := strconv.Itoa(i)
|
||||
valid := 0
|
||||
for c := 1; c <= len(curId)/2; c++ {
|
||||
if len(curId)%c != 0{
|
||||
continue
|
||||
}
|
||||
chunks := toChunks(curId, c)
|
||||
if allEqual(chunks) {
|
||||
valid += 1
|
||||
break
|
||||
}
|
||||
}
|
||||
if valid != 0 {
|
||||
tot += i
|
||||
}
|
||||
}
|
||||
}
|
||||
return tot
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(partOne())
|
||||
fmt.Println(partTwo())
|
||||
}
|
||||
Reference in New Issue
Block a user