diff --git a/apollod4.py b/apollod4.py new file mode 100644 index 0000000..b3710b2 --- /dev/null +++ b/apollod4.py @@ -0,0 +1,48 @@ +with open("src/bin/d4p2_custom.txt", "r") as inputFile: + lines = inputFile.read().split("\n") + +# part 1 + +horiz, verts = 0, 0 +cols = [""]*len(lines) +for i, row in enumerate(lines): + horiz += row.count("XMAS") + row.count("SAMX") + for j, c in enumerate(row): + cols[j] += c + +for col in cols: + verts += col.count("XMAS") + col.count("SAMX") + +diags = 0 +for lineIndex, line in enumerate(lines): + if lineIndex > len(lines)-4: + break + for colIndex in range(len(line) - 3): + if lines[lineIndex][colIndex] in "SX": + downRight = "".join(lines[lineIndex+n][colIndex+n] for n in range(4)) + if downRight in {"XMAS", "SAMX"}: + diags += 1 + for colIndex in range(3, len(line)): + if lines[lineIndex][colIndex] in "SX": + downLeft = "".join(lines[lineIndex+n][colIndex-n] for n in range(4)) + if downLeft in {"XMAS", "SAMX"}: + diags += 1 + +print(horiz + verts + diags) + +# part 2 +xmases = 0 +for rowIndex, line in enumerate(lines): + for colIndex, c in enumerate(line): + if c == "A": + try: + surrounds = lines[rowIndex-1][colIndex-1] + \ + lines[rowIndex-1][colIndex+1] + \ + lines[rowIndex+1][colIndex-1] + \ + lines[rowIndex+1][colIndex+1] + if surrounds in {"SSMM", "MMSS", "MSMS", "SMSM"}: + xmases += 1 + except IndexError: + pass + +print(xmases) diff --git a/src/bin/d4p2.rs b/src/bin/d4p2.rs index 6c046ae..01ad8a5 100644 --- a/src/bin/d4p2.rs +++ b/src/bin/d4p2.rs @@ -10,7 +10,6 @@ fn solve(lines: Vec) -> SResult { grid.push(line); } let mut total = 0; - let word: Vec = "XMAS".chars().collect(); for x in 0..grid.len() { for y in 0..grid[x].len() { if grid[x][y] != 'A' { diff --git a/src/bin/d4p2_custom.txt b/src/bin/d4p2_custom.txt new file mode 100644 index 0000000..e496753 --- /dev/null +++ b/src/bin/d4p2_custom.txt @@ -0,0 +1,3 @@ + A +M M +S S \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 5a0c2ac..859536d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ pub type Pair = (T, T); pub fn next((x, y): Pair, (xvel, yvel): Pair, width: usize) -> Option { let x = x as i64 + xvel; let y = y as i64 + yvel; - if x < 0 || y < 0 || x == width as i64 || y == width as i64 { + if x < 0 || y < 0 || x >= width as i64 || y >= width as i64 { None } else { Some((x as usize, y as usize))