Skip to content

Commit

Permalink
brayevalerien Solutions to days 1→5 & days 8→12 (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
brayevalerien authored Nov 30, 2024
1 parent 49b8765 commit b026ad9
Show file tree
Hide file tree
Showing 32 changed files with 1,726 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 2023/01/brayevalerien/part1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module main

import os

fn main() {
input_path := '../trebuchet-part1.input'

lines := os.read_lines(input_path) or { panic('Could not read input file.') }
mut sum := 0 // will contain the final result
for line in lines {
line_nums := get_line_nums(line)
sum += get_line_number(line_nums)
}
println('Final result: ${sum}')
}

// Given a string, returns the array of all numbers in this string
fn get_line_nums(line string) []rune {
nums := []rune{len: 10, init: index.str()[0]}
mut line_nums := []rune{}
for c in line {
if rune(c) in nums {
line_nums << rune(c)
}
}
return line_nums
}

// Given a array of digits as runes, returns the 2-digits number formed by
// the first digit in the array, concatenated with the last digit in the array.
fn get_line_number(nums []rune) int {
// runes cannot be concatenated easily. So first cast to string, concatenate and cast to int.
mut res := rune(nums[0]).str()
res += rune(nums[nums.len - 1]).str()
return res.int()
}
76 changes: 76 additions & 0 deletions 2023/01/brayevalerien/part2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module main

import os

fn main() {
input_path := '../trebuchet-part2.input'

lines := os.read_lines(input_path) or { panic('Could not read input file.') }
mut sum := 0 // will contain the final result
for line in lines {
line_nums := get_line_nums(line)
sum += get_line_number(line_nums)
}
println('Final result: ${sum}')
}

fn index(a []string, s string) int {
mut i := 0
for sp in a {
if sp == s {
return i
} else {
i += 1
}
}
return -1
}

// Given a string, returns the array of all numbers in this string
// BUT! numbers can be spelled out with letters as well.
// e.g. 'xtwone3four' -> [`2`, `1`, `3`, `4`]
fn get_line_nums(line string) []rune {
nums := []rune{len: 10, init: index.str()[0]}
mut line_nums := []rune{}
for i in 0 .. line.len {
if rune(line[i]) in nums {
line_nums << rune(line[i])
} else {
spelled_num := get_spelled_number(line[i..])
if spelled_num != -1 {
line_nums << spelled_num.str()[0]
}
}
}
return line_nums
}

// Given a string, returns the number that s starts with, or -1 if s does not start with a number
// e.g. 'xtwone3four' -> -1
// 'twone3four' -> `2`
// '3four' -> -1
fn get_spelled_number(s string) int {
spelled_nums := ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
min_len := 3 // minimum length of a spelled out number
max_len := 5 // maximum length of a spelled out number
for num_len in min_len .. (max_len + 1) {
if num_len <= s.len {
index_in_spelled_nums := index(spelled_nums, s[..num_len])
if index_in_spelled_nums != -1 {
return index_in_spelled_nums
}
} else {
break
}
}
return -1
}

// Given a array of digits as runes, returns the 2-digits number formed by
// the first digit in the array, concatenated with the last digit in the array.
fn get_line_number(nums []rune) int {
// runes cannot be concatenated easily. So first cast to string, concatenate and cast to int.
mut res := rune(nums[0]).str()
res += rune(nums[nums.len - 1]).str()
return res.int()
}
67 changes: 67 additions & 0 deletions 2023/02/brayevalerien/part1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module main

import math
import os

struct Game {
id int // game ID
red int // maximum number of red that appeared in the game
green int // maximum number of green that appeared in the game
blue int // maximum number of blue that appeared in the game
}

// Constructs a Game from a line of the input file
fn Game.new(s string) Game {
split := s.rsplit(': ')
id := split[1][5..]
record := split[0].rsplit('; ')
mut red := 0
mut green := 0
mut blue := 0
for set in record {
mut count_red := 0
mut count_green := 0
mut count_blue := 0
counts := set.rsplit(', ')
for count in counts {
if count.ends_with(' red') {
count_red += count.split(' red')[0].int()
} else if count.ends_with(' green') {
count_green += count.split(' green')[0].int()
} else if count.ends_with(' blue') {
count_blue += count.split(' blue')[0].int()
}
}
red = math.max(red, count_red)
green = math.max(green, count_green)
blue = math.max(blue, count_blue)
}
return Game{
id: id.int()
red: red
green: green
blue: blue
}
}

fn is_valid(game Game) bool {
// maximum number of cubes of each color in the bag
max_red := 12
max_green := 13
max_blue := 14
return game.red <= max_red && game.green <= max_green && game.blue <= max_blue
}

fn main() {
input_path := '../cube.input'

lines := os.read_lines(input_path) or { panic('Could not read input file.') }
mut result := 0
for line in lines {
game := Game.new(line)
if is_valid(game) {
result += game.id
}
}
println('Final result: ${result}')
}
64 changes: 64 additions & 0 deletions 2023/02/brayevalerien/part2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module main

import math
import os

struct Game {
id int // game ID
red int // maximum number of red that appeared in the game
green int // maximum number of green that appeared in the game
blue int // maximum number of blue that appeared in the game
}

// Constructs a Game from a line of the input file
fn Game.new(s string) Game {
split := s.rsplit(': ')
id := split[1][5..]
record := split[0].rsplit('; ')
mut red := 0
mut green := 0
mut blue := 0
for set in record {
mut count_red := 0
mut count_green := 0
mut count_blue := 0
counts := set.rsplit(', ')
for count in counts {
if count.ends_with(' red') {
count_red += count.split(' red')[0].int()
} else if count.ends_with(' green') {
count_green += count.split(' green')[0].int()
} else if count.ends_with(' blue') {
count_blue += count.split(' blue')[0].int()
}
}
red = math.max(red, count_red)
green = math.max(green, count_green)
blue = math.max(blue, count_blue)
}
return Game{
id: id.int()
red: red
green: green
blue: blue
}
}

// Return the power of a set of cubes of a game.
// The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together.
fn set_power(game Game) int {
// maximum number of cubes of each color in the bag
return game.red * game.green * game.blue
}

fn main() {
input_path := '../cube.input'

lines := os.read_lines(input_path) or { panic('Could not read input file.') }
mut result := 0
for line in lines {
game := Game.new(line)
result += set_power(game)
}
println('Final result: ${result}')
}
Loading

0 comments on commit b026ad9

Please sign in to comment.