Skip to content

Commit

Permalink
fix path renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonmowry committed Dec 3, 2023
1 parent 103a01d commit 89527c8
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions 2023/02/jacksonmowry.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module main

import os
import math
import arrays

const red_limit = 12
const green_limit = 13
const blue_limit = 14

fn main() {
games := os.read_lines('cube.input')!
total := part_1(games)
min := part_2(games) or { 0 }
println('Part 1 Total: ${total}')
println('Part 2 Total: ${min}')
}

fn part_2(input []string) !int {
return arrays.sum[int](input.map(it.split(': ')[1]).map(it.split_any(',;').map(it.trim_space().split(' '))).map(it.power()))
}

fn (s [][]string) power() int {
mut arr := [1, 1, 1]
for hand in s {
num := hand[0].int()
arr[hand[1][0] % 3] = math.max(num, arr[hand[1][0] % 3])
}
return arrays.reduce(arr, fn (t1 int, t2 int) int {
return t1 * t2
}) or {}
}

fn part_1(input []string) int {
mut total := 0

outer: for game in input {
game_num, turns := game.split_once(': ') or { panic('no delim found') }

for turn in turns.split(';') {
for selection in turn.split(',') {
hand := selection.trim_space().split(' ')
match hand[1] {
'red' {
if hand[0].int() > red_limit {
continue outer
}
}
'green' {
if hand[0].int() > green_limit {
continue outer
}
}
'blue' {
if hand[0].int() > blue_limit {
continue outer
}
}
else {}
}
}
}
total += game_num.split(' ').last().int()
}
return total
}

0 comments on commit 89527c8

Please sign in to comment.