Compare commits

...

2 Commits

Author SHA1 Message Date
9613baa5eb day02 2025-12-02 18:23:07 +00:00
c82f1c5b30 updates 2025-12-02 18:23:07 +00:00
5 changed files with 188 additions and 138 deletions

View File

@@ -20,46 +20,45 @@ type Instruction struct {
Steps int Steps int
} }
func Part1(input string) int { func parseInput(input string) []Instruction {
lines := strings.Split(input, "\n")
position := 50
code := 0
Instructions := make([]Instruction, 0) Instructions := make([]Instruction, 0)
lines := strings.Split(input, "\n")
for _, line := range lines { for _, line := range lines {
var steps int var steps int
var dir Dir var dir Dir
steps = utils.MustAtoi(line[1:])
switch line[0] { switch line[0] {
case 'L': case 'L':
steps = utils.MustAtoi(line[1:])
dir = Left dir = Left
case 'R': case 'R':
steps = utils.MustAtoi(line[1:])
dir = Right dir = Right
default: default:
fmt.Println("Invalid direction") fmt.Println("Invalid direction")
continue continue
} }
if steps > 100 {
steps = steps % 100
}
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps}) 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 { for _, instr := range Instructions {
if instr.Turn == Left { switch instr.Turn {
if position-instr.Steps < 0 { case Left:
position = 100 + (position - instr.Steps) position -= instr.Steps % 100
} else { case Right:
position -= instr.Steps position += instr.Steps % 100
} }
} else if instr.Turn == Right { if position < 0 {
if position+instr.Steps >= 100 { position += 100
position = (position + instr.Steps) - 100 } else if position > 99 {
} else { position -= 100
position += instr.Steps
}
} }
fmt.Println("\n", instr.Turn, instr.Steps, "->", position)
if position == 0 { if position == 0 {
code++ code++
} }
@@ -68,61 +67,34 @@ func Part1(input string) int {
} }
func Part2(input string) int { func Part2(input string) int {
lines := strings.Split(input, "\n") Instructions := parseInput(input)
position := 50 position := 50
code := 0 code := 0
Instructions := make([]Instruction, 0) new_position := position
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)
for _, instr := range Instructions { for _, instr := range Instructions {
if instr.Steps > 100 { switch instr.Turn {
code = code + (instr.Steps / 100) case Left:
instr.Steps = instr.Steps % 100 new_position = position - instr.Steps%100
case Right:
new_position = position + instr.Steps%100
} }
if instr.Turn == Left { code += instr.Steps / 100
if position-instr.Steps < 0 { if new_position == 0 {
if position != 0 { code++
code++ }
} if new_position < 0 {
position = 100 + (position - instr.Steps) new_position += 100
if position != 0 {
} else { code++
position -= instr.Steps
if position == 0 {
code++
}
} }
} else if instr.Turn == Right { } else if new_position > 99 {
if position+instr.Steps > 99 { new_position -= 100
if position != 0 { if position != 0 {
code++ code++
}
position = (position + instr.Steps) - 100
} else {
position += instr.Steps
if position == 0 {
code++
}
} }
} }
position = new_position
fmt.Println("\n", instr.Turn, instr.Steps, "->", position, "code:", code)
} }
return code return code
} }

59
2025/go/day02/day02.go Normal file
View 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
}

View 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
View 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

View File

@@ -9,6 +9,7 @@ import (
// "strings" // "strings"
// "time" // "time"
"adventofcode2025/day01" "adventofcode2025/day01"
"adventofcode2025/day02"
"adventofcode2025/utils" "adventofcode2025/utils"
) )
@@ -22,72 +23,72 @@ func main() {
case 1: case 1:
fmt.Printf("part 1: %d\n", day01.Part1(utils.Readfile(d))) fmt.Printf("part 1: %d\n", day01.Part1(utils.Readfile(d)))
fmt.Printf("part 2: %d\n", day01.Part2(utils.Readfile(d))) fmt.Printf("part 2: %d\n", day01.Part2(utils.Readfile(d)))
// case 2: case 2:
// fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d))) fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d))) fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d)))
// case 3: // case 3:
// fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d)))
// case 4: // case 4:
// fmt.Printf("part 1: %d\n", day04.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day04.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day04.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day04.Part2(utils.Readfile(d)))
// case 6: // case 6:
// fmt.Printf("part 1: %d\n", day06.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day06.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day06.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day06.Part2(utils.Readfile(d)))
// case 7: // case 7:
// fmt.Printf("part 1: %d\n", day07.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day07.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day07.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day07.Part2(utils.Readfile(d)))
// case 8: // case 8:
// fmt.Printf("part 1: %d\n", day08.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day08.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day08.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day08.Part2(utils.Readfile(d)))
// case 9: // case 9:
// fmt.Printf("part 1: %d\n", day09.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day09.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day09.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day09.Part2(utils.Readfile(d)))
// case 10: // case 10:
// fmt.Printf("part 1: %d\n", day10.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day10.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day10.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day10.Part2(utils.Readfile(d)))
// case 11: // case 11:
// fmt.Printf("part 1: %d\n", day11.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day11.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day11.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day11.Part2(utils.Readfile(d)))
// case 12: // case 12:
// fmt.Printf("part 1: %d\n", day12.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day12.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day12.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day12.Part2(utils.Readfile(d)))
// case 13: // case 13:
// fmt.Printf("part 1: %d\n", day13.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day13.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day13.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day13.Part2(utils.Readfile(d)))
// case 14: // case 14:
// fmt.Printf("part 1: %d\n", day14.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day14.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day14.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day14.Part2(utils.Readfile(d)))
// case 15: // case 15:
// fmt.Printf("part 1: %d\n", day15.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day15.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day15.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day15.Part2(utils.Readfile(d)))
// case 16: // case 16:
// fmt.Printf("part 1: %d\n", day16.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day16.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day16.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day16.Part2(utils.Readfile(d)))
// case 17: // case 17:
// fmt.Printf("part 1: %s\n", day17.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %s\n", day17.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day17.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day17.Part2(utils.Readfile(d)))
// case 18: // case 18:
// fmt.Printf("part 1: %d\n", day18.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day18.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day18.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day18.Part2(utils.Readfile(d)))
// case 19: // case 19:
// fmt.Printf("part 1: %d\n", day19.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day19.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day19.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day19.Part2(utils.Readfile(d)))
// case 21: // case 21:
// fmt.Printf("part 1: %d\n", day21.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day21.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day21.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day21.Part2(utils.Readfile(d)))
// case 22: // case 22:
// fmt.Printf("part 1: %d\n", day22.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day22.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day22.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day22.Part2(utils.Readfile(d)))
// case 23: // case 23:
// fmt.Printf("part 1: %d\n", day23.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day23.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day23.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day23.Part2(utils.Readfile(d)))
// case 24: // case 24:
// fmt.Printf("part 1: %d\n", day24.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day24.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day24.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day24.Part2(utils.Readfile(d)))
// case 25: // case 25:
// fmt.Printf("part 1: %d\n", day25.Part1(utils.Readfile(d))) // fmt.Printf("part 1: %d\n", day25.Part1(utils.Readfile(d)))
// fmt.Printf("part 2: %d\n", day25.Part2(utils.Readfile(d))) // fmt.Printf("part 2: %d\n", day25.Part2(utils.Readfile(d)))
default: default:
panic(fmt.Errorf("no such day: %d", d)) panic(fmt.Errorf("no such day: %d", d))
} }
@@ -97,7 +98,7 @@ func main() {
// Reads day from os.Args. // Reads day from os.Args.
func day() int { func day() int {
latest := 0 latest := 1
if len(os.Args) == 1 { if len(os.Args) == 1 {
return latest return latest
} }