Skip to content

Commit ca040b9

Browse files
committed
run v fmt -w .
1 parent 6a2e150 commit ca040b9

2 files changed

Lines changed: 144 additions & 147 deletions

File tree

2025/01/kylepritchard.v

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
1-
module main
2-
3-
import os
4-
import strconv
5-
6-
fn main() {
7-
codes := os.read_file('rotations.input') or { panic('Failed to read file') }
8-
9-
mut start := 50
10-
mut count := 0
11-
mut count2 := 0
12-
13-
for line in codes.split_into_lines() {
14-
if line.len == 0 {
15-
continue
16-
}
17-
18-
if line[0] == `L` {
19-
move_str := line[1..].trim_space()
20-
mut move := strconv.atoi(move_str) or { 0 }
21-
22-
for move > 0 {
23-
start--
24-
if start == 0 {
25-
count2++
26-
}
27-
if start == -1 {
28-
start = 99
29-
}
30-
move--
31-
}
32-
33-
if start == 0 {
34-
count++
35-
}
36-
} else {
37-
move_str := line[1..].trim_space()
38-
mut move := strconv.atoi(move_str) or { 0 }
39-
40-
for move > 0 {
41-
start++
42-
if start == 100 {
43-
start = 0
44-
}
45-
if start == 0 {
46-
count2++
47-
}
48-
move--
49-
}
50-
51-
if start == 0 {
52-
count++
53-
}
54-
}
55-
}
56-
57-
println('Part 1: $count')
58-
println('Part 2: $count2')
59-
}
60-
1+
module main
2+
3+
import os
4+
import strconv
5+
6+
fn main() {
7+
codes := os.read_file('rotations.input') or { panic('Failed to read file') }
8+
9+
mut start := 50
10+
mut count := 0
11+
mut count2 := 0
12+
13+
for line in codes.split_into_lines() {
14+
if line.len == 0 {
15+
continue
16+
}
17+
18+
if line[0] == `L` {
19+
move_str := line[1..].trim_space()
20+
mut move := strconv.atoi(move_str) or { 0 }
21+
22+
for move > 0 {
23+
start--
24+
if start == 0 {
25+
count2++
26+
}
27+
if start == -1 {
28+
start = 99
29+
}
30+
move--
31+
}
32+
33+
if start == 0 {
34+
count++
35+
}
36+
} else {
37+
move_str := line[1..].trim_space()
38+
mut move := strconv.atoi(move_str) or { 0 }
39+
40+
for move > 0 {
41+
start++
42+
if start == 100 {
43+
start = 0
44+
}
45+
if start == 0 {
46+
count2++
47+
}
48+
move--
49+
}
50+
51+
if start == 0 {
52+
count++
53+
}
54+
}
55+
}
56+
57+
println('Part 1: ${count}')
58+
println('Part 2: ${count2}')
59+
}

2025/02/kylepritchard.v

Lines changed: 85 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,85 @@
1-
module main
2-
3-
import os
4-
import strconv
5-
6-
fn has_exact_two_halves(s string) bool {
7-
if s.len % 2 != 0 {
8-
return false
9-
}
10-
half := s.len / 2
11-
return s[0..half] == s[half..]
12-
}
13-
14-
fn has_repeating_pattern(s string) bool {
15-
for pattern_len in 1 .. s.len / 2 + 1 {
16-
if s.len % pattern_len != 0 {
17-
continue
18-
}
19-
20-
pattern := s[0..pattern_len]
21-
mut matches := true
22-
23-
repetitions := s.len / pattern_len
24-
for rep in 1 .. repetitions {
25-
start := rep * pattern_len
26-
end := start + pattern_len
27-
if s[start..end] != pattern {
28-
matches = false
29-
break
30-
}
31-
}
32-
33-
if matches {
34-
return true
35-
}
36-
}
37-
return false
38-
}
39-
40-
fn main() {
41-
content := os.read_file('ids.input') or { panic('Failed to read file: $err') }
42-
43-
ids := content.split(',')
44-
45-
mut sum1 := i64(0)
46-
mut sum2 := i64(0)
47-
48-
for id in ids {
49-
if id.len == 0 {
50-
continue
51-
}
52-
53-
range_parts := id.split('-')
54-
55-
start_str := range_parts[0].trim_space()
56-
end_str := range_parts[1].trim_space()
57-
58-
start := strconv.parse_int(start_str, 10, 64) or {
59-
eprintln('Start number isn\'t valid: "$id"')
60-
continue
61-
}
62-
63-
end := strconv.parse_int(end_str, 10, 64) or {
64-
eprintln('End number isn\'t valid: "$id"')
65-
continue
66-
}
67-
68-
for num in start .. end + 1 {
69-
num_str := num.str()
70-
71-
// Part 1
72-
if has_exact_two_halves(num_str) {
73-
sum1 += num
74-
}
75-
76-
// Part 2
77-
if has_repeating_pattern(num_str) {
78-
sum2 += num
79-
}
80-
}
81-
}
82-
83-
println('Part 1: $sum1')
84-
println('Part 2: $sum2')
85-
}
86-
87-
1+
module main
2+
3+
import os
4+
import strconv
5+
6+
fn has_exact_two_halves(s string) bool {
7+
if s.len % 2 != 0 {
8+
return false
9+
}
10+
half := s.len / 2
11+
return s[0..half] == s[half..]
12+
}
13+
14+
fn has_repeating_pattern(s string) bool {
15+
for pattern_len in 1 .. s.len / 2 + 1 {
16+
if s.len % pattern_len != 0 {
17+
continue
18+
}
19+
20+
pattern := s[0..pattern_len]
21+
mut matches := true
22+
23+
repetitions := s.len / pattern_len
24+
for rep in 1 .. repetitions {
25+
start := rep * pattern_len
26+
end := start + pattern_len
27+
if s[start..end] != pattern {
28+
matches = false
29+
break
30+
}
31+
}
32+
33+
if matches {
34+
return true
35+
}
36+
}
37+
return false
38+
}
39+
40+
fn main() {
41+
content := os.read_file('ids.input') or { panic('Failed to read file: ${err}') }
42+
43+
ids := content.split(',')
44+
45+
mut sum1 := i64(0)
46+
mut sum2 := i64(0)
47+
48+
for id in ids {
49+
if id.len == 0 {
50+
continue
51+
}
52+
53+
range_parts := id.split('-')
54+
55+
start_str := range_parts[0].trim_space()
56+
end_str := range_parts[1].trim_space()
57+
58+
start := strconv.parse_int(start_str, 10, 64) or {
59+
eprintln('Start number isn\'t valid: "${id}"')
60+
continue
61+
}
62+
63+
end := strconv.parse_int(end_str, 10, 64) or {
64+
eprintln('End number isn\'t valid: "${id}"')
65+
continue
66+
}
67+
68+
for num in start .. end + 1 {
69+
num_str := num.str()
70+
71+
// Part 1
72+
if has_exact_two_halves(num_str) {
73+
sum1 += num
74+
}
75+
76+
// Part 2
77+
if has_repeating_pattern(num_str) {
78+
sum2 += num
79+
}
80+
}
81+
}
82+
83+
println('Part 1: ${sum1}')
84+
println('Part 2: ${sum2}')
85+
}

0 commit comments

Comments
 (0)