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

Commit 65c2136

Browse files
committed
push improvements
1 parent ccfd506 commit 65c2136

File tree

21 files changed

+188
-404
lines changed

21 files changed

+188
-404
lines changed

day1/day1.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func parseData() DataType {
2222
}
2323

2424
func solvePart1(data DataType) (rc int) {
25-
for _, v1 := range data {
26-
for _, v2 := range data {
25+
for i, v1 := range data {
26+
for _, v2 := range data[i+1:] {
2727
if v1+v2 == 2020 {
2828
return v1 * v2
2929
}
@@ -34,9 +34,9 @@ func solvePart1(data DataType) (rc int) {
3434
}
3535

3636
func solvePart2(data DataType) (rc int) {
37-
for _, v1 := range data {
38-
for _, v2 := range data {
39-
for _, v3 := range data {
37+
for i, v1 := range data {
38+
for j, v2 := range data[i+1:] {
39+
for _, v3 := range data[j+1:] {
4040
if v1+v2+v3 == 2020 {
4141
return v1 * v2 * v3
4242
}

day11/day11.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ var MAXX int
1111
var MAXY int
1212

1313
const (
14-
StateEmptySeat = 1
15-
StateOccupiedSeat = 2
14+
StateEmptySeat SeatState = 1 // iota
15+
StateOccupiedSeat SeatState = 2 // iota
1616
)
1717

18-
type DataType map[Location]int
18+
type SeatState int
19+
20+
type DataType map[Location]SeatState
1921

2022
func parseData() DataType {
2123
data := FetchInputData(11)
@@ -62,7 +64,7 @@ func findNeighboursOccupiedInf(data DataType, location Location) (rc int) {
6264
return
6365
}
6466

65-
func applyRulesPart1(data DataType, location Location) int {
67+
func applyRulesPart1(data DataType, location Location) SeatState {
6668
neighboursOccupied := findNeighboursOccupied(data, location)
6769

6870
if data[location] == StateEmptySeat && neighboursOccupied == 0 {
@@ -76,7 +78,7 @@ func applyRulesPart1(data DataType, location Location) int {
7678
return data[location]
7779
}
7880

79-
func applyRulesPart2(data DataType, location Location) int {
81+
func applyRulesPart2(data DataType, location Location) SeatState {
8082
neighboursOccupiedInf := findNeighboursOccupiedInf(data, location)
8183

8284
if data[location] == StateEmptySeat && neighboursOccupiedInf == 0 {
@@ -90,7 +92,7 @@ func applyRulesPart2(data DataType, location Location) int {
9092
return data[location]
9193
}
9294

93-
func transform(data DataType, applyRules func(DataType, Location) int) DataType {
95+
func transform(data DataType, applyRules func(DataType, Location) SeatState) DataType {
9496
newData := make(DataType, len(data))
9597
for location := range data {
9698
newData[location] = applyRules(data, location)
@@ -108,7 +110,7 @@ func countOccupied(data DataType) (rc int) {
108110
return
109111
}
110112

111-
func solve(data DataType, applyRules func(DataType, Location) int) int {
113+
func solve(data DataType, applyRules func(DataType, Location) SeatState) int {
112114
oldData := transform(data, applyRules)
113115
newData := transform(oldData, applyRules)
114116

day12/day12.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
. "../util"
55
"fmt"
6-
"strconv"
76
"strings"
87
)
98

@@ -20,10 +19,7 @@ func parseData() DataType {
2019

2120
result := make(DataType, len(dataSplit))
2221
for i, v := range dataSplit {
23-
action := v[0]
24-
value, _ := strconv.Atoi(v[1:])
25-
26-
result[i] = Instruction{action, value}
22+
_, _ = fmt.Sscanf(v, "%c%d", &result[i].action, &result[i].value)
2723
}
2824

2925
return result
@@ -55,16 +51,12 @@ func nextDirectionPart1(direction byte, code byte, value int) (n byte) {
5551
switch n {
5652
case 'N':
5753
n = 'E'
58-
break
5954
case 'E':
6055
n = 'S'
61-
break
6256
case 'S':
6357
n = 'W'
64-
break
6558
case 'W':
6659
n = 'N'
67-
break
6860
}
6961
value -= 90
7062
}
@@ -92,11 +84,12 @@ func solvePart1(data DataType) (rc int) {
9284
position := Location{}
9385

9486
for _, instruction := range data {
95-
if instruction.action == 'R' || instruction.action == 'L' {
87+
switch instruction.action {
88+
case 'L', 'R':
9689
direction = nextDirectionPart1(direction, instruction.action, instruction.value)
97-
} else if instruction.action == 'F' {
90+
case 'F':
9891
position = nextLocation(position, direction, instruction.value)
99-
} else {
92+
default:
10093
position = nextLocation(position, instruction.action, instruction.value)
10194
}
10295
}
@@ -109,11 +102,12 @@ func solvePart2(data DataType) (rc int) {
109102
position := Location{}
110103

111104
for _, instruction := range data {
112-
if instruction.action == 'R' || instruction.action == 'L' {
105+
switch instruction.action {
106+
case 'L', 'R':
113107
waypoint = nextWaypointRotatePart2(waypoint, instruction.action, instruction.value)
114-
} else if instruction.action == 'F' {
108+
case 'F':
115109
position = position.Add(waypoint.Mul(instruction.value))
116-
} else {
110+
default:
117111
waypoint = nextLocation(waypoint, instruction.action, instruction.value)
118112
}
119113
}

day13/day13.go

+10-29
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
. "../util"
55
"fmt"
6-
"math/big"
6+
"math"
77
"strconv"
88
"strings"
99
)
@@ -33,26 +33,7 @@ func parseData() DataType {
3333
}
3434
}
3535

36-
result := DataType{
37-
myTimestamp: myTimestamp,
38-
buses: buses,
39-
}
40-
41-
return result
42-
}
43-
44-
func ChineseRemainderTheorem(a []*big.Int, n []*big.Int) *big.Int {
45-
p := new(big.Int).Set(n[0])
46-
for _, n1 := range n[1:] {
47-
p.Mul(p, n1)
48-
}
49-
var x, q, s, z big.Int
50-
for i, n1 := range n {
51-
q.Div(p, n1)
52-
z.GCD(nil, &s, n1, &q)
53-
x.Add(&x, s.Mul(a[i], s.Mul(&s, &q)))
54-
}
55-
return x.Mod(&x, p)
36+
return DataType{myTimestamp, buses}
5637
}
5738

5839
func calculateWaitingTime(myTimestamp int, busId int) int {
@@ -61,7 +42,7 @@ func calculateWaitingTime(myTimestamp int, busId int) int {
6142

6243
func solvePart1(data DataType) (rc int) {
6344
minBusId := data.buses[0].id
64-
minWaitingTime := calculateWaitingTime(data.myTimestamp, minBusId)
45+
minWaitingTime := math.MaxInt64
6546

6647
for _, bus := range data.buses {
6748
waitingTime := calculateWaitingTime(data.myTimestamp, bus.id)
@@ -74,16 +55,16 @@ func solvePart1(data DataType) (rc int) {
7455
return minBusId * minWaitingTime
7556
}
7657

77-
func solvePart2(data DataType) (rc *big.Int) {
78-
a := make([]*big.Int, 0, len(data.buses))
79-
n := make([]*big.Int, 0, len(data.buses))
58+
func solvePart2(data DataType) (rc int) {
59+
step := 1
8060
for _, bus := range data.buses {
81-
a = append(a, big.NewInt(int64(-bus.departTimestamp)))
82-
n = append(n, big.NewInt(int64(bus.id)))
61+
for (rc+bus.departTimestamp)%bus.id != 0 {
62+
rc += step
63+
}
64+
step *= bus.id
8365
}
8466

85-
x := ChineseRemainderTheorem(a, n)
86-
return x
67+
return
8768
}
8869

8970
func main() {

day16/day16.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
. "../util"
55
"fmt"
6-
"regexp"
76
"strconv"
87
"strings"
98
)
@@ -30,36 +29,29 @@ func parseData() DataType {
3029
data := FetchInputData(16)
3130
dataSplit := strings.Split(data, "\n\n")
3231

33-
typesLineRe := regexp.MustCompile("^(.*): ([0-9]+)-([0-9]+) or ([0-9]+)-([0-9]+)$")
3432
typeLines := strings.Split(dataSplit[0], "\n")
3533
types := make([]Type, len(typeLines))
3634
for i, typesLine := range typeLines {
37-
match := typesLineRe.FindStringSubmatch(typesLine)
38-
name := match[1]
39-
ruleFrom1, _ := strconv.Atoi(match[2])
40-
ruleTo1, _ := strconv.Atoi(match[3])
41-
ruleFrom2, _ := strconv.Atoi(match[4])
42-
ruleTo2, _ := strconv.Atoi(match[5])
43-
44-
types[i] = Type{name, Rule{ruleFrom1, ruleTo1, ruleFrom2, ruleTo2}}
35+
typesLineSplit := strings.Split(typesLine, ": ")
36+
types[i].name = typesLineSplit[0]
37+
_, _ = fmt.Sscanf(typesLineSplit[1], "%d-%d or %d-%d", &types[i].rule.from1, &types[i].rule.to1, &types[i].rule.from2, &types[i].rule.to2)
4538
}
4639

4740
myTicketLine := strings.Split(dataSplit[1], "\n")[1]
48-
myTicket := make([]int, 0)
49-
for _, v := range strings.Split(myTicketLine, ",") {
50-
vi, _ := strconv.Atoi(v)
51-
myTicket = append(myTicket, vi)
41+
myTicketLineSplit := strings.Split(myTicketLine, ",")
42+
myTicket := make([]int, len(myTicketLineSplit))
43+
for i, v := range myTicketLineSplit {
44+
myTicket[i], _ = strconv.Atoi(v)
5245
}
5346

5447
nearbyTicketsLines := strings.Split(dataSplit[2], "\n")[1:]
5548
nearbyTickets := make([][]int, len(nearbyTicketsLines))
5649
for i, nearbyTicketsLine := range nearbyTicketsLines {
57-
tmp := make([]int, 0)
58-
for _, v := range strings.Split(nearbyTicketsLine, ",") {
59-
vi, _ := strconv.Atoi(v)
60-
tmp = append(tmp, vi)
50+
nearbyTicketsLineSplit := strings.Split(nearbyTicketsLine, ",")
51+
nearbyTickets[i] = make([]int, len(nearbyTicketsLineSplit))
52+
for ii, v := range nearbyTicketsLineSplit {
53+
nearbyTickets[i][ii], _ = strconv.Atoi(v)
6154
}
62-
nearbyTickets[i] = tmp
6355
}
6456

6557
return DataType{types, myTicket, nearbyTickets}

day17/day17.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ func findInactiveNeighbors(data map[LocationXYXW]bool, el LocationXYXW, dimensio
3737

3838
minDw, maxDw := 0, 0
3939
if dimensions == 4 {
40-
minDw = -1
41-
maxDw = 1
40+
minDw, maxDw = -1, 1
4241
}
4342

4443
for dw := minDw; dw <= maxDw; dw++ {

0 commit comments

Comments
 (0)