From 1827398746f776a7b64af5a56d09b562a4049ae5 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 7 Dec 2025 17:13:36 +0100 Subject: [PATCH] day07 --- bob/day07/day07-p1.py | 27 ++++++++ bob/day07/day07-p2.py | 42 ++++++++++++ bob/day07/example_input | 16 +++++ bob/day07/input | 142 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 bob/day07/day07-p1.py create mode 100644 bob/day07/day07-p2.py create mode 100644 bob/day07/example_input create mode 100644 bob/day07/input diff --git a/bob/day07/day07-p1.py b/bob/day07/day07-p1.py new file mode 100644 index 0000000..ab5e3f0 --- /dev/null +++ b/bob/day07/day07-p1.py @@ -0,0 +1,27 @@ + + +def read_input(filename): + sheet = [] + for line in open(filename).read().splitlines(): + sheet.append(list(line)) + return sheet + +# filename = "day07/example_input" # 21 +filename = "day07/input" # 1560 +sheet = read_input(filename) + +splits = 0 +for y in range(0,len(sheet)-1): # skip last line + for x in range(0,len(sheet[y])): + if sheet[y][x] == "S" or sheet[y][x] == "|": + # Shoot ray down + if sheet[y+1][x] == "^": # split + sheet[y+1][x+1] = "|" + sheet[y+1][x-1] = "|" + splits += 1 + else: + sheet[y+1][x] = "|" + + + +print(f"Total number of splits: {splits}") diff --git a/bob/day07/day07-p2.py b/bob/day07/day07-p2.py new file mode 100644 index 0000000..efefe6e --- /dev/null +++ b/bob/day07/day07-p2.py @@ -0,0 +1,42 @@ +def read_input(filename): + sheet = [] + for line in open(filename).read().splitlines(): + sheet.append(list(line)) + return sheet + +# filename = "day07/example_input" # 40 +filename = "day07/input" # 1560 +sheet = read_input(filename) + +for y in range(0,len(sheet)): + for x in range(0,len(sheet[y])): + if sheet[y][x] == "S": + sheet[y][x] = 1 + if sheet[y][x] == "^": + sheet[y][x] = -1 + if sheet[y][x] == ".": + sheet[y][x] = 0 + +# "Cheat alert"; figured this out after algorithm hints on reddit +for y in range(0,len(sheet)-1): # skip last line + for x in range(0,len(sheet[y])): + + # Path starts with weight one + # Just add weight to next step + # if empty (0), weight stays same + # if path is "joined", weights are joined/added + + if sheet[y][x] > 0: + # Shoot ray down + if sheet[y+1][x] == -1: # split + sheet[y+1][x+1] += sheet[y][x] + sheet[y+1][x-1] += sheet[y][x] + else: + sheet[y+1][x] += sheet[y][x] +# Last line of sheet contains the added/joined paths, add them up + +paths = 0 +for i in sheet[-1]: + paths += i + +print(f"Total number of paths: {paths}") diff --git a/bob/day07/example_input b/bob/day07/example_input new file mode 100644 index 0000000..57a2466 --- /dev/null +++ b/bob/day07/example_input @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... diff --git a/bob/day07/input b/bob/day07/input new file mode 100644 index 0000000..d92fa69 --- /dev/null +++ b/bob/day07/input @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^.^.^.................................................................... +............................................................................................................................................. +...................................................................^.^...^................................................................... +............................................................................................................................................. +..................................................................^.....^.^.................................................................. +............................................................................................................................................. +.................................................................^.^...^.^.^................................................................. +............................................................................................................................................. +................................................................^.^...^.^...^................................................................ +............................................................................................................................................. +...............................................................^...^.....^...^............................................................... +............................................................................................................................................. +..............................................................^...^...^.^.^.^.^.............................................................. +............................................................................................................................................. +.............................................................^.^.....^.^.^.....^............................................................. +............................................................................................................................................. +............................................................^.^.^.^...^.^.^...^.^............................................................ +............................................................................................................................................. +...........................................................^.......^.^.^.^.^...^.^........................................................... +............................................................................................................................................. +..........................................................^...^.^.^.^.^...^...^.^.^.......................................................... +............................................................................................................................................. +.........................................................^.^...^.^...^.....^.^.^.^.^......................................................... +............................................................................................................................................. +........................................................^.^...^.^.^.^.^...^.^.....^.^........................................................ +............................................................................................................................................. +.......................................................^.^.^.^.^.^.^.....^.^.^.....^.^....................................................... +............................................................................................................................................. +......................................................^.^.^...^.^.^.^.^.^.^.^.^.^.....^...................................................... +............................................................................................................................................. +.....................................................^...............^.^.^.....^...^...^..................................................... +............................................................................................................................................. +....................................................^.^.^.....^.^...^.^.^.^.....^.^...^.^.................................................... +............................................................................................................................................. +...................................................^.^...^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^................................................... +............................................................................................................................................. +..................................................^.^.^.^.^.^.^...^.^.^...^.^.^.^.^...^.^.^.................................................. +............................................................................................................................................. +.................................................^.^...^.^.^.^.....^.^.^...^.^.^...^...^.^.^................................................. +............................................................................................................................................. +................................................^...^...^.^...^.^.^.^.^.^.....^.^...^.^...^.^................................................ +............................................................................................................................................. +...............................................^.....^.....^.^.^...^.^.^.^.^.....^.^.^.......^............................................... +............................................................................................................................................. +..............................................^.^...^.^.^.^.^...^...^.^.....^...^.^.^.....^.^.^.............................................. +............................................................................................................................................. +.............................................^.^.^.^...^.^.....^.^.^.^.^.^.^.^.^.^.^.^...^...^.^............................................. +............................................................................................................................................. +............................................^.^.^.^...^.....^.^.^.^...^.^.^.^.^.^...^.^.^...^.^.^............................................ +............................................................................................................................................. +...........................................^.^.^...^...^...^.^.^.^.^.^.....^...^.^.....^.^.....^.^........................................... +............................................................................................................................................. +..........................................^.....^.......^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^.........^.......................................... +............................................................................................................................................. +.........................................^.^...^.....^.^...^.^.^...^.^.^.^.......^.....^.^...^.^.^.^......................................... +............................................................................................................................................. +........................................^.^...^.^.^.^.^.^.^.......^.^.^...^.^.^...^.^.^.^...^.^...^.^........................................ +............................................................................................................................................. +.......................................^...^...^.^.^.^.......^.^.^.^.^.^.^.^.^...^.^.....^...^.^...^.^....................................... +............................................................................................................................................. +......................................^.^...^.^.^...^.^.......^.........^...^...^.^...^.^...^.^.^.^...^...................................... +............................................................................................................................................. +.....................................^.^.^.^.^.^.......^.^.^...^.^.^.^.^...^.^...^...^.^...^.^.^.^.^...^..................................... +............................................................................................................................................. +....................................^.^.^...^.^.^.^.^.^.....^...^.^.^.^.^.^.^.....^.^.^.........^.....^.^.................................... +............................................................................................................................................. +...................................^.^.^.^.....^.^.^.^.^.^.^...^.^.^.....^...^.......^.^.^.^.^.^.^...^.^.^................................... +............................................................................................................................................. +..................................^...^.^.^.^...^.^...^...^.^...^.^.^...^...^...^.^.....^.....^.^.^...^.^.^.................................. +............................................................................................................................................. +.................................^.^...^.^.^...^.^...^.^.........^.^.^.^.^.^.^.^.^...^...^.^...^.^.^.^.^.^.^................................. +............................................................................................................................................. +................................^...^.^.^.^.^.^.^.^.^.....^...^.^.........^...^.^...^...^.....^.^.....^...^.^................................ +............................................................................................................................................. +...............................^.^.^.^...^.^.........^.^.^.^.^.....^.^.....^.^...^.^.^.^.^...^.^.^.....^.^.^.^............................... +............................................................................................................................................. +..............................^.^.^.^...^.^.^.^.^.^.....^.^.^...^...^.^.^.^.^...^.......^.^.^.^.^...^.^.....^.^.............................. +............................................................................................................................................. +.............................^.^...^...^.........^...^.....^.^.^.^.^.^.^.^...^...^...^.^.^.^.^.....^.^.....^...^............................. +............................................................................................................................................. +............................^.^.^.^...^...^.^...^.^.......^...^.^.^...^.^.^.^.^...^...^.^.......^.^.^...^.^.^.^.^............................ +............................................................................................................................................. +...........................^.^.^.^...^.^.^.^.^.^.^.^...^.^...^.^...^.^.^...^...^.^.^.^.^.^.....^...^.^.^.^.^.^...^........................... +............................................................................................................................................. +..........................^.....^.^.^.....^.^.......^.^...^...^.^.^...^.....^.^.^...^...^.^...^.^.^.^.^.^...^.^...^.......................... +............................................................................................................................................. +.........................^.^.....^.....^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.....^.^...^.^.....^.^...^.....^.^...^.^......................... +............................................................................................................................................. +........................^...^...^.^.^...^...........^.^...^.^.....^.^...^...^...^.....^.^.....^.^.^.........^.^...^.^........................ +............................................................................................................................................. +.......................^.^.^.^...^.......^.^...^.^...^.^.^.....^.^.^.^.^.......^.^.^.^...^.^.....^.^.^.^...^.^.^.^...^....................... +............................................................................................................................................. +......................^.^.^.........^.^...^.^.^.....^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^...................... +............................................................................................................................................. +.....................^...^...^.^.^.^.....^...^.^...^.^.^...^.^.^...^.......^.^.^.^.^.^.^.^.^...^.^.....^...^.^.....^.^.^..................... +............................................................................................................................................. +....................^.^.^.^...^.^.^.^.^.^...^.^.^...^.....^...^...^.^...^.^.^.^...^.^.^.^.....^.^.^.^.....^...^.^.^.^...^.................... +............................................................................................................................................. +...................^.......^.....^.^.^.^.^.^...^.^...^.^.^.....^...^...^.^.^.......^.^.^...^...^...^.^.^.....^.^.^...^.^.^................... +............................................................................................................................................. +..................^.^.^.^...^.^.^.^...^.^...^.^.^...^.^.^...^.^.....^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^...^.....^.^.....^.^.................. +............................................................................................................................................. +.................^.^.^.^.^.^.^.^...^.^.^...^...^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.....^.....^.....^.^...^.^.^.^.^.....^.^................. +............................................................................................................................................. +................^.^...^.^.....^.^...^.^.^.....^...^...^.........^.^.^.^.^...^.^.^.^.....^.^.....^.^.....^.^.^.^.^.....^.^...^................ +............................................................................................................................................. +...............^.^.....^.^...^.^...^.^.....^.^.^.^.^...^.^...^...^...^.^.^.^.^.^...^...^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^............... +............................................................................................................................................. +..............^.^.....^.^.^...^.^.^.^...^.^.^.....^.^.^.^.^.^.^.^.^.^...^...^.^...^.^.......^...^.......^...^.^.....^.^.^...^.^.............. +............................................................................................................................................. +.............^.^.^.^...^.....^.^.^.^.^...^...........^...^.^.^.^.....^.^.^.^.^...^...^.^.....^.^.^.^.^...^.^...^.^...^...^.....^............. +............................................................................................................................................. +............^.....^.^.^.^.^.^...^...^.^.^...^.^...^.^.^.^.^.^.^.^.....^.^.^.^.^.^...^.^.^.^...^.^...^.....^.....^.^.....^.^.^.^.^............ +............................................................................................................................................. +...........^.^...^.^...^.^...^.^.^.....^.^.^.^...^.....^.^...^.....^.^.^...^.^.^...^.^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^.^...^........... +............................................................................................................................................. +..........^.^.......^.^.^.....^...^.^...^.^.^.^...^.^.^.^...^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^...^.^.^.^.^.....^.^.^...^.^.......... +............................................................................................................................................. +.........^.^.....^...^.^.^.^.^.^...^.....^.^.^.^.^.....^.^.^.^.^...^.......^...^.^.^.....^.^.^.^.^.^.^.^.^.^.....^.^...........^...^......... +............................................................................................................................................. +........^...^.......^.^.^.....^.^.^.^.^.^.^...^.....^...^.^.^.^.......^.^.^.........^...^...^.^...^.^.....^...^...^...^...^.....^...^........ +............................................................................................................................................. +.......^...^.^.^...^...^.^.^.^...^...^.^.^...^.^.^.....^...^.^.^.^.......^.^...^.^.^.^.^...^...^.^.^...^.^.^...^.^.^.^.^.^...^...^...^....... +............................................................................................................................................. +......^.......^.......^.^.^.^.^.^...^.^...^.....^...^.^.^.^.^.^...^.^...^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^...^.^.....^.^.^.^.^.^...... +............................................................................................................................................. +.....^.^.....^.^.^.^...^.........^.^.^.^.^.^.^...^.^...^.^.^...^.....^...^.......^.^.^.^.^.......^.^.....^.^.^.^...^.^.^.^...^.^.......^..... +............................................................................................................................................. +....^.^.^.^...^.^.^.^.^.^...^...^.^.^.....^.....^...^.^.^.^.^.^.....^.^.....^.^.^...^.^.^.^.^.^.^.^.........^.^.......^.^...^.^.^.....^.^.... +............................................................................................................................................. +...^.^.^.^.^.^.^...^...^.....^.^.^...^.^.^.^.^.^.^.^.....^.^...^...^.^.^.^.^...^.^...^.....^.^.^.^.^.^.^.^.^.^...^.^.......^.^...^.^...^.^... +............................................................................................................................................. +..^.^.^.^.^.^.^.^.......^...^.....^.^...^.^.^.^.^.^.^.^...^.....^.^.^.^...^.^.^.^...^...^.^.^.^.^.^.^.^.......^.^.^.....^.^...^.^.^...^...^.. +............................................................................................................................................. +.^.^...^...^.^.^.^.....^.^.^.^.^...^.^...^.^.^...^.^.^.^.^.....^.^.......^.....^.....^...^...^.^.^.^.^...^.^...^.^...^.^.^.^.^.^.^...^.^...^. +.............................................................................................................................................