-
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
Showing
129 changed files
with
278 additions
and
7 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,56 @@ | ||
import os | ||
|
||
const nums = { | ||
'one': `1` | ||
'two': `2` | ||
'three': `3` | ||
'four': `4` | ||
'five': `5` | ||
'six': `6` | ||
'seven': `7` | ||
'eight': `8` | ||
'nine': `9` | ||
} | ||
|
||
fn calc_calibration(lines []string, part2 bool) int { | ||
mut calibration_total := 0 | ||
|
||
for l in lines { | ||
mut first_digit := 0 | ||
mut last_digit := 0 | ||
|
||
for idx := 0; idx < l.len; idx++ { | ||
mut c := l[idx] | ||
|
||
if part2 { | ||
for num in nums.keys() { | ||
if l[idx..].starts_with(num) { | ||
c = nums[num] | ||
} | ||
} | ||
} | ||
|
||
match c { | ||
`1`...`9` { | ||
if first_digit == 0 { | ||
first_digit = c - 0x30 | ||
last_digit = first_digit | ||
} else { | ||
last_digit = c - 0x30 | ||
} | ||
} | ||
else {} | ||
} | ||
} | ||
|
||
calibration_total += first_digit * 10 + last_digit | ||
} | ||
|
||
return calibration_total | ||
} | ||
|
||
lines_part1 := os.read_lines('trebuchet-part1.input')! | ||
lines_part2 := os.read_lines('trebuchet-part2.input')! | ||
|
||
println('Part 1: ${calc_calibration(lines_part1, false)}') | ||
println('Part 2: ${calc_calibration(lines_part2, true)}') |
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,58 @@ | ||
import os { read_file } | ||
|
||
fn main() { | ||
inputs_part1 := read_file('trebuchet-part1.input')!.split_into_lines() | ||
println(part1(inputs_part1)) | ||
inputs_part2 := read_file('trebuchet-part2.input')!.split_into_lines() | ||
println(part2(inputs_part2)) | ||
} | ||
|
||
fn part1(inputs []string) int { | ||
mut sum := 0 | ||
for line in inputs { | ||
mut first := 0 | ||
mut last := 0 | ||
for c in line { | ||
if c > `0` && c <= `9` { | ||
if first == 0 { | ||
first = c - u8(`0`) | ||
} | ||
last = c - u8(`0`) | ||
} | ||
} | ||
sum += first * 10 + last | ||
} | ||
return sum | ||
} | ||
|
||
fn part2(inputs []string) int { | ||
mut sum := 0 | ||
replacers := ['1', 'one', '2', 'two', '3', 'three', '4', 'four', '5', 'five', '6', 'six', '7', | ||
'seven', '8', 'eight', '9', 'nine'] | ||
values := ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] | ||
for old_line in inputs { | ||
line := old_line.replace_each(replacers) | ||
reversed := line.reverse() | ||
|
||
mut first := 0 | ||
mut first_index := 999 | ||
mut last := 0 | ||
mut last_index := 999 | ||
for idx, value in values { | ||
if front_index := line.index(value) { | ||
if front_index < first_index { | ||
first = idx + 1 | ||
first_index = front_index | ||
} | ||
} | ||
if back_index := reversed.index(value.reverse()) { | ||
if back_index < last_index { | ||
last = idx + 1 | ||
last_index = back_index | ||
} | ||
} | ||
} | ||
sum += first * 10 + last | ||
} | ||
return sum | ||
} |
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,4 @@ | ||
1abc2 | ||
pqr3stu8vwx | ||
a1b2c3d4e5f | ||
treb7uchet |
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,7 @@ | ||
two1nine | ||
eightwothree | ||
abcone2threexyz | ||
xtwone3four | ||
4nineeightseven2 | ||
zoneight234 | ||
7pqrstsixteen |
File renamed without changes.
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,5 @@ | ||
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green | ||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue | ||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red | ||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red | ||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green |
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,97 @@ | ||
import os { read_file } | ||
|
||
const max_red = 12 | ||
const max_green = 13 | ||
const max_blue = 14 | ||
|
||
struct Game { | ||
idx int | ||
draws []Pair | ||
} | ||
|
||
struct Pair { | ||
amount int | ||
color Color | ||
} | ||
|
||
enum Color { | ||
red | ||
green | ||
blue | ||
} | ||
|
||
fn main() { | ||
inputs := read_file('cube.input')!.split_into_lines() | ||
mut games := []Game{} | ||
for line in inputs { | ||
parts := line.split(': ') | ||
game_idx := parts[0].split(' ')[1].int() | ||
mut draws := []Pair{} | ||
for draw in parts[1].split('; ') { | ||
for pair in draw.split(', ') { | ||
pair_parts := pair.split(' ') | ||
amount := pair_parts[0].int() | ||
color := match pair_parts[1] { | ||
'red' { Color.red } | ||
'green' { Color.green } | ||
'blue' { Color.blue } | ||
else { panic('unexpected color ${pair_parts[1]}') } | ||
} | ||
draws << Pair{amount, color} | ||
} | ||
} | ||
games << Game{game_idx, draws} | ||
} | ||
|
||
println(part1(games)) | ||
println(part2(games)) | ||
} | ||
|
||
fn part1(games []Game) int { | ||
mut sum := 0 | ||
game: for game in games { | ||
for draw in game.draws { | ||
max := match draw.color { | ||
.red { max_red } | ||
.green { max_green } | ||
.blue { max_blue } | ||
} | ||
if draw.amount > max { | ||
continue game | ||
} | ||
} | ||
sum += game.idx | ||
} | ||
return sum | ||
} | ||
|
||
fn part2(games []Game) int { | ||
mut sum := 0 | ||
for game in games { | ||
mut highest_red := 0 | ||
mut highest_green := 0 | ||
mut highest_blue := 0 | ||
for draw in game.draws { | ||
match draw.color { | ||
.red { | ||
if draw.amount > highest_red { | ||
highest_red = draw.amount | ||
} | ||
} | ||
.green { | ||
if draw.amount > highest_green { | ||
highest_green = draw.amount | ||
} | ||
} | ||
.blue { | ||
if draw.amount > highest_blue { | ||
highest_blue = draw.amount | ||
} | ||
} | ||
} | ||
} | ||
|
||
sum += highest_red * highest_green * highest_blue | ||
} | ||
return sum | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,2 @@ | ||
Part 1: 142 | ||
Part 2: 281 |
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,2 @@ | ||
142 | ||
281 |
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,2 @@ | ||
8 | ||
2286 |
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