Skip to content

Commit

Permalink
krotki days 1..3 (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krotki authored Dec 3, 2024
1 parent fefac4a commit 0137c6a
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 2024/01/krotki/part1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module main

import math
import os

fn main() {
lines := os.get_lines()

mut col1 := []i64{}
mut col2 := []i64{}

for l in lines {
a, b := l.split_once(' ') or { panic('Could not split a line ${l}') }
col1 << a.parse_int(10, 32)!
col2 << b.parse_int(10, 32)!
}

col1.sort()
col2.sort()

mut sum := i64(0)
for i := 0; i < col1.len; i++ {
sum += math.abs(col2[i] - col1[i])
}

println(sum)
}
25 changes: 25 additions & 0 deletions 2024/01/krotki/part2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module main

import os
import arrays

fn main() {
lines := os.get_lines()
mut col1 := []i64{}
mut col2 := []i64{}

for l in lines {
a, b := l.split_once(' ') or { panic('Could not split a line ${l}') }
col1 << a.parse_int(10, 32)!
col2 << b.parse_int(10, 32)!
}

counts := arrays.map_of_counts[i64](col2)

mut sum := i64(0)
for n in col1 {
sum += n * counts[n]
}

println(sum)
}
22 changes: 22 additions & 0 deletions 2024/02/krotki/part1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module main

import os
import arrays

fn main() {
input := os.get_lines()

mut count := 0
for line in input {
levels := line.split(' ').map(it.int())
diff := arrays.window(levels, size: 2).map(it[1] - it[0])
within_bounds := diff.all(it <= 3 && it >= -3)
positive := diff.all(it > 0)
negative := diff.all(it < 0)
if within_bounds && (positive || negative) {
count++
}
}

println(count)
}
27 changes: 27 additions & 0 deletions 2024/02/krotki/part2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module main

import os
import arrays

fn main() {
input := os.get_lines()

mut count := 0
for line in input {
levels := line.split(' ').map(it.int())
for i := 0; i < levels.len; i++ {
mut lvl := levels.clone()
lvl.delete(i)
diff := arrays.window(lvl, size: 2).map(it[1] - it[0])
within_bounds := diff.all(it <= 3 && it >= -3)
positive := diff.all(it > 0)
negative := diff.all(it < 0)
if within_bounds && (positive || negative) {
count++
break
}
}
}

println(count)
}
19 changes: 19 additions & 0 deletions 2024/03/krotki/part1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module main

import os
import regex

fn main() {
input := os.get_raw_lines_joined()

mut re := regex.regex_opt('mul\\(\\d+,\\d+\\)')!
list := re.find_all_str(input)

mut sum := 0
for l in list {
a, b := l.trim('mul()').split_once(',')?
sum += a.int() * b.int()
}

println(sum)
}
33 changes: 33 additions & 0 deletions 2024/03/krotki/part2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module main

import os
import regex

fn main() {
input := os.get_raw_lines_joined()

mut re := regex.regex_opt(r"(do\(\))|(don't\(\))|(mul\(\d+,\d+\))")!
list := re.find_all_str(input)

mut is_enabled := true
mut sum := 0
for l in list {
match l {
'do()' {
is_enabled = true
continue
}
"don't()" {
is_enabled = false
continue
}
else {}
}
if is_enabled {
a, b := l.trim('mul()').split_once(',')?
sum += a.int() * b.int()
}
}

println(sum)
}
1 change: 1 addition & 0 deletions known/2024/01/krotki/part1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11
1 change: 1 addition & 0 deletions known/2024/01/krotki/part2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
31
1 change: 1 addition & 0 deletions known/2024/02/krotki/part1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions known/2024/02/krotki/part2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
1 change: 1 addition & 0 deletions known/2024/03/krotki/part1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
161
1 change: 1 addition & 0 deletions known/2024/03/krotki/part2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
48

0 comments on commit 0137c6a

Please sign in to comment.