Skip to content

Commit 2dd30b1

Browse files
Day 7, Part 1, Part 2 (#37)
Co-authored-by: JingerJack <[email protected]>
1 parent 4f2baf4 commit 2dd30b1

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

2023/07/camel_cards.input

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
32T3K 765
2+
T55J5 684
3+
KK677 28
4+
KTJJT 220
5+
QQQJA 483

2023/07/jacksonmowry.v

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
module main
2+
3+
import os
4+
5+
const card_to_val = {
6+
`A`: 13
7+
`K`: 12
8+
`Q`: 11
9+
`J`: 10
10+
`T`: 9
11+
`9`: 8
12+
`8`: 7
13+
`7`: 6
14+
`6`: 5
15+
`5`: 4
16+
`4`: 3
17+
`3`: 2
18+
`2`: 1
19+
}
20+
21+
const card_to_val_2 = {
22+
`A`: 13
23+
`K`: 12
24+
`Q`: 11
25+
`T`: 10
26+
`9`: 9
27+
`8`: 8
28+
`7`: 7
29+
`6`: 6
30+
`5`: 5
31+
`4`: 4
32+
`3`: 3
33+
`2`: 2
34+
`J`: 1
35+
}
36+
37+
fn main() {
38+
filename := 'camel_cards.input'
39+
println('Part 1: ${solver(filename, false)!}')
40+
println('Part 2: ${solver(filename, true)!}')
41+
}
42+
43+
fn solver(filename string, part_2 bool) !int {
44+
mut lines := os.read_lines(filename)!.map(it.split(' '))
45+
for i, hand in lines {
46+
mut counts := map[u8]int{}
47+
for card in hand[0] {
48+
counts[card]++
49+
}
50+
mut freq := []int{}
51+
mut j_count := 0
52+
for key, val in counts {
53+
if part_2 && key == `J` {
54+
j_count += val
55+
} else {
56+
freq << val
57+
}
58+
}
59+
if j_count == 5 {
60+
lines[i] << 7.str()
61+
continue
62+
}
63+
freq.sort(b < a)
64+
if part_2 {
65+
freq[0] += j_count
66+
}
67+
lines[i] << score_hand(freq)
68+
}
69+
if part_2 {
70+
lines.sort_with_compare(fn (mut a []string, mut b []string) int {
71+
if a[2] < b[2] {
72+
return -1
73+
} else if a[2] > b[2] {
74+
return 1
75+
}
76+
for i := 0; i < 5; i++ {
77+
if card_to_val_2[a[0][i]] == card_to_val_2[b[0][i]] {
78+
continue
79+
} else if card_to_val_2[a[0][i]] < card_to_val_2[b[0][i]] {
80+
return -1
81+
} else {
82+
return 1
83+
}
84+
}
85+
return 0
86+
})
87+
} else {
88+
lines.sort_with_compare(fn (mut a []string, mut b []string) int {
89+
if a[2] < b[2] {
90+
return -1
91+
} else if a[2] > b[2] {
92+
return 1
93+
}
94+
for i := 0; i < 5; i++ {
95+
if card_to_val[a[0][i]] == card_to_val[b[0][i]] {
96+
continue
97+
} else if card_to_val[a[0][i]] < card_to_val[b[0][i]] {
98+
return -1
99+
} else {
100+
return 1
101+
}
102+
}
103+
return 0
104+
})
105+
}
106+
mut sum := 0
107+
for i := 0; i < lines.len; i++ {
108+
sum += lines[i][1].int() * (i + 1)
109+
}
110+
return sum
111+
}
112+
113+
fn score_hand(hand_counts []int) string {
114+
return match hand_counts[0] {
115+
5 {
116+
7.str()
117+
}
118+
4 {
119+
6.str()
120+
}
121+
3 {
122+
if hand_counts[1] == 2 {
123+
5.str()
124+
} else {
125+
4.str()
126+
}
127+
}
128+
2 {
129+
if hand_counts[1] == 2 {
130+
3.str()
131+
} else {
132+
2.str()
133+
}
134+
}
135+
1 {
136+
1.str()
137+
}
138+
else {
139+
panic('invalid card count')
140+
}
141+
}
142+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Part 1: 6440
2+
Part 2: 5905

0 commit comments

Comments
 (0)