浏览代码

day 3 part 1

Abderrahmane Faiz 2 周之前
父节点
当前提交
a9b55adee8
共有 2 个文件被更改,包括 59 次插入1 次删除
  1. 58 0
      3/solve_1.go
  2. 1 1
      README.md

+ 58 - 0
3/solve_1.go

@@ -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)
+}

+ 1 - 1
README.md

@@ -22,7 +22,7 @@ Each day's folder is named after the day number (`1` to `12`). Inside each folde
 |-----|--------|--------|
 | 1   | ✅     | ✅     |
 | 2   | ✅     | ✅     |
-| 3   |      | ⬜     |
+| 3   |      | ⬜     |
 | 4   | ⬜     | ⬜     |
 | 5   | ⬜     | ⬜     |
 | 6   | ⬜     | ⬜     |