commit f4c675a02197ab82b04ceb3f6f40b15f292875a9 (patch)
parent 8d65bf401ae1870e22179b8fdcad61fb21eadaec
Author: Alex Karle <alex@alexkarle.com>
Date: Sun, 8 Dec 2024 02:41:00 +0100
2023: Finish Day 3
I guess I implemented part one last year and never committed it?
Felt bad having a half completed puzzle, so did it from scratch :)
Diffstat:
4 files changed, 100 insertions(+), 38 deletions(-)
diff --git a/2023/03/1.go b/2023/03/1.go
@@ -1,35 +0,0 @@
-package main
-
-import (
- "bufio"
- "fmt"
- "log"
- "os"
- "strings"
-)
-
-func main() {
- fname := "input.txt"
- if len(os.Args) > 1 {
- fname = os.Args[1]
- }
- f, err := os.Open(fname)
- if err != nil {
- log.Fatal(err)
- }
- defer f.Close()
-
- grid := [][]string{}
- scanner := bufio.NewScanner(f)
- for scanner.Scan() {
- line := scanner.Text()
- row := strings.Split(line, "")
- fmt.Println(row)
- grid = append(grid, [][]string{row}...)
- }
- fmt.Println(grid[0][0])
-
- if err := scanner.Err(); err != nil {
- log.Fatal(err)
- }
-}
diff --git a/2023/03/1.py b/2023/03/1.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+import sys
+
+G = []
+for l in sys.stdin:
+ chars = [c for c in l.strip()]
+ G.append(chars)
+
+def issym(c):
+ return c is not None and c != '.' and not c.isdigit()
+
+def safeget(i, j):
+ if i > 0 and j > 0 and i < len(G) and j < len(G[0]):
+ return G[i][j]
+ return None
+
+def nearsym(i, j):
+ return (
+ issym(safeget(i-1,j-1)) or
+ issym(safeget(i-1,j)) or
+ issym(safeget(i-1,j+1)) or
+ issym(safeget(i,j-1)) or
+ issym(safeget(i,j)) or
+ issym(safeget(i,j+1)) or
+ issym(safeget(i+1,j-1)) or
+ issym(safeget(i+1,j)) or
+ issym(safeget(i+1,j+1))
+ )
+
+tot = 0
+for i in range(len(G)):
+ numstr = ''
+ sym = False
+ for j in range(len(G[0])):
+ if G[i][j].isdigit():
+ numstr += G[i][j]
+ if not sym:
+ sym = nearsym(i, j)
+ else:
+ if numstr and sym:
+ tot += int(numstr)
+ numstr = ''
+ sym = False
+ if numstr and sym:
+ tot += int(numstr)
+
+print(tot)
diff --git a/2023/03/2.py b/2023/03/2.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python3
+import sys
+from collections import defaultdict
+
+G = []
+for l in sys.stdin:
+ chars = [c for c in l.strip()]
+ G.append(chars)
+
+def safeget(i, j):
+ if i > 0 and j > 0 and i < len(G) and j < len(G[0]):
+ return G[i][j]
+ return None
+
+def getgear(i, j):
+ locs = [
+ (i-1,j-1),
+ (i-1,j),
+ (i-1,j+1),
+ (i,j-1),
+ (i,j),
+ (i,j+1),
+ (i+1,j-1),
+ (i+1,j),
+ (i+1,j+1),
+ ]
+ for loc in locs:
+ if safeget(loc[0], loc[1]) == '*':
+ return loc
+ return None
+
+gearnums = defaultdict(list)
+for i in range(len(G)):
+ numstr = ''
+ sym = None
+ for j in range(len(G[0])):
+ if G[i][j].isdigit():
+ numstr += G[i][j]
+ if not sym:
+ sym = getgear(i, j)
+ else:
+ if numstr and sym:
+ gearnums[sym].append(int(numstr))
+ numstr = ''
+ sym = None
+ if numstr and sym:
+ gearnums[sym].append(int(numstr))
+
+tot = 0
+for gear in gearnums:
+ if len(gearnums[gear]) == 2:
+ tot += gearnums[gear][0] * gearnums[gear][1]
+print(tot)
diff --git a/2023/03/go.mod b/2023/03/go.mod
@@ -1,3 +0,0 @@
-module alexkarle.com/aoc-2023-d3
-
-go 1.23.1