Skip to content

Commit b14aed3

Browse files
author
Miloš Rackov
committedDec 21, 2022
Day 21 Go
1 parent ccf1afc commit b14aed3

File tree

10 files changed

+245
-12
lines changed

10 files changed

+245
-12
lines changed
 

‎Day21/Go/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module AoC2022D21
2+
3+
go 1.19

‎Day21/Go/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package main
7+
8+
import (
9+
"AoC2022D21/part1"
10+
"AoC2022D21/part2"
11+
"fmt"
12+
"os"
13+
)
14+
15+
func main() {
16+
inputDay21, err := os.ReadFile("./input.txt")
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
p1 := part1.Part1(string(inputDay21))
22+
fmt.Printf("Result for part 1: %d\n", p1)
23+
p2 := part2.Part2(string(inputDay21))
24+
fmt.Printf("Result for part 2: %d\n", p2)
25+
}

‎Day21/Go/part1/part1.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part1
7+
8+
import (
9+
"strconv"
10+
"strings"
11+
)
12+
13+
var monkeys = map[string]string{}
14+
15+
func Part1(input string) int {
16+
for _, s := range strings.Split(input, "\n") {
17+
s := strings.Split(s, ": ")
18+
monkeys[s[0]] = s[1]
19+
}
20+
return solve("root")
21+
}
22+
23+
func solve(expr string) int {
24+
if v, err := strconv.Atoi(monkeys[expr]); err == nil {
25+
return v
26+
}
27+
28+
s := strings.Fields(monkeys[expr])
29+
return map[string]func(int, int) int{
30+
"+": func(a, b int) int { return a + b },
31+
"-": func(a, b int) int { return a - b },
32+
"*": func(a, b int) int { return a * b },
33+
"/": func(a, b int) int { return a / b },
34+
}[s[1]](solve(s[0]), solve(s[2]))
35+
}

‎Day21/Go/part1/part1_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part1
7+
8+
import (
9+
"os"
10+
"testing"
11+
)
12+
13+
func TestPart1(t *testing.T) {
14+
testInput, err := os.ReadFile("../testinput.txt")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
type args struct {
20+
input string
21+
}
22+
tests := []struct {
23+
name string
24+
args args
25+
want int
26+
}{
27+
{
28+
name: "Provided input",
29+
args: args{
30+
input: string(testInput),
31+
},
32+
want: 152,
33+
},
34+
}
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
if got := Part1(tt.args.input); got != tt.want {
38+
t.Errorf("Part1() = %v, want %v", got, tt.want)
39+
}
40+
})
41+
}
42+
}
43+
44+
func BenchmarkPart1(b *testing.B) {
45+
testInput, err := os.ReadFile("../input.txt")
46+
if err != nil {
47+
b.Fatal(err)
48+
}
49+
b.ResetTimer()
50+
for i := 0; i < b.N; i++ {
51+
Part1(string(testInput))
52+
}
53+
}

‎Day21/Go/part2/part2.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part2
7+
8+
import (
9+
"sort"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
var monkeys = map[string]string{}
15+
16+
func Part2(input string) int {
17+
for _, s := range strings.Split(input, "\n") {
18+
s := strings.Split(s, ": ")
19+
monkeys[s[0]] = s[1]
20+
}
21+
22+
monkeys["humn"] = "0"
23+
s := strings.Fields(monkeys["root"])
24+
if solve(s[0]) < solve(s[2]) {
25+
s[0], s[2] = s[2], s[0]
26+
}
27+
28+
i, _ := sort.Find(1e16, func(v int) int {
29+
monkeys["humn"] = strconv.Itoa(v)
30+
return solve(s[0]) - solve(s[2])
31+
})
32+
return i
33+
}
34+
35+
func solve(expr string) int {
36+
if v, err := strconv.Atoi(monkeys[expr]); err == nil {
37+
return v
38+
}
39+
40+
s := strings.Fields(monkeys[expr])
41+
return map[string]func(int, int) int{
42+
"+": func(a, b int) int { return a + b },
43+
"-": func(a, b int) int { return a - b },
44+
"*": func(a, b int) int { return a * b },
45+
"/": func(a, b int) int { return a / b },
46+
}[s[1]](solve(s[0]), solve(s[2]))
47+
}

‎Day21/Go/part2/part2_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part2
7+
8+
import (
9+
"os"
10+
"testing"
11+
)
12+
13+
func TestPart2(t *testing.T) {
14+
testInput, err := os.ReadFile("../testinput.txt")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
type args struct {
20+
input string
21+
}
22+
tests := []struct {
23+
name string
24+
args args
25+
want int
26+
}{
27+
{
28+
name: "Provided input",
29+
args: args{
30+
input: string(testInput),
31+
},
32+
want: 301,
33+
},
34+
}
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
if got := Part2(tt.args.input); got != tt.want {
38+
t.Errorf("Part2() = %v, want %v", got, tt.want)
39+
}
40+
})
41+
}
42+
}
43+
44+
func BenchmarkPart2(b *testing.B) {
45+
testInput, err := os.ReadFile("../input.txt")
46+
if err != nil {
47+
b.Fatal(err)
48+
}
49+
b.ResetTimer()
50+
for i := 0; i < b.N; i++ {
51+
Part2(string(testInput))
52+
}
53+
}

‎Day21/Go/testinput.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../testinput.txt

‎Day21/PowerShell/testinput.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../testinput.txt

‎Day21/testinput.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root: pppw + sjmn
2+
dbpl: 5
3+
cczh: sllz + lgvd
4+
zczc: 2
5+
ptdq: humn - dvpt
6+
dvpt: 3
7+
lfqf: 4
8+
humn: 5
9+
ljgn: 2
10+
sjmn: drzm * dbpl
11+
sllz: 4
12+
pppw: cczh / lfqf
13+
lgvd: ljgn * ptdq
14+
drzm: hmdt - zczc
15+
hmdt: 32

‎README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# Advent of Code 2022
22

3-
![day](https://img.shields.io/badge/day%20📅-12-blue)
4-
![stars](https://img.shields.io/badge/stars%20⭐-24x-yellow)
3+
![day](https://img.shields.io/badge/day%20📅-21-blue)
4+
![stars](https://img.shields.io/badge/stars%20⭐-36x-yellow)
55

66
[Advent of Code](https://adventofcode.com)
77

88
## Local Leaderboard JPMC
99

1010
|Order|Name|Stars|Local Score|
1111
|--:|:--|--:|--:|
12-
|1|andrewpickett|24|1448|
13-
|2|dratcliff|24|1433|
14-
|3||24|1391|
15-
|4|gschaefer3|24|1381|
16-
|5|Jacob Logsdon|24|1305|
17-
|_6_|_dpajkovic_|_24_|_1276_|
18-
|7|Paul Bender|21|1143|
19-
|8|cmbeam|24|1131|
20-
|9|Krzysztof Malinowski|24|1118|
21-
|10|evgeniya-sotirova|24|1106|
12+
|1|andrewpickett|42|2491|
13+
|2|gschaefer3|42|2439|
14+
|3|dratcliff|40|2334|
15+
|_4_|_dpajkovic_|_36_|_1930_|
16+
|5|Igor Dvoeglazov|36|1631|
17+
|6|cmbeam|32|1573|
18+
|7|evgeniya-sotirova|32|1561|
19+
|8|David Hanley|30|1540|
20+
|9|JKerwood|29|1378|
21+
|10|Mihajlo|29|1337|
2222

0 commit comments

Comments
 (0)
Please sign in to comment.