File tree Expand file tree Collapse file tree 2 files changed +111
-0
lines changed
src/advent_of_code/year_2024 Expand file tree Collapse file tree 2 files changed +111
-0
lines changed Original file line number Diff line number Diff line change 1+ from advent_of_code .utils .input_handling import read_input
2+ import numpy as np
3+
4+ STRING_TO_FIND = 'XMAS'
5+
6+ DIRECTIONS = [
7+ (0 , 1 ),
8+ (0 , - 1 ),
9+ (1 , 0 ),
10+ (- 1 , 0 ),
11+ (1 , 1 ),
12+ (1 , - 1 ),
13+ (- 1 , 1 ),
14+ (- 1 , - 1 )
15+ ]
16+
17+ def check_direction_safety (i ,j ,nrows ,ncols ):
18+ if (
19+ (i < 0 ) or
20+ (i >= nrows ) or
21+ (j < 0 ) or
22+ (j > ncols )
23+ ):
24+ return False
25+ else :
26+ return True
27+
28+
29+ def solve (input ):
30+ input_parsed = np .array ([list (row ) for row in input ])
31+ nrows , ncols = input_parsed .shape
32+ print (nrows , ncols )
33+ for i in range (nrows ):
34+ for j in range (ncols ):
35+ character = input_parsed [i ,j ]
36+ if character == STRING_TO_FIND [0 ]:
37+ for direction in DIRECTIONS :
38+ new_i = i + direction [0 ]
39+ new_j = j + direction [1 ]
40+ direction_is_safe = check_direction_safety (new_i ,new_j ,nrows ,ncols )
41+ if direction_is_safe
42+ return (18 , None )
43+
44+
45+ def main (input_file ):
46+ input = read_input (input_file )
47+ (result_part_1 , result_part_2 ) = solve (input )
48+ print (
49+ f"Day 04: "
50+ f" Result for part 1 is { result_part_1 } . "
51+ f" Result for part 2 is { result_part_2 } . "
52+ )
53+
54+
55+ if __name__ == "__main__" :
56+ main ()
Original file line number Diff line number Diff line change 1+ import pytest
2+ from advent_of_code .year_2024 .day_04 import (
3+ solve ,
4+ check_direction_safety ,
5+ )
6+
7+ @pytest .mark .parametrize (
8+ "test_input, expected_result" ,
9+ [
10+ [
11+ [- 1 ,- 1 ,1 ,1 ],
12+ False ,
13+ ],
14+ [
15+ [0 ,0 ,1 ,1 ],
16+ True ,
17+ ],
18+ [
19+ [2 ,0 ,1 ,1 ],
20+ False ,
21+ ],
22+ ]
23+ )
24+ def test_check_direction_safety (test_input , expected_result ):
25+ result = check_direction_safety (* test_input )
26+ assert result == expected_result
27+
28+
29+
30+ @pytest .fixture
31+ def day_04_test_input ():
32+ return [
33+ "MMMSXXMASM" ,
34+ # "MSAMXMSMSA",
35+ # "AMXSXMAAMM",
36+ # "MSAMASMSMX",
37+ # "XMASAMXAMM",
38+ # "XXAMMXXAMA",
39+ # "SMSMSASXSS",
40+ # "SAXAMASAAA",
41+ # "MAMMMXMMMM",
42+ # "MXMXAXMASX",
43+ ]
44+
45+
46+ @pytest .fixture
47+ def day_04_expected_output ():
48+ # return (18, None)
49+ return (1 , None )
50+
51+
52+ def test_solve (day_04_test_input , day_04_expected_output ):
53+ result = solve (day_04_test_input )
54+ assert result == day_04_expected_output
55+
You can’t perform that action at this time.
0 commit comments