|
|
@@ -0,0 +1,58 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "bufio"
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+func main() {
|
|
|
+ fmt.Println("Advent of Code 2025 - Day 3 - Part 1")
|
|
|
+ f,_:= os.Open("3/input")
|
|
|
+ defer f.Close()
|
|
|
+ scanner := bufio.NewScanner(f)
|
|
|
+ var res int;
|
|
|
+ for scanner.Scan(){
|
|
|
+ bank := scanner.Text()
|
|
|
+ max_volt,_ := max_voltage(bank)
|
|
|
+ res += max_volt
|
|
|
+ }
|
|
|
+ fmt.Println(res)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func max_voltage(bank string) (int, error){
|
|
|
+ max1, max2 := byte('0'),byte('0')
|
|
|
+ i_max1, i_max2:= -1,-1
|
|
|
+ var result string
|
|
|
+
|
|
|
+ for i:= 0; i < len(bank); i++{
|
|
|
+ if bank[i] > max1{
|
|
|
+ max1,i_max1 = bank[i],i
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for i:= i_max1+1; i < len(bank); i++{
|
|
|
+ if bank[i] > max2{
|
|
|
+ max2,i_max2 = bank[i],i
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if i_max2 == -1 {
|
|
|
+ for i:= 0; i < len(bank); i++{
|
|
|
+ if bank[i] > max2 && i != i_max1{
|
|
|
+ max2,i_max2 = bank[i],i
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if i_max2 > i_max1 {
|
|
|
+ result = string([]byte{max1, max2})
|
|
|
+ } else {
|
|
|
+ result = string([]byte{max2, max1})
|
|
|
+ }
|
|
|
+
|
|
|
+ return strconv.Atoi(result)
|
|
|
+}
|