Skip to content

Commit e8c0fa6

Browse files
committed
add day 5 & remove input data
1 parent 3cde2d5 commit e8c0fa6

File tree

7 files changed

+85
-2147
lines changed

7 files changed

+85
-2147
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
data/*.big.txt
1+
data/*.txt
22
.DS_Store

d5.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"sort"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
func (*methods) D5P1(input string) string {
10+
parts := strings.Split(input, "\n\n")
11+
12+
rlines := strings.Split(parts[0], "\n")
13+
rules := make(map[string]bool)
14+
for _, rl := range rlines {
15+
rules[rl] = true
16+
}
17+
18+
updates := strings.Split(parts[1], "\n")
19+
var sum int
20+
for _, u := range updates {
21+
upd := strings.Split(u, ",")
22+
sort.Slice(upd, func(i, j int) bool {
23+
a := upd[i]
24+
b := upd[j]
25+
_, ok := rules[a+"|"+b]
26+
if ok {
27+
return true
28+
}
29+
_, ok = rules[b+"|"+a]
30+
if ok {
31+
return false
32+
}
33+
return false
34+
})
35+
u2 := strings.Join(upd, ",")
36+
if u != u2 {
37+
continue
38+
}
39+
n, _ := strconv.Atoi(upd[len(upd)/2])
40+
sum += n
41+
}
42+
return strconv.Itoa(sum)
43+
}
44+
45+
func (*methods) D5P2(input string) string {
46+
parts := strings.Split(input, "\n\n")
47+
48+
rlines := strings.Split(parts[0], "\n")
49+
rules := make(map[string]bool)
50+
for _, rl := range rlines {
51+
rules[rl] = true
52+
}
53+
54+
updates := strings.Split(parts[1], "\n")
55+
var sum int
56+
for _, u := range updates {
57+
upd := strings.Split(u, ",")
58+
sort.Slice(upd, func(i, j int) bool {
59+
a := upd[i]
60+
b := upd[j]
61+
_, ok := rules[a+"|"+b]
62+
if ok {
63+
return true
64+
}
65+
_, ok = rules[b+"|"+a]
66+
if ok {
67+
return false
68+
}
69+
return false
70+
})
71+
u2 := strings.Join(upd, ",")
72+
if u == u2 {
73+
continue
74+
}
75+
n, _ := strconv.Atoi(upd[len(upd)/2])
76+
sum += n
77+
}
78+
return strconv.Itoa(sum)
79+
}

data/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The input for the puzzles goes here.
2+
3+
The files should be named `d%d.txt`, where `%d` is the day number starting with 1.
4+
5+
Thus, the input for the day 3 puzzle would be in `data/d3.txt`.

0 commit comments

Comments
 (0)