add: day4
This commit is contained in:
+48
@@ -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)
|
||||
@@ -10,7 +10,6 @@ fn solve(lines: Vec<String>) -> SResult<usize> {
|
||||
grid.push(line);
|
||||
}
|
||||
let mut total = 0;
|
||||
let word: Vec<char> = "XMAS".chars().collect();
|
||||
for x in 0..grid.len() {
|
||||
for y in 0..grid[x].len() {
|
||||
if grid[x][y] != 'A' {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
A
|
||||
M M
|
||||
S S
|
||||
+1
-1
@@ -25,7 +25,7 @@ pub type Pair<T = usize> = (T, T);
|
||||
pub fn next((x, y): Pair, (xvel, yvel): Pair<i64>, width: usize) -> Option<Pair> {
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user