Skip to content

Commit 0294e37

Browse files
authored
drakeerv, day 5 and fix for day 1 (#60)
1 parent e9466ea commit 0294e37

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

2024/01/drakeerv.v

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import strconv
32
import arrays
43

54
fn main() {
@@ -11,8 +10,8 @@ fn main() {
1110
for i in 0 .. lines.len {
1211
numbers := lines[i].split(' ')
1312

14-
first_numbers << int(strconv.parse_uint(numbers[0], 10, 32)!)
15-
second_numbers << int(strconv.parse_uint(numbers[1], 10, 32)!)
13+
first_numbers << numbers[0].int()
14+
second_numbers << numbers[1].int()
1615
}
1716

1817
first_numbers.sort()

2024/05/drakeerv.v

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import arrays
3+
4+
fn create_inverse_map[T](arr []T) map[T]int {
5+
mut result := map[T]int{}
6+
for i, x in arr {
7+
result[x] = i
8+
}
9+
return result
10+
}
11+
12+
fn main() {
13+
data := os.read_file('pages.input')!.split_into_lines()
14+
split_index := arrays.index_of_first(data, fn (idx int, x string) bool {
15+
return x == ''
16+
})
17+
18+
mut rules := [][2]int{}
19+
for rule in data[0..split_index] {
20+
split := rule.split('|')
21+
rules << [split[0].int(), split[1].int()]!
22+
}
23+
24+
mut total1 := 0
25+
mut incorect_updates := [][]int{}
26+
for update in data[split_index + 1..] {
27+
pages := update.split(',').map(it.int())
28+
inverse_map := create_inverse_map(pages)
29+
30+
mut all_passed := true
31+
for page in pages {
32+
concerned_rules := rules.filter(it[0] == page || it[1] == page)
33+
passed := concerned_rules.all(fn [page, inverse_map] (rule [2]int) bool {
34+
x := rule[0]
35+
y := rule[1]
36+
if x == page && y in inverse_map {
37+
return inverse_map[x] < inverse_map[y]
38+
} else if y == page && x in inverse_map {
39+
return inverse_map[x] < inverse_map[y]
40+
}
41+
return true
42+
})
43+
44+
if !passed {
45+
all_passed = false
46+
break
47+
}
48+
}
49+
50+
if all_passed {
51+
total1 += pages[pages.len / 2]
52+
} else {
53+
incorect_updates << pages
54+
}
55+
}
56+
57+
println('part1: ${total1}')
58+
59+
mut total2 := 0
60+
for update in incorect_updates {
61+
sorted_update := update.sorted_with_compare(fn [rules] (a &int, b &int) int {
62+
a_concerned_rules := rules.filter(it[0] == a || it[1] == a)
63+
b_concerned_rules := rules.filter(it[0] == b || it[1] == b)
64+
65+
mut common_rules := [][2]int{}
66+
for rule in a_concerned_rules {
67+
if rule in b_concerned_rules {
68+
common_rules << rule
69+
}
70+
}
71+
72+
if common_rules.len == 0 {
73+
return 0
74+
}
75+
76+
common_rule := common_rules[0]
77+
if common_rule[0] == a {
78+
return -1
79+
} else {
80+
return 1
81+
}
82+
})
83+
84+
total2 += sorted_update[sorted_update.len / 2]
85+
}
86+
87+
println('part2: ${total2}')
88+
}

known/2024/05/drakeerv.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
part1: 143
2+
part2: 123

0 commit comments

Comments
 (0)