Skip to content

Commit 7985952

Browse files
authored
2024 Day 4 (#82)
1 parent 57042f2 commit 7985952

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

2024/04/JalonSolov.v

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
3+
// vfmt off
4+
const deltas = [[-1, 0]!, [1, 0]!, [0, -1]!, [0, 1]!, [-1, -1]!, [-1, 1]!, [1, -1]!, [1, 1]!]!
5+
// vfmt on
6+
const lines = os.read_lines('words.input')!
7+
8+
fn word_found(x int, y int, dir int) bool {
9+
mut adjusted_x := x
10+
mut adjusted_y := y
11+
12+
for c in 'MAS' {
13+
adjusted_x += deltas[dir][0]
14+
adjusted_y += deltas[dir][1]
15+
16+
if adjusted_x < 0 || adjusted_y < 0 || adjusted_x >= lines[0].len || adjusted_y >= lines.len
17+
|| lines[adjusted_y][adjusted_x] != c {
18+
return false
19+
}
20+
}
21+
22+
return true
23+
}
24+
25+
fn main() {
26+
mut xmas := 0
27+
mut x_mas := 0
28+
29+
for y, line in lines {
30+
for x, c in line {
31+
match c {
32+
`X` {
33+
for dir in 0 .. deltas.len {
34+
if word_found(x, y, dir) {
35+
xmas++
36+
}
37+
}
38+
}
39+
`A` {
40+
if x > 0 && x < line.len - 1 && y > 0 && y < lines.len - 1 {
41+
if ((lines[y - 1][x - 1] == `M` && lines[y + 1][x + 1] == `S`)
42+
|| (lines[y - 1][x - 1] == `S` && lines[y + 1][x + 1] == `M`))
43+
&& ((lines[y - 1][x + 1] == `M` && lines[y + 1][x - 1] == `S`)
44+
|| (lines[y - 1][x + 1] == `S` && lines[y + 1][x - 1] == `M`)) {
45+
x_mas++
46+
}
47+
}
48+
}
49+
else {}
50+
}
51+
}
52+
}
53+
54+
println('Part 1: ${xmas}')
55+
println('Part 2: ${x_mas}')
56+
}

2024/05/pages.input

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
75,29,13
2626
75,97,47,61,53
2727
61,13,29
28-
97,13,75,29,47
28+
97,13,75,29,47

known/2024/04/JalonSolov.out

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Part 1: 18
2+
Part 2: 9

0 commit comments

Comments
 (0)