mirror of
https://github.com/tiennm99/adventofcode.git
synced 2026-10-14 18:18:12 +00:00
refactor(2025): day1
This commit is contained in:
1 parent
cb90b2f8de
commit
9a869dc923
5 files changed
+34
-40
No files matched your search
@@ -1,3 +0,0 @@
|
|||||||
module part1
|
|
||||||
|
|
||||||
go 1.23.4
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
file, _ := os.Open("input.txt")
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
current := 50
|
|
||||||
counter := 0
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
direction := line[0]
|
|
||||||
multiplier := 1
|
|
||||||
if direction == 'L' {
|
|
||||||
multiplier = -1
|
|
||||||
}
|
|
||||||
number, _ := strconv.Atoi(line[1:])
|
|
||||||
current += number * multiplier
|
|
||||||
current = ((current % 100) + 100) % 100
|
|
||||||
if current == 0 {
|
|
||||||
counter++
|
|
||||||
}
|
|
||||||
// fmt.Printf("%s %c %d %d %d %d\n", line, direction, number, multiplier, current, counter)
|
|
||||||
}
|
|
||||||
fmt.Printf("%d\n", counter)
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
module part2
|
|
||||||
|
|
||||||
go 1.23.4
|
|
||||||
@@ -8,7 +8,32 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func part1() {
|
||||||
|
file, _ := os.Open("input.txt")
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
current := 50
|
||||||
|
counter := 0
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
direction := line[0]
|
||||||
|
multiplier := 1
|
||||||
|
if direction == 'L' {
|
||||||
|
multiplier = -1
|
||||||
|
}
|
||||||
|
number, _ := strconv.Atoi(line[1:])
|
||||||
|
current += number * multiplier
|
||||||
|
current = ((current % 100) + 100) % 100
|
||||||
|
if current == 0 {
|
||||||
|
counter++
|
||||||
|
}
|
||||||
|
// fmt.Printf("%s %c %d %d %d %d\n", line, direction, number, multiplier, current, counter)
|
||||||
|
}
|
||||||
|
fmt.Printf("%d\n", counter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2() {
|
||||||
file, _ := os.Open("input.txt")
|
file, _ := os.Open("input.txt")
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
@@ -36,3 +61,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Printf("%d\n", counter)
|
fmt.Printf("%d\n", counter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
part1()
|
||||||
|
part2()
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module day1
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
Reference in new issue
Block a user