From f4c675a02197ab82b04ceb3f6f40b15f292875a9 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sun, 8 Dec 2024 02:41:00 +0100 Subject: [PATCH] 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 :) --- 2023/03/1.go | 35 ----------------------------------- 2023/03/1.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2023/03/2.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2023/03/go.mod | 3 --- 4 files changed, 100 insertions(+), 38 deletions(-) delete mode 100644 2023/03/1.go create mode 100755 2023/03/1.py create mode 100755 2023/03/2.py delete mode 100644 2023/03/go.mod diff --git a/2023/03/1.go b/2023/03/1.go deleted file mode 100644 index 35799d0..0000000 --- a/2023/03/1.go +++ /dev/null @@ -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 new file mode 100755 index 0000000..f18b5d2 --- /dev/null +++ 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 new file mode 100755 index 0000000..6048e4a --- /dev/null +++ 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 deleted file mode 100644 index 995d061..0000000 --- a/2023/03/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module alexkarle.com/aoc-2023-d3 - -go 1.23.1 -- libgit2 1.8.1