Skip to content

Commit 9822d6c

Browse files
authored
add hillerstorm day 1 through 4 (#30)
1 parent ddbf5c8 commit 9822d6c

File tree

9 files changed

+242
-0
lines changed

9 files changed

+242
-0
lines changed

2023/01/hillerstorm.v

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module main
2+
3+
import arrays
4+
import os
5+
6+
const numbers = [
7+
'0',
8+
'1',
9+
'2',
10+
'3',
11+
'4',
12+
'5',
13+
'6',
14+
'7',
15+
'8',
16+
'9',
17+
'zero',
18+
'one',
19+
'two',
20+
'three',
21+
'four',
22+
'five',
23+
'six',
24+
'seven',
25+
'eight',
26+
'nine',
27+
]
28+
29+
struct Match {
30+
value string
31+
idx int
32+
}
33+
34+
fn main() {
35+
part_one := arrays.sum(os.read_lines('trebuchet-part1.input')!
36+
.map(it.split('').filter(it in numbers[..10]))
37+
.filter(it.len > 0)
38+
.map((it.first() + it.last()).int()))!
39+
40+
part_two := arrays.sum(os.read_lines('trebuchet-part2.input')!
41+
.map(fn (line string) int {
42+
mut first_indices := numbers
43+
.filter(line.index(it) or { -1 } > -1)
44+
.map(Match{it, line.index(it) or { -1 }})
45+
first_indices.sort(a.idx < b.idx)
46+
first_num := numbers[numbers.index(first_indices.first().value) % 10]
47+
48+
mut last_indices := numbers
49+
.filter(line.index(it) or { -1 } > -1)
50+
.map(Match{it, line.last_index(it) or { -1 }})
51+
last_indices.sort(a.idx < b.idx)
52+
last_num := numbers[numbers.index(last_indices.last().value) % 10]
53+
54+
return (first_num + last_num).int()
55+
}))!
56+
57+
println('Part 1: ${part_one}')
58+
println('Part 2: ${part_two}')
59+
}

2023/02/hillerstorm.v

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module main
2+
3+
import math
4+
import os
5+
6+
const bag = {
7+
'red': 12
8+
'green': 13
9+
'blue': 14
10+
}
11+
12+
fn main() {
13+
mut part_one := 0
14+
mut part_two := 0
15+
16+
for line in os.read_lines('cube.input')! {
17+
parts := line.split(': ')
18+
19+
mut valid := true
20+
mut min_red := 0
21+
mut min_green := 0
22+
mut min_blue := 0
23+
24+
for set in parts[1].split('; ') {
25+
for cube in set.split(', ') {
26+
cube_parts := cube.split(' ')
27+
color := cube_parts[1]
28+
count := cube_parts[0].int()
29+
30+
if color == 'red' {
31+
min_red = math.max(min_red, count)
32+
} else if color == 'green' {
33+
min_green = math.max(min_green, count)
34+
} else if color == 'blue' {
35+
min_blue = math.max(min_blue, count)
36+
}
37+
38+
if bag[color] < count {
39+
valid = false
40+
}
41+
}
42+
}
43+
44+
if valid {
45+
part_one += parts[0].split(' ')[1].int()
46+
}
47+
48+
part_two += min_red * min_green * min_blue
49+
}
50+
51+
println('Part 1: ${part_one}')
52+
println('Part 2: ${part_two}')
53+
}

2023/03/hillerstorm.v

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module main
2+
3+
import arrays
4+
import os
5+
6+
const digits = '0123456789'.runes()
7+
8+
fn main() {
9+
lines := os.read_lines('schematic.input')!
10+
11+
width := lines[0].len + 2
12+
deltas := [
13+
-width - 1,
14+
-width,
15+
-width + 1,
16+
-1,
17+
1,
18+
width - 1,
19+
width,
20+
width + 1,
21+
]
22+
23+
extra_row := '.'.repeat(width)
24+
mut padded := [extra_row]
25+
padded << lines.map('.${it}.')
26+
padded << extra_row
27+
28+
grid := arrays.flatten(padded.map(it.runes()))
29+
30+
mut part_one := 0
31+
32+
mut digit := ''
33+
mut digit_indices := []int{}
34+
mut gears := map[int][]int{}
35+
for idx, chr in grid {
36+
if chr in digits {
37+
digit += chr.str()
38+
digit_indices << idx
39+
continue
40+
}
41+
42+
if digit.len > 0 {
43+
digit_value := digit.int()
44+
if is_part(digit_indices, digit_value, grid, deltas, mut gears) {
45+
part_one += digit_value
46+
}
47+
}
48+
49+
digit = ''
50+
digit_indices = []
51+
}
52+
53+
part_two := arrays.sum(gears
54+
.keys()
55+
.filter(gears[it].len == 2)
56+
.map(arrays.reduce(gears[it], fn (a int, b int) int {
57+
return a * b
58+
})!))!
59+
60+
println('Part 1: ${part_one}')
61+
println('Part 2: ${part_two}')
62+
}
63+
64+
fn is_part(digit_indices []int, digit_value int, grid []rune, deltas []int, mut gears map[int][]int) bool {
65+
neighbours := digit_indices.map(fn [deltas] (i int) []int {
66+
return deltas.map(i + it)
67+
})
68+
69+
mut result := false
70+
for idx in arrays.map_of_indexes[int](arrays.flatten(neighbours)).keys() {
71+
chr := grid[idx]
72+
if chr == `.` || chr in digits {
73+
continue
74+
} else if chr == `*` {
75+
gears[idx] << digit_value
76+
}
77+
result = true
78+
}
79+
80+
return result
81+
}

2023/04/hillerstorm.v

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module main
2+
3+
import arrays
4+
import math
5+
import os
6+
7+
fn main() {
8+
mut part_one := i64(0)
9+
10+
mut cards := map[int]int{}
11+
mut max_real_id := 0
12+
13+
for line in os.read_lines('scratchcards.input')! {
14+
parts := line.split(': ')
15+
numbers := parts[1].split(' | ')
16+
winning_numbers := numbers[0].split(' ').filter(it.len > 0).map(it.int())
17+
own_numbers := numbers[1].split(' ').filter(it.len > 0).map(it.int())
18+
matching_numbers := own_numbers.filter(it in winning_numbers).len
19+
20+
card := parts[0].split(' ').last().int()
21+
cards[card] += 1
22+
max_real_id = math.max(max_real_id, card)
23+
24+
for id in []int{len: matching_numbers, init: card + index + 1} {
25+
cards[id] += cards[card]
26+
}
27+
28+
part_one += math.powi(2, matching_numbers) / 2
29+
}
30+
31+
part_two := arrays.sum(cards.keys().filter(it <= max_real_id).map(cards[it]))!
32+
33+
println('Part 1: ${part_one}')
34+
println('Part 2: ${part_two}')
35+
}

2023/04/scratchcards.input

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
2+
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
3+
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
4+
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
5+
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
6+
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11

known_outputs/2023/01/hillerstorm.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Part 1: 142
2+
Part 2: 281

known_outputs/2023/02/hillerstorm.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Part 1: 8
2+
Part 2: 2286

known_outputs/2023/03/hillerstorm.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Part 1: 4361
2+
Part 2: 467835

known_outputs/2023/04/hillerstorm.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Part 1: 13
2+
Part 2: 30

0 commit comments

Comments
 (0)