Compare commits
2 Commits
015607cde3
...
9613baa5eb
| Author | SHA1 | Date | |
|---|---|---|---|
| 9613baa5eb | |||
| c82f1c5b30 |
@@ -20,46 +20,45 @@ type Instruction struct {
|
||||
Steps int
|
||||
}
|
||||
|
||||
func Part1(input string) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
position := 50
|
||||
code := 0
|
||||
func parseInput(input string) []Instruction {
|
||||
Instructions := make([]Instruction, 0)
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
var steps int
|
||||
var dir Dir
|
||||
steps = utils.MustAtoi(line[1:])
|
||||
switch line[0] {
|
||||
case 'L':
|
||||
steps = utils.MustAtoi(line[1:])
|
||||
dir = Left
|
||||
case 'R':
|
||||
steps = utils.MustAtoi(line[1:])
|
||||
dir = Right
|
||||
default:
|
||||
fmt.Println("Invalid direction")
|
||||
continue
|
||||
}
|
||||
if steps > 100 {
|
||||
steps = steps % 100
|
||||
}
|
||||
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
|
||||
}
|
||||
fmt.Print(Instructions)
|
||||
return Instructions
|
||||
}
|
||||
|
||||
func Part1(input string) int {
|
||||
|
||||
Instructions := parseInput(input)
|
||||
position := 50
|
||||
code := 0
|
||||
|
||||
for _, instr := range Instructions {
|
||||
if instr.Turn == Left {
|
||||
if position-instr.Steps < 0 {
|
||||
position = 100 + (position - instr.Steps)
|
||||
} else {
|
||||
position -= instr.Steps
|
||||
switch instr.Turn {
|
||||
case Left:
|
||||
position -= instr.Steps % 100
|
||||
case Right:
|
||||
position += instr.Steps % 100
|
||||
}
|
||||
} else if instr.Turn == Right {
|
||||
if position+instr.Steps >= 100 {
|
||||
position = (position + instr.Steps) - 100
|
||||
} else {
|
||||
position += instr.Steps
|
||||
if position < 0 {
|
||||
position += 100
|
||||
} else if position > 99 {
|
||||
position -= 100
|
||||
}
|
||||
}
|
||||
fmt.Println("\n", instr.Turn, instr.Steps, "->", position)
|
||||
if position == 0 {
|
||||
code++
|
||||
}
|
||||
@@ -68,61 +67,34 @@ func Part1(input string) int {
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
Instructions := parseInput(input)
|
||||
position := 50
|
||||
code := 0
|
||||
Instructions := make([]Instruction, 0)
|
||||
for _, line := range lines {
|
||||
var steps int
|
||||
var dir Dir
|
||||
switch line[0] {
|
||||
case 'L':
|
||||
steps = utils.MustAtoi(line[1:])
|
||||
dir = Left
|
||||
case 'R':
|
||||
steps = utils.MustAtoi(line[1:])
|
||||
dir = Right
|
||||
default:
|
||||
fmt.Println("Invalid direction")
|
||||
continue
|
||||
}
|
||||
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
|
||||
}
|
||||
fmt.Print(Instructions)
|
||||
new_position := position
|
||||
|
||||
for _, instr := range Instructions {
|
||||
if instr.Steps > 100 {
|
||||
code = code + (instr.Steps / 100)
|
||||
instr.Steps = instr.Steps % 100
|
||||
switch instr.Turn {
|
||||
case Left:
|
||||
new_position = position - instr.Steps%100
|
||||
case Right:
|
||||
new_position = position + instr.Steps%100
|
||||
}
|
||||
if instr.Turn == Left {
|
||||
if position-instr.Steps < 0 {
|
||||
code += instr.Steps / 100
|
||||
if new_position == 0 {
|
||||
code++
|
||||
}
|
||||
if new_position < 0 {
|
||||
new_position += 100
|
||||
if position != 0 {
|
||||
code++
|
||||
}
|
||||
position = 100 + (position - instr.Steps)
|
||||
|
||||
} else {
|
||||
position -= instr.Steps
|
||||
if position == 0 {
|
||||
code++
|
||||
}
|
||||
}
|
||||
} else if instr.Turn == Right {
|
||||
if position+instr.Steps > 99 {
|
||||
} else if new_position > 99 {
|
||||
new_position -= 100
|
||||
if position != 0 {
|
||||
code++
|
||||
}
|
||||
position = (position + instr.Steps) - 100
|
||||
} else {
|
||||
position += instr.Steps
|
||||
if position == 0 {
|
||||
code++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("\n", instr.Turn, instr.Steps, "->", position, "code:", code)
|
||||
|
||||
position = new_position
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
59
2025/go/day02/day02.go
Normal file
59
2025/go/day02/day02.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package day02
|
||||
|
||||
import (
|
||||
"adventofcode2025/utils"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
ranges := strings.Split(input, ",")
|
||||
count := 0
|
||||
for _, r := range ranges {
|
||||
min_s, max_s := strings.Split(r, "-")[0], strings.Split(r, "-")[1]
|
||||
min := utils.MustAtoi(min_s)
|
||||
max := utils.MustAtoi(max_s)
|
||||
for i := min; i <= max; i++ {
|
||||
s := fmt.Sprintf("%d", i)
|
||||
if len(s)%2 != 0 {
|
||||
continue
|
||||
}
|
||||
if s[:len(s)/2] != s[len(s)/2:] {
|
||||
continue
|
||||
}
|
||||
count += utils.MustAtoi(s)
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
ranges := strings.Split(input, ",")
|
||||
count := 0
|
||||
for _, r := range ranges {
|
||||
min_s, max_s := strings.Split(r, "-")[0], strings.Split(r, "-")[1]
|
||||
min := utils.MustAtoi(min_s)
|
||||
max := utils.MustAtoi(max_s)
|
||||
for i := min; i <= max; i++ {
|
||||
s := fmt.Sprintf("%d", i)
|
||||
for numDigits := 1; numDigits <= len(s)/2; numDigits++ {
|
||||
if len(s)%numDigits != 0 {
|
||||
continue
|
||||
}
|
||||
found := true
|
||||
for j := 1; j < len(s)/numDigits; j++ {
|
||||
if s[0:numDigits] != s[j*numDigits:(j+1)*numDigits] {
|
||||
found = false
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
if found {
|
||||
count += utils.MustAtoi(s)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
17
2025/go/day02/day02_test.go
Normal file
17
2025/go/day02/day02_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package day02
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1("446443-446449")
|
||||
require.Equal(t, 446446, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2("222220-222224")
|
||||
require.Equal(t, 222222, r)
|
||||
}
|
||||
1
2025/go/day02/input.txt
Normal file
1
2025/go/day02/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
69810572-69955342,3434061167-3434167492,76756725-76781020,49-147,296131-386620,910523-946587,34308309-34358652,64542-127485,640436-659023,25-45,35313993-35393518,753722181-753795479,1544-9792,256-647,444628-483065,5863911-6054673,6969623908-6969778569,658-1220,12631-63767,670238-830345,1-18,214165106-214245544,3309229-3355697
|
||||
135
2025/go/main.go
135
2025/go/main.go
@@ -9,6 +9,7 @@ import (
|
||||
// "strings"
|
||||
// "time"
|
||||
"adventofcode2025/day01"
|
||||
"adventofcode2025/day02"
|
||||
"adventofcode2025/utils"
|
||||
)
|
||||
|
||||
@@ -22,72 +23,72 @@ func main() {
|
||||
case 1:
|
||||
fmt.Printf("part 1: %d\n", day01.Part1(utils.Readfile(d)))
|
||||
fmt.Printf("part 2: %d\n", day01.Part2(utils.Readfile(d)))
|
||||
// case 2:
|
||||
// fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d)))
|
||||
// case 3:
|
||||
// fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d)))
|
||||
// case 4:
|
||||
// fmt.Printf("part 1: %d\n", day04.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day04.Part2(utils.Readfile(d)))
|
||||
// case 6:
|
||||
// fmt.Printf("part 1: %d\n", day06.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day06.Part2(utils.Readfile(d)))
|
||||
// case 7:
|
||||
// fmt.Printf("part 1: %d\n", day07.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day07.Part2(utils.Readfile(d)))
|
||||
// case 8:
|
||||
// fmt.Printf("part 1: %d\n", day08.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day08.Part2(utils.Readfile(d)))
|
||||
// case 9:
|
||||
// fmt.Printf("part 1: %d\n", day09.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day09.Part2(utils.Readfile(d)))
|
||||
// case 10:
|
||||
// fmt.Printf("part 1: %d\n", day10.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day10.Part2(utils.Readfile(d)))
|
||||
// case 11:
|
||||
// fmt.Printf("part 1: %d\n", day11.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day11.Part2(utils.Readfile(d)))
|
||||
// case 12:
|
||||
// fmt.Printf("part 1: %d\n", day12.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day12.Part2(utils.Readfile(d)))
|
||||
// case 13:
|
||||
// fmt.Printf("part 1: %d\n", day13.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day13.Part2(utils.Readfile(d)))
|
||||
// case 14:
|
||||
// fmt.Printf("part 1: %d\n", day14.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day14.Part2(utils.Readfile(d)))
|
||||
// case 15:
|
||||
// fmt.Printf("part 1: %d\n", day15.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day15.Part2(utils.Readfile(d)))
|
||||
// case 16:
|
||||
// fmt.Printf("part 1: %d\n", day16.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day16.Part2(utils.Readfile(d)))
|
||||
// case 17:
|
||||
// fmt.Printf("part 1: %s\n", day17.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day17.Part2(utils.Readfile(d)))
|
||||
// case 18:
|
||||
// fmt.Printf("part 1: %d\n", day18.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day18.Part2(utils.Readfile(d)))
|
||||
// case 19:
|
||||
// fmt.Printf("part 1: %d\n", day19.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day19.Part2(utils.Readfile(d)))
|
||||
// case 21:
|
||||
// fmt.Printf("part 1: %d\n", day21.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day21.Part2(utils.Readfile(d)))
|
||||
// case 22:
|
||||
// fmt.Printf("part 1: %d\n", day22.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day22.Part2(utils.Readfile(d)))
|
||||
// case 23:
|
||||
// fmt.Printf("part 1: %d\n", day23.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day23.Part2(utils.Readfile(d)))
|
||||
// case 24:
|
||||
// fmt.Printf("part 1: %d\n", day24.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day24.Part2(utils.Readfile(d)))
|
||||
// case 25:
|
||||
// fmt.Printf("part 1: %d\n", day25.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day25.Part2(utils.Readfile(d)))
|
||||
case 2:
|
||||
fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d)))
|
||||
fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d)))
|
||||
// case 3:
|
||||
// fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d)))
|
||||
// case 4:
|
||||
// fmt.Printf("part 1: %d\n", day04.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day04.Part2(utils.Readfile(d)))
|
||||
// case 6:
|
||||
// fmt.Printf("part 1: %d\n", day06.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day06.Part2(utils.Readfile(d)))
|
||||
// case 7:
|
||||
// fmt.Printf("part 1: %d\n", day07.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day07.Part2(utils.Readfile(d)))
|
||||
// case 8:
|
||||
// fmt.Printf("part 1: %d\n", day08.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day08.Part2(utils.Readfile(d)))
|
||||
// case 9:
|
||||
// fmt.Printf("part 1: %d\n", day09.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day09.Part2(utils.Readfile(d)))
|
||||
// case 10:
|
||||
// fmt.Printf("part 1: %d\n", day10.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day10.Part2(utils.Readfile(d)))
|
||||
// case 11:
|
||||
// fmt.Printf("part 1: %d\n", day11.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day11.Part2(utils.Readfile(d)))
|
||||
// case 12:
|
||||
// fmt.Printf("part 1: %d\n", day12.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day12.Part2(utils.Readfile(d)))
|
||||
// case 13:
|
||||
// fmt.Printf("part 1: %d\n", day13.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day13.Part2(utils.Readfile(d)))
|
||||
// case 14:
|
||||
// fmt.Printf("part 1: %d\n", day14.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day14.Part2(utils.Readfile(d)))
|
||||
// case 15:
|
||||
// fmt.Printf("part 1: %d\n", day15.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day15.Part2(utils.Readfile(d)))
|
||||
// case 16:
|
||||
// fmt.Printf("part 1: %d\n", day16.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day16.Part2(utils.Readfile(d)))
|
||||
// case 17:
|
||||
// fmt.Printf("part 1: %s\n", day17.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day17.Part2(utils.Readfile(d)))
|
||||
// case 18:
|
||||
// fmt.Printf("part 1: %d\n", day18.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day18.Part2(utils.Readfile(d)))
|
||||
// case 19:
|
||||
// fmt.Printf("part 1: %d\n", day19.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day19.Part2(utils.Readfile(d)))
|
||||
// case 21:
|
||||
// fmt.Printf("part 1: %d\n", day21.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day21.Part2(utils.Readfile(d)))
|
||||
// case 22:
|
||||
// fmt.Printf("part 1: %d\n", day22.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day22.Part2(utils.Readfile(d)))
|
||||
// case 23:
|
||||
// fmt.Printf("part 1: %d\n", day23.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day23.Part2(utils.Readfile(d)))
|
||||
// case 24:
|
||||
// fmt.Printf("part 1: %d\n", day24.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day24.Part2(utils.Readfile(d)))
|
||||
// case 25:
|
||||
// fmt.Printf("part 1: %d\n", day25.Part1(utils.Readfile(d)))
|
||||
// fmt.Printf("part 2: %d\n", day25.Part2(utils.Readfile(d)))
|
||||
default:
|
||||
panic(fmt.Errorf("no such day: %d", d))
|
||||
}
|
||||
@@ -97,7 +98,7 @@ func main() {
|
||||
|
||||
// Reads day from os.Args.
|
||||
func day() int {
|
||||
latest := 0
|
||||
latest := 1
|
||||
if len(os.Args) == 1 {
|
||||
return latest
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user