-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
103a01d
commit 89527c8
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |