Skip to content

Commit

Permalink
Merge branch 'vlang:main' into day2
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonmowry authored Dec 3, 2023
2 parents a9eebe4 + aaac2cf commit 103a01d
Show file tree
Hide file tree
Showing 129 changed files with 278 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ jobs:
with:
path: aoc

- name: Install dependencies
run: v install pcre

- name: Run verify.v
run: cd aoc && v install pcre && v run verify.v
run: cd aoc && v run verify.v

- name: Format
run: v fmt . -c
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.
56 changes: 56 additions & 0 deletions 2023/01/JalonSolov.v
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)}')
58 changes: 58 additions & 0 deletions 2023/01/leodev.v
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
}
4 changes: 4 additions & 0 deletions 2023/01/trebuchet-part1.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
7 changes: 7 additions & 0 deletions 2023/01/trebuchet-part2.input
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.
5 changes: 5 additions & 0 deletions 2023/02/cube.input
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
97 changes: 97 additions & 0 deletions 2023/02/leodev.v
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 removed 2023/9/.gitkeep
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.
2 changes: 2 additions & 0 deletions known_outputs/2023/01/JalonSolov.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Part 1: 142
Part 2: 281
2 changes: 2 additions & 0 deletions known_outputs/2023/01/leodev.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
142
281
2 changes: 2 additions & 0 deletions known_outputs/2023/02/leodev.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
8
2286
44 changes: 38 additions & 6 deletions verify.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,30 @@ fn v_file2relative_out_file(v_file string) string {
return v_file2out_file(v_file).replace(wd + '/', '')
}

fn vout(v_file string, output string) !string {
fn vout(v_file string, output string) !(string, bool) {
out_file := v_file2out_file(v_file)
if !os.exists(out_file) {
eprintln('> .out file for ${v_file} does not exist, creating it based on the current run output...')
os.mkdir_all(os.dir(out_file))!
os.write_file(out_file, output)!
return output, true
}
return os.read_file(out_file)!
return os.read_file(out_file)!, false
}

fn main() {
fn discover_files() ![]string {
glob_pattern := '*' + os.args[1] or { '' } + '*'
if glob_pattern == '**' {
if os.getenv('CI') != '' {
git_diff_cmd := 'git --no-pager diff --name-only origin'
println(git_diff_cmd)
changes := os.execute(git_diff_cmd).output.split_into_lines()
files := changes.filter(it.ends_with('.v') && it.starts_with('20'))
println('running only a subset of all tests, based on the git diff for the new/changed solutions, compared to the main origin branch: ${files}')
return files
}
println('Note: you can also `v run verify.v PATTERN`, where PATTERN can be any part of the .v filepath, like: `v run verify.v 2022` or `v run verify.v Jalon` etc.')
}

mut v_files := []string{}
for folder in 2015 .. 2050 {
unfiltered_files := os.walk_ext(folder.str(), '.v')
Expand All @@ -74,17 +83,29 @@ fn main() {
v_files << v_file
}
}

return v_files
}

fn main() {
mut v_files := discover_files()!
v_files.sort_with_compare(fn (a &string, b &string) int {
xa := a.split('/').map(if it.len == 1 { '0${it}' } else { it }).join('/')
xb := b.split('/').map(if it.len == 1 { '0${it}' } else { it }).join('/')
return int(xa < xb)
})

mut erroring_files := []string{}
mut new_files := []string{}
mut total_compilation_time := time.Duration(0)
mut total_running_time := time.Duration(0)
for idx, v_file in v_files {
os.chdir(wd)!
if !os.exists(v_file) {
// in the case of a CI diff, the file may have been deleted
eprintln('> skipping missing file ${v_file}')
continue
}
vdir := os.dir(v_file)
os.chdir(vdir)!

Expand All @@ -104,14 +125,18 @@ fn main() {
println(' took ${term.green(ctook)} to compile, and ${term.bright_green(rtook)} to run.')
flush_stdout()

known := vout(v_file, output)!
known, is_new := vout(v_file, output)!
if is_new {
eprintln('> .out file for ${v_file} does not exist, creating it based on the current run output...')
new_files << v_file
}
if output != known {
eprintln('current output does not match the known one:')
eprintln('v_file: ${v_file}')
eprintln('current output:\n${output}')
eprintln(' known output:\n${known}')
eprintln(diff.color_compare_strings(diff_cmd, rand.ulid(), output, known))
exit(1)
erroring_files << v_file
}
}
ctook := '${total_compilation_time.milliseconds():6} ms'
Expand All @@ -125,4 +150,11 @@ fn main() {
}
exit(1)
}
if new_files.len > 0 && os.getenv('CI') != '' {
eprintln('Detected ${new_files.len} missing output files, you should run "v run verify.v" to generate output files')
for n in new_files {
eprintln(' v run verify.v ${n}')
}
exit(2)
}
}

0 comments on commit 103a01d

Please sign in to comment.