Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit ccfd506

Browse files
committed
my go solutions
1 parent 1326b96 commit ccfd506

File tree

33 files changed

+3075
-0
lines changed

33 files changed

+3075
-0
lines changed

day1/day1.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
. "../util"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type DataType []int
11+
12+
func parseData() DataType {
13+
data := FetchInputData(1)
14+
dataSplit := strings.Split(data, "\n")
15+
16+
result := make(DataType, len(dataSplit))
17+
for i, v := range dataSplit {
18+
result[i], _ = strconv.Atoi(v)
19+
}
20+
21+
return result
22+
}
23+
24+
func solvePart1(data DataType) (rc int) {
25+
for _, v1 := range data {
26+
for _, v2 := range data {
27+
if v1+v2 == 2020 {
28+
return v1 * v2
29+
}
30+
}
31+
}
32+
33+
return
34+
}
35+
36+
func solvePart2(data DataType) (rc int) {
37+
for _, v1 := range data {
38+
for _, v2 := range data {
39+
for _, v3 := range data {
40+
if v1+v2+v3 == 2020 {
41+
return v1 * v2 * v3
42+
}
43+
}
44+
}
45+
}
46+
47+
return
48+
}
49+
50+
func main() {
51+
data := parseData()
52+
fmt.Println(solvePart1(data))
53+
fmt.Println(solvePart2(data))
54+
}

day10/day10.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
. "../util"
5+
"fmt"
6+
"sort"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
type DataType []int
12+
13+
func parseData() DataType {
14+
data := FetchInputData(10)
15+
dataSplit := strings.Split(data, "\n")
16+
17+
result := make(DataType, len(dataSplit))
18+
for i, v := range dataSplit {
19+
result[i], _ = strconv.Atoi(v)
20+
}
21+
22+
sort.Ints(result)
23+
return result
24+
}
25+
26+
func solvePart1(data DataType) (rc int) {
27+
differences := make(map[int]int)
28+
29+
prev := 0
30+
for _, x := range data {
31+
differences[x-prev] += 1
32+
prev = x
33+
}
34+
35+
return differences[1] * (differences[3] + 1)
36+
}
37+
38+
func solvePart2(data DataType) (rc int) {
39+
data = append(data, data[len(data)-1]+3)
40+
41+
lastUsed := map[int]int{0: 1}
42+
lastPrevUsed := lastUsed
43+
for i := 0; i < len(data)-1; i++ {
44+
lastPrevUsed = lastUsed
45+
lastUsed = make(map[int]int)
46+
for f, v := range lastPrevUsed {
47+
if data[i+1]-f <= 3 {
48+
lastUsed[f] += v
49+
}
50+
lastUsed[data[i]] += v
51+
}
52+
}
53+
54+
for _, v := range lastUsed {
55+
return v
56+
}
57+
58+
return
59+
}
60+
61+
func main() {
62+
data := parseData()
63+
fmt.Println(solvePart1(data))
64+
fmt.Println(solvePart2(data))
65+
}

day11/day11.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package main
2+
3+
import (
4+
. "../util"
5+
"fmt"
6+
"reflect"
7+
"strings"
8+
)
9+
10+
var MAXX int
11+
var MAXY int
12+
13+
const (
14+
StateEmptySeat = 1
15+
StateOccupiedSeat = 2
16+
)
17+
18+
type DataType map[Location]int
19+
20+
func parseData() DataType {
21+
data := FetchInputData(11)
22+
dataSplit := strings.Split(data, "\n")
23+
24+
MAXY = len(dataSplit)
25+
MAXX = len(dataSplit[0])
26+
27+
result := make(DataType)
28+
for y, dy := range dataSplit {
29+
for x, dx := range dy {
30+
if string(dx) == "L" {
31+
result[Location{X: x, Y: y}] = StateEmptySeat
32+
}
33+
}
34+
}
35+
36+
return result
37+
}
38+
39+
func findNeighboursOccupied(data DataType, location Location) (rc int) {
40+
for _, n := range GetNeighbours8() {
41+
if data[location.Add(n)] == StateOccupiedSeat {
42+
rc++
43+
}
44+
}
45+
46+
return
47+
}
48+
49+
func findNeighboursOccupiedInf(data DataType, location Location) (rc int) {
50+
for _, neighbour := range GetNeighbours8() {
51+
nl := location.Add(neighbour)
52+
for data[nl] != StateEmptySeat && 0 <= nl.X && nl.X <= MAXX && 0 <= nl.Y && nl.Y <= MAXY {
53+
if data[nl] == StateOccupiedSeat {
54+
rc++
55+
break
56+
}
57+
58+
nl = nl.Add(neighbour)
59+
}
60+
}
61+
62+
return
63+
}
64+
65+
func applyRulesPart1(data DataType, location Location) int {
66+
neighboursOccupied := findNeighboursOccupied(data, location)
67+
68+
if data[location] == StateEmptySeat && neighboursOccupied == 0 {
69+
return StateOccupiedSeat
70+
}
71+
72+
if data[location] == StateOccupiedSeat && neighboursOccupied >= 4 {
73+
return StateEmptySeat
74+
}
75+
76+
return data[location]
77+
}
78+
79+
func applyRulesPart2(data DataType, location Location) int {
80+
neighboursOccupiedInf := findNeighboursOccupiedInf(data, location)
81+
82+
if data[location] == StateEmptySeat && neighboursOccupiedInf == 0 {
83+
return StateOccupiedSeat
84+
}
85+
86+
if data[location] == StateOccupiedSeat && neighboursOccupiedInf >= 5 {
87+
return StateEmptySeat
88+
}
89+
90+
return data[location]
91+
}
92+
93+
func transform(data DataType, applyRules func(DataType, Location) int) DataType {
94+
newData := make(DataType, len(data))
95+
for location := range data {
96+
newData[location] = applyRules(data, location)
97+
}
98+
return newData
99+
}
100+
101+
func countOccupied(data DataType) (rc int) {
102+
for _, v := range data {
103+
if v == StateOccupiedSeat {
104+
rc++
105+
}
106+
}
107+
108+
return
109+
}
110+
111+
func solve(data DataType, applyRules func(DataType, Location) int) int {
112+
oldData := transform(data, applyRules)
113+
newData := transform(oldData, applyRules)
114+
115+
for !reflect.DeepEqual(oldData, newData) {
116+
oldData = newData
117+
newData = transform(oldData, applyRules)
118+
}
119+
120+
return countOccupied(newData)
121+
}
122+
123+
func solvePart1(data DataType) (rc int) {
124+
return solve(data, applyRulesPart1)
125+
}
126+
127+
func solvePart2(data DataType) (rc int) {
128+
return solve(data, applyRulesPart2)
129+
}
130+
131+
func main() {
132+
data := parseData()
133+
fmt.Println(solvePart1(data))
134+
fmt.Println(solvePart2(data))
135+
}

day12/day12.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package main
2+
3+
import (
4+
. "../util"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type DataType []Instruction
11+
12+
type Instruction struct {
13+
action byte
14+
value int
15+
}
16+
17+
func parseData() DataType {
18+
data := FetchInputData(12)
19+
dataSplit := strings.Split(data, "\n")
20+
21+
result := make(DataType, len(dataSplit))
22+
for i, v := range dataSplit {
23+
action := v[0]
24+
value, _ := strconv.Atoi(v[1:])
25+
26+
result[i] = Instruction{action, value}
27+
}
28+
29+
return result
30+
}
31+
32+
func nextLocation(l Location, code byte, value int) Location {
33+
switch code {
34+
case 'N':
35+
return l.Add(Location{Y: value})
36+
case 'E':
37+
return l.Add(Location{X: value})
38+
case 'S':
39+
return l.Add(Location{Y: -value})
40+
case 'W':
41+
return l.Add(Location{X: -value})
42+
}
43+
44+
return Location{}
45+
}
46+
47+
func nextDirectionPart1(direction byte, code byte, value int) (n byte) {
48+
value = value % 360
49+
if code == 'L' {
50+
value = 360 - value
51+
}
52+
53+
n = direction
54+
for value > 0 {
55+
switch n {
56+
case 'N':
57+
n = 'E'
58+
break
59+
case 'E':
60+
n = 'S'
61+
break
62+
case 'S':
63+
n = 'W'
64+
break
65+
case 'W':
66+
n = 'N'
67+
break
68+
}
69+
value -= 90
70+
}
71+
72+
return
73+
}
74+
75+
func nextWaypointRotatePart2(waypoint Location, code byte, value int) (n Location) {
76+
value = value % 360
77+
if code == 'L' {
78+
value = 360 - value
79+
}
80+
81+
n = waypoint
82+
for value > 0 {
83+
n = Location{X: n.Y, Y: -n.X}
84+
value -= 90
85+
}
86+
87+
return
88+
}
89+
90+
func solvePart1(data DataType) (rc int) {
91+
direction := byte('E')
92+
position := Location{}
93+
94+
for _, instruction := range data {
95+
if instruction.action == 'R' || instruction.action == 'L' {
96+
direction = nextDirectionPart1(direction, instruction.action, instruction.value)
97+
} else if instruction.action == 'F' {
98+
position = nextLocation(position, direction, instruction.value)
99+
} else {
100+
position = nextLocation(position, instruction.action, instruction.value)
101+
}
102+
}
103+
104+
return Abs(position.X) + Abs(position.Y)
105+
}
106+
107+
func solvePart2(data DataType) (rc int) {
108+
waypoint := Location{X: 10, Y: 1}
109+
position := Location{}
110+
111+
for _, instruction := range data {
112+
if instruction.action == 'R' || instruction.action == 'L' {
113+
waypoint = nextWaypointRotatePart2(waypoint, instruction.action, instruction.value)
114+
} else if instruction.action == 'F' {
115+
position = position.Add(waypoint.Mul(instruction.value))
116+
} else {
117+
waypoint = nextLocation(waypoint, instruction.action, instruction.value)
118+
}
119+
}
120+
121+
return Abs(position.X) + Abs(position.Y)
122+
}
123+
124+
func main() {
125+
data := parseData()
126+
fmt.Println(solvePart1(data))
127+
fmt.Println(solvePart2(data))
128+
}

0 commit comments

Comments
 (0)