From 282bbf9f35d35aa1a07f97b9a8deab01f4891dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jab=C5=82o=C5=84ski?= Date: Tue, 3 Dec 2024 16:07:31 +0100 Subject: [PATCH 1/2] krotki days 1..3 --- 2024/01/krotki/part1.v | 27 +++++++++++++++++++++++++++ 2024/01/krotki/part2.v | 25 +++++++++++++++++++++++++ 2024/02/krotki/part1.v | 22 ++++++++++++++++++++++ 2024/02/krotki/part2.v | 27 +++++++++++++++++++++++++++ 2024/03/krotki/part1.v | 19 +++++++++++++++++++ 2024/03/krotki/part2.v | 33 +++++++++++++++++++++++++++++++++ known/2024/01/krotki/part1.out | 1 + known/2024/01/krotki/part2.out | 1 + known/2024/02/krotki/part1.out | 1 + known/2024/02/krotki/part2.out | 1 + known/2024/03/krotki/part1.out | 1 + known/2024/03/krotki/part2.out | 1 + 12 files changed, 159 insertions(+) create mode 100644 2024/01/krotki/part1.v create mode 100644 2024/01/krotki/part2.v create mode 100644 2024/02/krotki/part1.v create mode 100644 2024/02/krotki/part2.v create mode 100644 2024/03/krotki/part1.v create mode 100644 2024/03/krotki/part2.v create mode 100644 known/2024/01/krotki/part1.out create mode 100644 known/2024/01/krotki/part2.out create mode 100644 known/2024/02/krotki/part1.out create mode 100644 known/2024/02/krotki/part2.out create mode 100644 known/2024/03/krotki/part1.out create mode 100644 known/2024/03/krotki/part2.out diff --git a/2024/01/krotki/part1.v b/2024/01/krotki/part1.v new file mode 100644 index 0000000..14f6b75 --- /dev/null +++ b/2024/01/krotki/part1.v @@ -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) +} diff --git a/2024/01/krotki/part2.v b/2024/01/krotki/part2.v new file mode 100644 index 0000000..fec5aac --- /dev/null +++ b/2024/01/krotki/part2.v @@ -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) +} diff --git a/2024/02/krotki/part1.v b/2024/02/krotki/part1.v new file mode 100644 index 0000000..2a1c22b --- /dev/null +++ b/2024/02/krotki/part1.v @@ -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) +} diff --git a/2024/02/krotki/part2.v b/2024/02/krotki/part2.v new file mode 100644 index 0000000..61bbab4 --- /dev/null +++ b/2024/02/krotki/part2.v @@ -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) +} diff --git a/2024/03/krotki/part1.v b/2024/03/krotki/part1.v new file mode 100644 index 0000000..6694799 --- /dev/null +++ b/2024/03/krotki/part1.v @@ -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) +} diff --git a/2024/03/krotki/part2.v b/2024/03/krotki/part2.v new file mode 100644 index 0000000..7e64a53 --- /dev/null +++ b/2024/03/krotki/part2.v @@ -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) +} diff --git a/known/2024/01/krotki/part1.out b/known/2024/01/krotki/part1.out new file mode 100644 index 0000000..b4de394 --- /dev/null +++ b/known/2024/01/krotki/part1.out @@ -0,0 +1 @@ +11 diff --git a/known/2024/01/krotki/part2.out b/known/2024/01/krotki/part2.out new file mode 100644 index 0000000..e85087a --- /dev/null +++ b/known/2024/01/krotki/part2.out @@ -0,0 +1 @@ +31 diff --git a/known/2024/02/krotki/part1.out b/known/2024/02/krotki/part1.out new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/known/2024/02/krotki/part1.out @@ -0,0 +1 @@ +2 diff --git a/known/2024/02/krotki/part2.out b/known/2024/02/krotki/part2.out new file mode 100644 index 0000000..b8626c4 --- /dev/null +++ b/known/2024/02/krotki/part2.out @@ -0,0 +1 @@ +4 diff --git a/known/2024/03/krotki/part1.out b/known/2024/03/krotki/part1.out new file mode 100644 index 0000000..9386c22 --- /dev/null +++ b/known/2024/03/krotki/part1.out @@ -0,0 +1 @@ +161 diff --git a/known/2024/03/krotki/part2.out b/known/2024/03/krotki/part2.out new file mode 100644 index 0000000..21e72e8 --- /dev/null +++ b/known/2024/03/krotki/part2.out @@ -0,0 +1 @@ +48 From e585e5d7a82771247ac8959cf7e54118f0c76b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jab=C5=82o=C5=84ski?= Date: Wed, 4 Dec 2024 12:35:43 +0100 Subject: [PATCH 2/2] Krotki day 4 --- 2024/04/krotki/part1.v | 44 ++++++++++++++++++++++++++++++++++ 2024/04/krotki/part2.v | 32 +++++++++++++++++++++++++ known/2024/04/krotki/part1.out | 1 + known/2024/04/krotki/part2.out | 1 + 4 files changed, 78 insertions(+) create mode 100644 2024/04/krotki/part1.v create mode 100644 2024/04/krotki/part2.v create mode 100644 known/2024/04/krotki/part1.out create mode 100644 known/2024/04/krotki/part2.out diff --git a/2024/04/krotki/part1.v b/2024/04/krotki/part1.v new file mode 100644 index 0000000..2b14b48 --- /dev/null +++ b/2024/04/krotki/part1.v @@ -0,0 +1,44 @@ +module main + +import os + +fn main() { + input := os.get_lines() + + dirs := [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0], + [1, 1], + [1, -1], + [-1, 1], + [-1, -1], + ] + + xmax := input[0].len + ymax := input.len + + mut count := 0 + for y, line in input { + for x, c in line.bytes() { + if c == `X` { + dirl: for dir in dirs { + for dist, letter in 'MAS' { + d := dist + 1 + dx := x + dir[0] * d + dy := y + dir[1] * d + if dx < 0 || dx >= xmax || dy < 0 || dy >= ymax || input[dy][dx] != letter { + continue dirl + } + if d == 3 { + count++ + } + } + } + } + } + } + + println(count) +} diff --git a/2024/04/krotki/part2.v b/2024/04/krotki/part2.v new file mode 100644 index 0000000..2fcda24 --- /dev/null +++ b/2024/04/krotki/part2.v @@ -0,0 +1,32 @@ +module main + +import os + +fn main() { + input := os.get_raw_lines() + + xmax := input[0].len + ymax := input.len + + mut count := 0 + for y, line in input { + for x, c in line.bytes() { + if x <= 0 || x >= xmax - 1 || y <= 0 || y >= ymax - 1 { + continue + } + if c == `A` { + // / + diag1 := (input[y - 1][x - 1] == `M` && input[y + 1][x + 1] == `S`) + || (input[y - 1][x - 1] == `S` && input[y + 1][x + 1] == `M`) + // \ + diag2 := (input[y + 1][x - 1] == `M` && input[y - 1][x + 1] == `S`) + || (input[y + 1][x - 1] == `S` && input[y - 1][x + 1] == `M`) + if diag1 && diag2 { + count++ + } + } + } + } + + println(count) +} diff --git a/known/2024/04/krotki/part1.out b/known/2024/04/krotki/part1.out new file mode 100644 index 0000000..3c03207 --- /dev/null +++ b/known/2024/04/krotki/part1.out @@ -0,0 +1 @@ +18 diff --git a/known/2024/04/krotki/part2.out b/known/2024/04/krotki/part2.out new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/known/2024/04/krotki/part2.out @@ -0,0 +1 @@ +9