From c364e8cf900bafd4618fdebcef1ac6b563509136 Mon Sep 17 00:00:00 2001 From: rawhide kobayashi Date: Fri, 6 Dec 2024 19:36:09 -0600 Subject: [PATCH] =?UTF-8?q?2024=20day=206=20=F0=9F=92=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2024/Day 6/gold.py | 28 +++++++ 2024/Day 6/main.py | 168 ++++++++++++++++++++++++++++++++++++++++++ 2024/Day 6/puzzle.txt | 130 ++++++++++++++++++++++++++++++++ 3 files changed, 326 insertions(+) create mode 100644 2024/Day 6/gold.py create mode 100644 2024/Day 6/main.py create mode 100644 2024/Day 6/puzzle.txt diff --git a/2024/Day 6/gold.py b/2024/Day 6/gold.py new file mode 100644 index 0000000..3268f23 --- /dev/null +++ b/2024/Day 6/gold.py @@ -0,0 +1,28 @@ +with open('puzzle.txt', 'r') as f: + lines = f.read().splitlines() + +xmas = 0 + +for y in range(len(lines)): + for x in range(len(lines[y])): + if lines[y][x] == 'A': + extra_check = [] + + if 0 < x < len(lines[y]) - 1 and 0 < y < len(lines) - 1: + if (lines[y - 1][x - 1] == 'M' and lines[y - 1][x + 1] == 'S' and + lines[y + 1][x - 1] == 'M' and lines[y + 1][x + 1] == 'S'): + xmas += 1 + + if (lines[y - 1][x - 1] == 'M' and lines[y - 1][x + 1] == 'M' and + lines[y + 1][x - 1] == 'S' and lines[y + 1][x + 1] == 'S'): + xmas += 1 + + if (lines[y - 1][x - 1] == 'S' and lines[y - 1][x + 1] == 'M' and + lines[y + 1][x - 1] == 'S' and lines[y + 1][x + 1] == 'M'): + xmas += 1 + + if (lines[y - 1][x - 1] == 'S' and lines[y - 1][x + 1] == 'S' and + lines[y + 1][x - 1] == 'M' and lines[y + 1][x + 1] == 'M'): + xmas += 1 + +print(xmas) \ No newline at end of file diff --git a/2024/Day 6/main.py b/2024/Day 6/main.py new file mode 100644 index 0000000..88b63be --- /dev/null +++ b/2024/Day 6/main.py @@ -0,0 +1,168 @@ +with open('puzzle.txt', 'r') as f: + lines = f.read().splitlines() + +import time +import copy + +guard_map = {} + +guard_orientation = '^' + +total_visits = 0 + +for line_index, line in enumerate(lines): + for column_index, column in enumerate(list(line)): + if line_index not in guard_map: + guard_map[line_index] = {} + + if column_index not in guard_map[line_index]: + guard_map[line_index][column_index] = { + 'value': line[column_index], + } + + if guard_map[line_index][column_index]['value'] == '^': + guard_map[line_index][column_index]['value'] = 'X' + guard_initial_x = column_index + guard_initial_y = line_index + +guard_x = guard_initial_x +guard_y = guard_initial_y + +#print(guard_map) + +while True: + guard_map[guard_y ][guard_x]['value'] = 'X' + + try: + #map_string = "" + #for line in guard_map: + # for column in guard_map[line]: + # if column == guard_x and line == guard_y: + # map_string += guard_orientation + # else: + # map_string += guard_map[line][column]['value'] + # map_string += "\n" + + #print(map_string) + + match guard_orientation: + case '^': + if guard_map[guard_y - 1][guard_x]['value'] == '#': + guard_orientation = '>' + + else: + guard_y -= 1 + + case '>': + if guard_map[guard_y][guard_x + 1]['value'] == '#': + guard_orientation = 'v' + + else: + guard_x += 1 + + case 'v': + if guard_map[guard_y + 1][guard_x]['value'] == '#': + guard_orientation = '<' + + else: + guard_y += 1 + + case '<': + if guard_map[guard_y][guard_x - 1]['value'] == '#': + guard_orientation = '^' + + else: + guard_x -= 1 + + except KeyError: + break + +for line in guard_map: + for column in guard_map[line]: + if guard_map[line][column]['value'] == 'X': + total_visits += 1 + +total_obstacles = 0 + +obstacle_coords = [] + +for line in guard_map: + for column in guard_map[line]: + guard_x = guard_initial_x + guard_y = guard_initial_y + obstacle_encountered = 0 + encounter_x = None + encounter_y = None + guard_orientation = '^' + init_time = time.time() + if guard_map[line][column]['value'] == 'X': + guard_map_copy = copy.deepcopy(guard_map) + guard_map_copy[line][column]['value'] = '#' + + while True: + #map_string = "" + #for line_map in guard_map_copy: + # for column_map in guard_map_copy[line_map]: + # if column_map == guard_x and line_map == guard_y: + # map_string += guard_orientation + # else: + # map_string += guard_map[line_map][column_map]['value'] + # map_string += "\n" + + #print(map_string) + guard_prev_orientation = guard_orientation + try: + match guard_orientation: + case '^': + if guard_map_copy[guard_y - 1][guard_x]['value'] == '#': + guard_orientation = '>' + + else: + guard_y -= 1 + + case '>': + if guard_map_copy[guard_y][guard_x + 1]['value'] == '#': + guard_orientation = 'v' + + else: + guard_x += 1 + + case 'v': + if guard_map_copy[guard_y + 1][guard_x]['value'] == '#': + guard_orientation = '<' + + else: + guard_y += 1 + + case '<': + if guard_map_copy[guard_y][guard_x - 1]['value'] == '#': + guard_orientation = '^' + + else: + guard_x -= 1 + + #if (abs(column - guard_x) == 1 and line - guard_y == 0) or (column - guard_x == 0 and abs(line - guard_y) == 1): + # obstacle_encountered += 1 + # if obstacle_encountered == 1: + # encounter_orientation = guard_prev_orientation + # encounter_x = guard_x + # encounter_y = guard_y +# + #if (obstacle_encountered >= 2 and guard_x == encounter_x and guard_y == encounter_y and guard_prev_orientation == encounter_orientation): + # total_obstacles += 1 + # print(line, column) + # obstacle_coords.append([line, column]) + # print(total_obstacles) + # break + + if time.time() - init_time > 0.1: + total_obstacles += 1 + print(total_obstacles) + break + + except KeyError: + break + +print(total_visits) +print(total_obstacles) +print(obstacle_coords) \ No newline at end of file diff --git a/2024/Day 6/puzzle.txt b/2024/Day 6/puzzle.txt new file mode 100644 index 0000000..d562b74 --- /dev/null +++ b/2024/Day 6/puzzle.txt @@ -0,0 +1,130 @@ +........#...........................#...........#............................................................................#.... +...........#..............................................##.#....................##.......#.............##..............#........ +...................#..#........#..#..##.................#......##..................#.....#....#.............#...........#...##.... +..........#......#..................................................#...................#........#..#.....#....................... +.........#...#......#...................................#............#...#........#.............#..#........................#..... +..##.#...........#......#.....#.#...#..............#.#......#.......#..............#.............#..................#.......#..... +...#.................................####...........#..........#..#.....#........#...........................................#.... +..........#......................#............................#....................#..............#......#....#.................#. +...#.................#.........#.........#.#.....#....#.................................................................#.#....... +.......#.......................#...........#..................................#.............#..................#......#.#......... +....................#.#.........#.........#................................#........#.#.........................#..#....#......... +........#.............#....#.....#................................##.....#..................#.....................#............... +..............#...................#..........................................#..........#........................#................ +...........#...................................................................................#..........#.........##.#.......... +............#....................#...................#.................................#.......#............#.......#............. +..........................................#.....#.............#.#.......................##......#.....#........................... +........................#...#..................#......................................................#.#..#...................... +...#.....#.....................#..........................................#................................................#.....# +..##....................................#.....................#.............#.......#............................................. +........#..................#......#................................#.......................................................#...... +.........#................#...............................##.#.....................#...........................#...........#.#.... +............#.....#..................................#.....#........#................#............................................ +.....................................................##..............#.....#.....#.......#........................................ +..........#..#..............#..#...#.......................#..........................................#........................... +...............#.............................................................#..#......#.......#..#....................#.......... +.#......................#....##.#....................#..#...#............................#............................#....#....#. +...#...................................................................................................................#.......#.. +.#....#..............#....................#......................................#.......................#....#..#................ +........#.....................#............................................#...............#...................................... +....#........#..........................................................................#..............................###.......# +........#..#.....................#..................#..................##..........#.............................................. +..........................#...#................#....................................................#..#............#............. +......#..............#................................#.....................#.................................#............#...... +...................................................#....................#.................#.....#................................# +..........................#...............#....#........................#.............................................#........... +##..............#..............................#....#.......................#.................................................#... +...........................#.............................................................#.#................#......#........#.##.. +........#..................#...#..............#..............................#.....#..........#................................... +..........#......#.##..#.....#.......##.....#......................................................#..................#........... +....#.........#................#............................................#..#...#............#.....#..#........................ +#.......#..............#.....................#..........#....#......#......................................#........#............. +.........#..........................................................................#.........#............................#...... +..........................#...#.......#...............#..............#.......................#...#................................ +.......................................................................#.........................................#...............# +...................#.....#.......................................................#.............................................#.. +...##.....................#....#................................#........#...#....#.......##...................................... +.................................................................#.......#................................#.........#............. +#..........................#.#........................#............................................#...#.........#................ +................#.#........#...................#...............................#.........#............#.........#................. +...#...................................#.....................................................................#..#......#.......... +.................................................#..........................................#....#................................ +..............##.#...............................................#......................................................##........ +...........#..............................................#.....................................##.....#.........#................ +#.......................#..........................#......#...................#.#................................................. +..............##..........................#............................................................#................#......... +.......................#...#.........#....................................................................#............#.......... +.....#.................##.................................................#...........#..............#....#.................#..... +.........#................................................#....................#.......#.........................##.#............. +...........#............#...#...................#.........#....................................................#.................. +....................#.....................................................................#..........#.#.......................... +............................##..#.......................#.......#...................#.#..........#..........................#...#. +..................#...........#.......#.............................................................#............................. +...##......................#....##..................................#...............#.#..#........................................ +..#..............................##..................................................................#.......#.................... +...#.......#..........#.#.................................................##....#...............#...............#..........#.....# +.....................................................##....................#..#..#...........................#.................... +...#......................#........#..................#.##..............#..............#....................................#..... +...............#..........................#..........................................................#.#..#..............#........ +....................##....................#..#...................................#................................................ +...........................#..............#.....................................#.........................##...................... +...............#.................................................................#.....................#.......................... +......#..........#...#...........#................................................#...#.................................#..#...... +..........#............................#.............#.....................#........#............#.........#...................... +...........#...........#..........#....#......................................................................#........#.........# +......................#...............##.................................................................................#.....#.. +...#................#........#..........#...................#............^...........#.................#.#......##.#.............# +.......................##..............#......#.....................................#.....#.........................#.#.#......... +.................#..#..........................................#...................#...#.........#................................ +..#.#.....#.............................................................#.#....................................................... +..............................#...............###...............#..........##..........................#...#..#..........##....... +.................................#.....#.........#......................................................#................#........ +...............##.#..................................................#........................#...................#.........#..##. +.........#.................................#......#............................................................................... +..................#......................#.#.........................................................#.....#...................... +.#..................#.............................#............................................................................... +...................#...................#.#........#......#...........................#......................#..........#.......... +.............#...................................................................................................#................ +..........................#................................................#........#..#.......................................... +.#........................................................#.......#......#..#..................................................... +..................##..............#.............#................#...............................#......#......#........#....#.#.. +#.............#....................................#.....................................................#........................ +............................#................................................................#....#.....#................#........ +.........#.......................................................................................................................# +..#.........................................................................................#........#.#...........#.............. +...........#......................#.......#................................#...#.......#...............#.......................... +.......................#...#..#.......#..................#........#.....#.........#............................................... +..#..##...................#..........#................................................#.........#...................#............. +........................##..........................#.......#..........................#...#.#.................................... +..........................................................................................#.............#...........#............# +..............................#.............................................................................................#.#..# +.............#..........#...........................#.........................#........#.....................#............#......# +....................................#..........................................................................................#.. +.........#.................................#...........................................................................#.......... +.#..............................#......#......#.#................................#.................#......#....#..#..#...........# +.#...#................#.......#............#.........#...#..........#......#.........................#....................#....... +..............#........................#................................................................................#......... +.........#........#..#.........#........#......#..#..............................................#................#............... +...........#......................#.......#..................................#....#...#......#..........#......................... +...........#........................................................#.#...........#................#.............................. +.#...........................#...................#...#..................................................................#........# +....................................................................#...................#.....#......#...........................# +...#..#..............#......................#......#..#...............#..............#..............#..............#.............. +...........#........................#............#.#...........#.................................................................. +............#........................#.................................................#..............#.#....................#.... +............................#...........................#............................#........#..........................#........ +#................................................................#...........................#.............#.#........#.#.......#. +............#..........#..............#.......#...#................................#.........#.#................#........#....#... +...#.....................#..........#.............#....................................................#.#........................ +...............#..................................................#......................#...............#........................ +......#............................#..................#.....#...........................................................#...#.#... +#................#..........................................................................................#..........#.......#.. +.................................................................#..............#...................#...................#.#....... +...............#..............................................#.................#...............#...........................#..... +.............................#...#........................#....................#........#.....................................#.## +...............##..#.....#..#....#.#...#...........................................#.#........#........#.......#.................. +.......#........................#....#..#............................................................#.......#.................... +........#....................#........#....#.................#........................#.............#............................. +#...#...........#......................................................#...............#...........#..#..#....#................#.. +................................#........................................................#...#..#......#....#.....#....#...#...... +..#..............#............................#........#....#......#.................................................#............ \ No newline at end of file