|
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