Skip to content

Commit b026ad9

Browse files
brayevalerien Solutions to days 1→5 & days 8→12 (#48)
1 parent 49b8765 commit b026ad9

File tree

32 files changed

+1726
-0
lines changed

32 files changed

+1726
-0
lines changed

2023/01/brayevalerien/part1.v

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module main
2+
3+
import os
4+
5+
fn main() {
6+
input_path := '../trebuchet-part1.input'
7+
8+
lines := os.read_lines(input_path) or { panic('Could not read input file.') }
9+
mut sum := 0 // will contain the final result
10+
for line in lines {
11+
line_nums := get_line_nums(line)
12+
sum += get_line_number(line_nums)
13+
}
14+
println('Final result: ${sum}')
15+
}
16+
17+
// Given a string, returns the array of all numbers in this string
18+
fn get_line_nums(line string) []rune {
19+
nums := []rune{len: 10, init: index.str()[0]}
20+
mut line_nums := []rune{}
21+
for c in line {
22+
if rune(c) in nums {
23+
line_nums << rune(c)
24+
}
25+
}
26+
return line_nums
27+
}
28+
29+
// Given a array of digits as runes, returns the 2-digits number formed by
30+
// the first digit in the array, concatenated with the last digit in the array.
31+
fn get_line_number(nums []rune) int {
32+
// runes cannot be concatenated easily. So first cast to string, concatenate and cast to int.
33+
mut res := rune(nums[0]).str()
34+
res += rune(nums[nums.len - 1]).str()
35+
return res.int()
36+
}

2023/01/brayevalerien/part2.v

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
module main
2+
3+
import os
4+
5+
fn main() {
6+
input_path := '../trebuchet-part2.input'
7+
8+
lines := os.read_lines(input_path) or { panic('Could not read input file.') }
9+
mut sum := 0 // will contain the final result
10+
for line in lines {
11+
line_nums := get_line_nums(line)
12+
sum += get_line_number(line_nums)
13+
}
14+
println('Final result: ${sum}')
15+
}
16+
17+
fn index(a []string, s string) int {
18+
mut i := 0
19+
for sp in a {
20+
if sp == s {
21+
return i
22+
} else {
23+
i += 1
24+
}
25+
}
26+
return -1
27+
}
28+
29+
// Given a string, returns the array of all numbers in this string
30+
// BUT! numbers can be spelled out with letters as well.
31+
// e.g. 'xtwone3four' -> [`2`, `1`, `3`, `4`]
32+
fn get_line_nums(line string) []rune {
33+
nums := []rune{len: 10, init: index.str()[0]}
34+
mut line_nums := []rune{}
35+
for i in 0 .. line.len {
36+
if rune(line[i]) in nums {
37+
line_nums << rune(line[i])
38+
} else {
39+
spelled_num := get_spelled_number(line[i..])
40+
if spelled_num != -1 {
41+
line_nums << spelled_num.str()[0]
42+
}
43+
}
44+
}
45+
return line_nums
46+
}
47+
48+
// Given a string, returns the number that s starts with, or -1 if s does not start with a number
49+
// e.g. 'xtwone3four' -> -1
50+
// 'twone3four' -> `2`
51+
// '3four' -> -1
52+
fn get_spelled_number(s string) int {
53+
spelled_nums := ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
54+
min_len := 3 // minimum length of a spelled out number
55+
max_len := 5 // maximum length of a spelled out number
56+
for num_len in min_len .. (max_len + 1) {
57+
if num_len <= s.len {
58+
index_in_spelled_nums := index(spelled_nums, s[..num_len])
59+
if index_in_spelled_nums != -1 {
60+
return index_in_spelled_nums
61+
}
62+
} else {
63+
break
64+
}
65+
}
66+
return -1
67+
}
68+
69+
// Given a array of digits as runes, returns the 2-digits number formed by
70+
// the first digit in the array, concatenated with the last digit in the array.
71+
fn get_line_number(nums []rune) int {
72+
// runes cannot be concatenated easily. So first cast to string, concatenate and cast to int.
73+
mut res := rune(nums[0]).str()
74+
res += rune(nums[nums.len - 1]).str()
75+
return res.int()
76+
}

2023/02/brayevalerien/part1.v

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module main
2+
3+
import math
4+
import os
5+
6+
struct Game {
7+
id int // game ID
8+
red int // maximum number of red that appeared in the game
9+
green int // maximum number of green that appeared in the game
10+
blue int // maximum number of blue that appeared in the game
11+
}
12+
13+
// Constructs a Game from a line of the input file
14+
fn Game.new(s string) Game {
15+
split := s.rsplit(': ')
16+
id := split[1][5..]
17+
record := split[0].rsplit('; ')
18+
mut red := 0
19+
mut green := 0
20+
mut blue := 0
21+
for set in record {
22+
mut count_red := 0
23+
mut count_green := 0
24+
mut count_blue := 0
25+
counts := set.rsplit(', ')
26+
for count in counts {
27+
if count.ends_with(' red') {
28+
count_red += count.split(' red')[0].int()
29+
} else if count.ends_with(' green') {
30+
count_green += count.split(' green')[0].int()
31+
} else if count.ends_with(' blue') {
32+
count_blue += count.split(' blue')[0].int()
33+
}
34+
}
35+
red = math.max(red, count_red)
36+
green = math.max(green, count_green)
37+
blue = math.max(blue, count_blue)
38+
}
39+
return Game{
40+
id: id.int()
41+
red: red
42+
green: green
43+
blue: blue
44+
}
45+
}
46+
47+
fn is_valid(game Game) bool {
48+
// maximum number of cubes of each color in the bag
49+
max_red := 12
50+
max_green := 13
51+
max_blue := 14
52+
return game.red <= max_red && game.green <= max_green && game.blue <= max_blue
53+
}
54+
55+
fn main() {
56+
input_path := '../cube.input'
57+
58+
lines := os.read_lines(input_path) or { panic('Could not read input file.') }
59+
mut result := 0
60+
for line in lines {
61+
game := Game.new(line)
62+
if is_valid(game) {
63+
result += game.id
64+
}
65+
}
66+
println('Final result: ${result}')
67+
}

2023/02/brayevalerien/part2.v

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module main
2+
3+
import math
4+
import os
5+
6+
struct Game {
7+
id int // game ID
8+
red int // maximum number of red that appeared in the game
9+
green int // maximum number of green that appeared in the game
10+
blue int // maximum number of blue that appeared in the game
11+
}
12+
13+
// Constructs a Game from a line of the input file
14+
fn Game.new(s string) Game {
15+
split := s.rsplit(': ')
16+
id := split[1][5..]
17+
record := split[0].rsplit('; ')
18+
mut red := 0
19+
mut green := 0
20+
mut blue := 0
21+
for set in record {
22+
mut count_red := 0
23+
mut count_green := 0
24+
mut count_blue := 0
25+
counts := set.rsplit(', ')
26+
for count in counts {
27+
if count.ends_with(' red') {
28+
count_red += count.split(' red')[0].int()
29+
} else if count.ends_with(' green') {
30+
count_green += count.split(' green')[0].int()
31+
} else if count.ends_with(' blue') {
32+
count_blue += count.split(' blue')[0].int()
33+
}
34+
}
35+
red = math.max(red, count_red)
36+
green = math.max(green, count_green)
37+
blue = math.max(blue, count_blue)
38+
}
39+
return Game{
40+
id: id.int()
41+
red: red
42+
green: green
43+
blue: blue
44+
}
45+
}
46+
47+
// Return the power of a set of cubes of a game.
48+
// The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together.
49+
fn set_power(game Game) int {
50+
// maximum number of cubes of each color in the bag
51+
return game.red * game.green * game.blue
52+
}
53+
54+
fn main() {
55+
input_path := '../cube.input'
56+
57+
lines := os.read_lines(input_path) or { panic('Could not read input file.') }
58+
mut result := 0
59+
for line in lines {
60+
game := Game.new(line)
61+
result += set_power(game)
62+
}
63+
println('Final result: ${result}')
64+
}

0 commit comments

Comments
 (0)