Compare commits
2 Commits
2528bb064a
...
1f8652c176
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f8652c176 | |||
| c708aca1f6 |
62
2020/day03/main.go
Normal file
62
2020/day03/main.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseInput(file string) []string {
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatalf("Faild to read input file: %v", err)
|
||||
}
|
||||
lines := strings.Split(string(content), "\n")
|
||||
var data []string
|
||||
for _, line := range lines {
|
||||
data = append(data, line)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func PartOne(input []string) int {
|
||||
trees := 0
|
||||
column := 0
|
||||
for row := range input {
|
||||
if len(input[row]) == 0 {
|
||||
continue
|
||||
}
|
||||
if input[row][column%len(input[row])] == '#' {
|
||||
trees++
|
||||
}
|
||||
column += 3
|
||||
}
|
||||
return trees
|
||||
}
|
||||
|
||||
func PartTwo(input []string) int {
|
||||
result := 1
|
||||
slopes := [][]int{{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}}
|
||||
for _, slope := range slopes {
|
||||
trees := 0
|
||||
column := 0
|
||||
for row := 0; row < len(input); row += slope[1] {
|
||||
if len(input[row]) == 0 {
|
||||
continue
|
||||
}
|
||||
if input[row][column%len(input[row])] == '#' {
|
||||
trees++
|
||||
}
|
||||
column += slope[0]
|
||||
}
|
||||
result *= trees
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
data := parseInput("input.txt")
|
||||
fmt.Println("Part 1:", PartOne(data))
|
||||
fmt.Println("Part 2:", PartTwo(data))
|
||||
}
|
||||
45
2020/day03/main_test.go
Normal file
45
2020/day03/main_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPartOne(t *testing.T) {
|
||||
input := []string{
|
||||
"..##.......",
|
||||
"#...#...#..",
|
||||
".#....#..#.",
|
||||
"..#.#...#.#",
|
||||
".#...##..#.",
|
||||
"..#.##.....",
|
||||
".#.#.#....#",
|
||||
".#........#",
|
||||
"#.##...#...",
|
||||
"#...##....#",
|
||||
".#..#...#.#",
|
||||
}
|
||||
expected := 7
|
||||
got := PartOne(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartTwo(t *testing.T) {
|
||||
input := []string{
|
||||
"..##.......",
|
||||
"#...#...#..",
|
||||
".#....#..#.",
|
||||
"..#.#...#.#",
|
||||
".#...##..#.",
|
||||
"..#.##.....",
|
||||
".#.#.#....#",
|
||||
".#........#",
|
||||
"#.##...#...",
|
||||
"#...##....#",
|
||||
".#..#...#.#",
|
||||
}
|
||||
expected := 336
|
||||
got := PartTwo(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartTwo() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user