diff --git a/2025/05/.gitkeep b/2025/05/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/2025/05/dy-tea.v b/2025/05/dy-tea.v new file mode 100644 index 0000000..b219944 --- /dev/null +++ b/2025/05/dy-tea.v @@ -0,0 +1,51 @@ +import os +import arrays + +lines := os.read_lines('ingredients.input')! +mut sep_idx := 0 +for i, line in lines { + if line == '' { + sep_idx = i + break + } +} +ranges := lines[..sep_idx].map(fn (x string) []u64 { + left, right := x.split_once('-') or { panic('invalid input') } + return [left.u64(), right.u64()] +}) +available := lines[sep_idx + 1..].map(|x| x.u64()) + +// part 1 +mut sum := u64(0) +for id in available { + for rn in ranges { + left, right := rn[0], rn[1] + if id >= left && id <= right { + sum++ + break + } + } +} +println(sum) + +// part 2 +mut merged := [][]u64{} +for rn in ranges { + mut nl := rn[0] + mut nr := rn[1] + mut removed := []int{} + for i, ex in merged { + el, er := ex[0], ex[1] + if nl <= er + 1 && nr >= el - 1 { + nl = if nl < el { nl } else { el } + nr = if nr > er { nr } else { er } + removed << i + } + } + for i := removed.len - 1; i >= 0; i-- { + merged.delete(removed[i]) + } + merged << [nl, nr] +} +sum = arrays.sum(merged.map(|arr| arr[1] - arr[0] + 1))! +println(sum) diff --git a/2025/05/ingredients.input b/2025/05/ingredients.input new file mode 100644 index 0000000..2e9078d --- /dev/null +++ b/2025/05/ingredients.input @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 diff --git a/2025/06/.gitkeep b/2025/06/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/2025/06/dy-tea.v b/2025/06/dy-tea.v new file mode 100644 index 0000000..7f181c2 --- /dev/null +++ b/2025/06/dy-tea.v @@ -0,0 +1,53 @@ +import os +import regex as re +import arrays + +mut multiple_spaces := re.regex_opt(' +')! +file := os.read_file('worksheet.input')!.split_any('\n\r') +mut lines := [][]string{} +for line in file { + lines << multiple_spaces.split(line).filter(|s| s != '') +} +ops := lines.last() + +// part 1 +mut sum := u64(0) +for i in 0 .. ops.len { + mut tmp := lines[0][i].u64() + if ops[i] == '+' { + for j in 1 .. lines.len - 1 { + tmp += lines[j][i].u64() + } + } else { + for j in 1 .. lines.len - 1 { + tmp *= lines[j][i].u64() + } + } + sum += tmp +} +println(sum) + +// part 2 +sum = 0 +file_last := file.last() +mut digits := []u64{} +for j := file_last.len - 1; j >= 0; j-- { + curr := file_last[j] + mut st := '' + for i in 0 .. file.len - 1 { + st += file[i][j].ascii_str() + } + dig := st.replace(' ', '').u64() + if dig != 0 { + digits << dig + } + if curr != ` ` { + if curr == `+` { + sum += arrays.sum[u64](digits)! + } else { + sum += arrays.fold[u64, u64](digits, 1, |acc, elem| acc * elem) + } + digits.clear() + } +} +println(sum) diff --git a/2025/06/worksheet.input b/2025/06/worksheet.input new file mode 100644 index 0000000..337b837 --- /dev/null +++ b/2025/06/worksheet.input @@ -0,0 +1,4 @@ +123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * + diff --git a/2025/07/.gitkeep b/2025/07/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/2025/07/dy-tea.v b/2025/07/dy-tea.v new file mode 100644 index 0000000..5045efe --- /dev/null +++ b/2025/07/dy-tea.v @@ -0,0 +1,43 @@ +import os +import arrays + +lines := os.read_lines('manifolds.input')! +s_index := lines[0].index('S') or { panic('S not found') } + +// part 1 +mut x_indices := [s_index] +mut count := u64(0) +for y in 0 .. lines.len { + mut x_after := []int{} + for x in x_indices { + if lines[y][x] == `^` { + count++ + x_after << [x - 1, x + 1] + } else { + x_after << x + } + } + mut seen := map[int]bool{} + for pos in x_after { + seen[pos] = true + } + x_indices = seen.keys() +} +println(count) + +// part 2 +mut counts := map[int]u64{} +counts[s_index] = 1 +for y in 0 .. lines.len { + mut next := map[int]u64{} + for x, val in counts { + if lines[y][x] == `^` { + next[x - 1] += val + next[x + 1] += val + } else { + next[x] += val + } + } + counts = next.move() +} +println(arrays.sum(counts.values()) or { panic('Failed to sum values') }) diff --git a/2025/07/manifolds.input b/2025/07/manifolds.input new file mode 100644 index 0000000..57a2466 --- /dev/null +++ b/2025/07/manifolds.input @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... diff --git a/2025/08/.gitkeep b/2025/08/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/2025/08/dy-tea.v b/2025/08/dy-tea.v new file mode 100644 index 0000000..a2f1c38 --- /dev/null +++ b/2025/08/dy-tea.v @@ -0,0 +1,133 @@ +import os +import arrays + +struct Point { + x int + y int + z int +} + +struct Pair { + i int + j int + dist i64 +} + +struct Union { +mut: + parent []int + rank []int +} + +fn Union.new(n int) Union { + return Union{ + parent: []int{len: n, init: index} + rank: []int{len: n, init: 0} + } +} + +fn (mut u Union) find(x int) int { + if u.parent[x] != x { + u.parent[x] = u.find(u.parent[x]) + } + return u.parent[x] +} + +fn (mut u Union) union(p Pair) bool { + x := u.find(p.i) + y := u.find(p.j) + match true { + x == y { + return false + } + u.rank[x] < u.rank[y] { + u.parent[x] = y + } + u.rank[x] > u.rank[y] { + u.parent[y] = x + } + else { + u.parent[y] = x + u.rank[x]++ + } + } + return true +} + +fn distance(p1 Point, p2 Point) i64 { + dx := i64(p1.x - p2.x) + dy := i64(p1.y - p2.y) + dz := i64(p1.z - p2.z) + return dx * dx + dy * dy + dz * dz +} + +fn dfs(node int, graph map[int][]int, mut visited map[int]bool) int { + visited[node] = true + mut size := 1 + for neighbor in graph[node] { + if neighbor !in visited { + size += dfs(neighbor, graph, mut visited) + } + } + return size +} + +fn make_pairs(points []Point) []Pair { + mut pairs := []Pair{} + for i in 0 .. points.len { + for j in i + 1 .. points.len { + dist := distance(points[i], points[j]) + pairs << Pair{ + i: i + j: j + dist: dist + } + } + } + return pairs.sorted(|a, b| a.dist < b.dist) +} + +lines := os.read_lines('positions.input')! +points := lines.map(fn (line string) Point { + parts := line.split(',') + return Point{ + x: parts[0].int() + y: parts[1].int() + z: parts[2].int() + } +}) + +// part 1 +pairs := make_pairs(points) +mut graph := map[int][]int{} +mut u := Union.new(points.len) +max_pairs := if points.len == 20 { 10 } else { 1000 } +for idx in 0 .. max_pairs { + p := pairs[idx] + if u.union(p) { + graph[p.i] << p.j + graph[p.j] << p.i + } +} +mut sizes := []int{} +mut visited := map[int]bool{} +for i in 0 .. points.len { + if i !in visited { + sizes << dfs(i, graph, mut visited) + } +} +sorted_sizes := sizes.sorted(|a, b| a > b) +top3 := if sorted_sizes.len >= 3 { sorted_sizes[..3] } else { sorted_sizes } +println(arrays.fold[int, int](top3, 1, |acc, size| acc * size)) + +// part 2 +mut last_i := 0 +mut last_j := 0 +for pair in pairs { + if u.union(pair) { + graph[pair.i] << pair.j + graph[pair.j] << pair.i + last_i, last_j = pair.i, pair.j + } +} +println(points[last_i].x * points[last_j].x) diff --git a/2025/08/positions.input b/2025/08/positions.input new file mode 100644 index 0000000..e98a3b6 --- /dev/null +++ b/2025/08/positions.input @@ -0,0 +1,20 @@ +162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689 diff --git a/known/2025/05/dy-tea.out b/known/2025/05/dy-tea.out new file mode 100644 index 0000000..962c963 --- /dev/null +++ b/known/2025/05/dy-tea.out @@ -0,0 +1,2 @@ +3 +14 diff --git a/known/2025/06/dy-tea.out b/known/2025/06/dy-tea.out new file mode 100644 index 0000000..d9c5946 --- /dev/null +++ b/known/2025/06/dy-tea.out @@ -0,0 +1,2 @@ +4277556 +3263827 diff --git a/known/2025/07/dy-tea.out b/known/2025/07/dy-tea.out new file mode 100644 index 0000000..67bb1c8 --- /dev/null +++ b/known/2025/07/dy-tea.out @@ -0,0 +1,2 @@ +21 +40 diff --git a/known/2025/08/dy-tea.out b/known/2025/08/dy-tea.out new file mode 100644 index 0000000..45ca8ae --- /dev/null +++ b/known/2025/08/dy-tea.out @@ -0,0 +1,2 @@ +40 +25272