-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03_1.go
47 lines (39 loc) · 1020 Bytes
/
day03_1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package day03
import (
"AdventOfCode_2015_Go/utilities"
)
type HouseKey struct {
row, col int
}
func Executepart1() int {
var fileName string = "./day03/day03.txt"
// var fileName string = "./day03/test_input_03.txt"
var result int = 0
if fileContent, err := utilities.ReadFileAsText(fileName); err == nil {
chars := []rune(fileContent)
// fmt.Printf("%c\n", chars)
result = runInstructions1(chars)
}
return result
}
func runInstructions1(steps []rune) int {
movements := make(map[rune]([]int))
movements['v'] = []int{1, 0}
movements['^'] = []int{-1, 0}
movements['>'] = []int{0, 1}
movements['<'] = []int{0, -1}
houses := make(map[HouseKey](int))
initHouse := HouseKey{0, 0}
houses[initHouse] = 1
var newPos HouseKey
for _, v := range steps {
newPos = HouseKey{initHouse.row + movements[v][0], initHouse.col + movements[v][1]}
if presents, ok := houses[newPos]; ok {
houses[newPos] = presents + 1
} else {
houses[newPos] = 1
}
initHouse = newPos
}
return len(houses)
}