Solve three!
This commit is contained in:
parent
d2922dd41d
commit
1eeb20c606
2 changed files with 143 additions and 0 deletions
87
2025/three/three.go
Normal file
87
2025/three/three.go
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseBank(text string) []int {
|
||||||
|
bank := make([]int, 0, len(text))
|
||||||
|
for _, char := range text {
|
||||||
|
battery, err := strconv.Atoi(string(char))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
bank = append(bank, battery)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bank
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
file, err := os.Open("three.txt")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
ch := make(chan int)
|
||||||
|
n_banks := 0
|
||||||
|
jolts := 0
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
n_banks++
|
||||||
|
text := scanner.Text()
|
||||||
|
|
||||||
|
bank := parseBank(text)
|
||||||
|
|
||||||
|
go joltage(bank, ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < n_banks; i++ {
|
||||||
|
jolts += <-ch
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(jolts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func joltage(bank []int, ch chan int) {
|
||||||
|
ch <- _joltage(bank)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _joltage(bank []int) int {
|
||||||
|
out := 0
|
||||||
|
|
||||||
|
startIndex := 0
|
||||||
|
|
||||||
|
for endIndex := len(bank) - 11; endIndex <= len(bank); endIndex++ {
|
||||||
|
|
||||||
|
out *= 10
|
||||||
|
highest, index := highestEarly(bank, startIndex, endIndex)
|
||||||
|
out += highest
|
||||||
|
startIndex = index + 1
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func highestEarly(bank []int, start int, end int) (int, int) {
|
||||||
|
highest := 0
|
||||||
|
index := 0
|
||||||
|
|
||||||
|
for i := start; i < end; i++ {
|
||||||
|
val := bank[i]
|
||||||
|
if val > highest {
|
||||||
|
highest = val
|
||||||
|
index = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return highest, index
|
||||||
|
}
|
||||||
56
2025/three/three_test.go
Normal file
56
2025/three/three_test.go
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
type highestTest struct {
|
||||||
|
bank string
|
||||||
|
start int
|
||||||
|
end int
|
||||||
|
expected, expectedIndex int
|
||||||
|
}
|
||||||
|
|
||||||
|
var highestTests = []highestTest{
|
||||||
|
{"999", 0, 3, 9, 0},
|
||||||
|
{"919", 0, 3, 9, 0},
|
||||||
|
{"919", 1, 3, 9, 2},
|
||||||
|
{"818181911112111", 0, 4, 8, 0},
|
||||||
|
{"818181911112111", 1, 5, 8, 2},
|
||||||
|
{"818181911112111", 3, 6, 8, 4},
|
||||||
|
{"818181911112111", 5, 7, 9, 6},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHhigest(t *testing.T) {
|
||||||
|
for _, test := range highestTests {
|
||||||
|
highest, index := highestEarly(parseBank(test.bank), test.start, test.end)
|
||||||
|
|
||||||
|
if highest != test.expected {
|
||||||
|
t.Errorf("Expected %d got %d for bank %v", test.expected, highest, test.bank)
|
||||||
|
}
|
||||||
|
|
||||||
|
if index != test.expectedIndex {
|
||||||
|
t.Errorf("Expected %d got %d for bank %v", test.expectedIndex, index, test.bank)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type joltageTest struct {
|
||||||
|
bank string
|
||||||
|
expected int
|
||||||
|
}
|
||||||
|
|
||||||
|
var joltageTests = []joltageTest{
|
||||||
|
{"987654321111111", 987654321111},
|
||||||
|
{"811111111111119", 811111111119},
|
||||||
|
{"234234234234278", 434234234278},
|
||||||
|
{"818181911112111", 888911112111},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJoltage(t *testing.T) {
|
||||||
|
for _, test := range joltageTests {
|
||||||
|
out := _joltage(parseBank(test.bank))
|
||||||
|
|
||||||
|
if out != test.expected {
|
||||||
|
t.Errorf("Expected %d got %d for bank %v", test.expected, out, test.bank)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue