From 2ab82894dd6de6b8abd37db3f182ea9610711919 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 29 Nov 2024 19:23:57 +0100 Subject: [PATCH] Added 2017/22 part 2 --- 2017/22/22.md | 12 ++++-- 2017/22/solution.py | 94 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 6 deletions(-) diff --git a/2017/22/22.md b/2017/22/22.md index 7241329..8fd402d 100644 --- a/2017/22/22.md +++ b/2017/22/22.md @@ -130,8 +130,6 @@ infected.) Your puzzle answer was `5266`. -The first half of this puzzle is complete! It provides one gold star: \* - ## \-\-- Part Two \-\-- {#part2} As you go to remove the virus from the infected nodes, it *evolves* to @@ -248,8 +246,14 @@ Given your actual map, after `10000000` bursts of activity, *how many bursts cause a node to become infected*? (Do not count nodes that begin infected.) -Answer: +Your puzzle answer was `2511895`. -Although it hasn\'t changed, you can still [get your puzzle +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2017) and +try another puzzle. + +If you still want to see it, you can [get your puzzle input](22/input). diff --git a/2017/22/solution.py b/2017/22/solution.py index d677432..af69475 100644 --- a/2017/22/solution.py +++ b/2017/22/solution.py @@ -6,7 +6,7 @@ from fred import list2int, toGrid,nprint,get_value_in_direction, expand_grid, ge input_f = 'input' -part = 1 +part = 2 ######################################### # # # Part 1 # @@ -30,6 +30,10 @@ def goLeft(dir:str): elif dir == 'west': new_dir = (1,0) facing = 'south' + else: + print('Something is wrong') + print(dir) + print(new_dir) return new_dir,facing @@ -112,5 +116,91 @@ if part == 1: # Part 2 # # # ######################################### + +def goBack(dir:str): + + if dir == 'north': + new_dir = (1,0) + facing = 'south' + + elif dir == 'south': + new_dir = (-1,0) + facing = 'north' + + elif dir == 'east': + new_dir = (0,-1) + facing = 'west' + + elif dir == 'west': + new_dir = (0,1) + facing = 'east' + else: + print('Something is wrong') + print(dir) + print(new_dir) + + return new_dir,facing + if part == 2: - exit() + grid = toGrid(input_f) + + start = getCenter(grid) + + #nprint(grid) + + #print(start) + end = False + dir = (-1,0) + facing = 'north' + cur = start + infect = 0 + iter = 1 + nprint(grid) + while not end: + #print('-----------START-----------') + #print(cur[0]-1) + if cur[0]-1 <= 0 or cur[0]+1 >= len(grid[0]) or cur[1]+1 >= len(grid) or cur[1]-1 <= 0: + #print('Expanding') + grid = expand_grid(grid) + cur = tuple(map(lambda i, j: i + j, cur, (1,1))) + + #print(cur) + + + + if get_value_in_direction(grid,cur) == '#': + #print('infected') + + dir,facing = goRight(facing) + grid[cur[0]][cur[1]] = 'F' + #cur = (cur[0]+dir[0],cur[1]+dir[1]) + + elif get_value_in_direction(grid,cur) == 'W': + dir,facing = dir,facing + grid[cur[0]][cur[1]] = '#' + infect += 1 + + elif get_value_in_direction(grid,cur) == 'F': + dir,facing = goBack(facing) + grid[cur[0]][cur[1]] = '.' + + elif get_value_in_direction(grid,cur) == '.': + #print('not infected') + dir,facing = goLeft(facing) + grid[cur[0]][cur[1]] = 'W' + + #cur = (cur[0]+dir[0],cur[1]+dir[1]) + + + cur = (cur[0]+dir[0],cur[1]+dir[1]) + #print(cur,dir,facing) + #nprint(grid,cur,'X') + #print('-----------END-----------',iter,infect) + iter += 1 + if iter > 10000000: + end = True + elif iter % 100000 == 0: + print(iter,infect) + #input() + print(infect) +