subesokun
: subesokun
subesokun |
This solution is written in Rust. It is the solution for the first day of AoC: The Tyranny of the Rocket Equation
Simple fuel calculation by creating the sum over all masses with the mapping function floor(mass/3) - 2
. For the second star, I needed to add a recursive function.
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::path::Path;
fn read_puzzle_input(path: &Path) -> std::vec::Vec<f64> {
let file = File::open(path).unwrap();
let content = BufReader::new(&file);
let mut vec = Vec::new();
for line in content.lines() {
let number: f64 = line.unwrap().parse().unwrap();
vec.push(number);
}
return vec
}
fn calculate_fuel(mass: f64) -> f64 {
return (mass / 3.0).floor() - 2.0;
}
fn calculate_fuel_recursive(mass: f64) -> f64 {
let fuel: f64 = calculate_fuel(mass);
if fuel < 0.0 {
return 0.0
} else {
return fuel + calculate_fuel_recursive(fuel)
};
}
fn main() {
let input_path = Path::new("input.txt");
let puzzle_input = read_puzzle_input(&input_path);
let solution_part_1: f64 = puzzle_input.iter().map(|&mass| calculate_fuel(mass)).sum();
println!("Solution to part 1: {}", solution_part_1);
let solution_part_2: f64 = puzzle_input.iter().map(|&mass| calculate_fuel_recursive(mass)).sum();
println!("Solution to part 2: {}", solution_part_2);
}
No need to install and configure Rust on your host, just use VSCode and the remote development extension pack for running the code without polluting your host. It’s magic ✨
code --install-extension ms-vscode-remote.vscode-remote-extensionpack
Open folder in VSCode and re-open folder inside of the dev container by running the quick action
Select Remote-Containers: Open Folder in Container...
Run the program by executing following command in the integrated VSCode terminal
cargo run
This solution is written in Python. It is the solution for the first day of AoC: The Tyranny of the Rocket Equation
Simple fuel calculation by creating the sum over all masses with the mapping function floor(mass/3) - 2
. For the second star, I needed to add a recursive function.
import math
INPUT_FILE_NAME = 'input.txt'
puzzle_input = None
with open(INPUT_FILE_NAME) as input_file:
puzzle_input = [float(n) for n in input_file.readlines()]
def calculate_fuel(mass):
return math.floor(mass / 3) - 2
solution_part_1 = sum(map(calculate_fuel, puzzle_input))
print('Solution to part 1: %i' % (solution_part_1,))
def calculate_fuel_recursive(mass):
fuel = calculate_fuel(mass)
return 0 if fuel < 0 else fuel + calculate_fuel_recursive(fuel)
solution_part_2 = sum(map(calculate_fuel_recursive, puzzle_input))
print('Solution to part 2: %i' % (solution_part_2,))
No need to install and configure Python on your host, just use VSCode and the remote development extension pack for running the code without polluting your host. It’s magic ✨
code --install-extension ms-vscode-remote.vscode-remote-extensionpack
Open folder in VSCode and re-open folder inside of the dev container by running the quick action
Select Remote-Containers: Open Folder in Container...
Run the code by hitting the "play" button in the VSCode debugger.
This solution is written in Python. It is the solution for the first day of AoC: 1202 Program Alarm
INPUT_FILE_NAME = 'input.txt'
puzzle_input = None
with open(INPUT_FILE_NAME) as input_file:
puzzle_input = list(map(lambda val: int(val), input_file.readline().rstrip('\n').split(',')))
def run_instruction(opcode, param_1, param_2, param_3, memory):
if opcode == 1:
memory[param_3] = memory[param_1] + memory[param_2]
elif opcode == 2:
memory[param_3] = memory[param_1] * memory[param_2]
else:
raise Exception('Ooooppps')
def run_program(memory):
instruction_pointer = 0
while memory[instruction_pointer] != 99:
run_instruction(memory[instruction_pointer + 0], memory[instruction_pointer + 1], memory[instruction_pointer + 2], memory[instruction_pointer + 3], memory)
instruction_pointer += 4
return memory
memory_solution_part1 = puzzle_input.copy()
memory_solution_part1[1] = 12
memory_solution_part1[2] = 2
solution_part_1 = run_program(memory_solution_part1)
print('Solution to part 1: %i' % (solution_part_1[0],))
def find_noun_verb(output, memory):
for noun in range(0, 100):
for verb in range(0, 100):
memory_copy = memory.copy()
memory_copy[1] = noun
memory_copy[2] = verb
result = run_program(memory_copy)[0]
if result == output:
return (noun, verb)
(noun, verb) = find_noun_verb(19690720, puzzle_input)
solution_part_2 = 100 * noun + verb
print('Solution to part 2: %i' % (solution_part_2,))
No need to install and configure Python on your host, just use VSCode and the remote development extension pack for running the code without polluting your host. It’s magic ✨
code --install-extension ms-vscode-remote.vscode-remote-extensionpack
Open folder in VSCode and re-open folder inside of the dev container by running the quick action
Select Remote-Containers: Open Folder in Container...
Run the code via the integrated VSCode terminal
python solution.py
This solution is written in Python. It is the solution for the first day of AoC: Crossed Wires
Not best solution, but it works 🙈
import sys
INPUT_FILE_NAME = 'input.txt'
def parse_cable_input(line):
result = []
for instruction in line.rstrip('\n').split(','):
result.append({'op': instruction[0], 'val': int(instruction[1:])})
return result
def add_cable_position_to_map(grid_map, cable_id, cable_position, cable_steps):
if cable_position in grid_map:
for cable in grid_map[cable_position]:
if cable[0] == cable_id:
# print('Ignoring cable "%i" as it visited already the grid position' % (cable_id,))
return
grid_map[cable_position].append((cable_id, cable_steps))
else:
grid_map[cable_position] = [(cable_id, cable_steps)]
def create_cable_grid(puzzle_input):
unique_cable_id = 0
grid_map = {}
for cable_instruction_list in puzzle_input:
current_cable_id = unique_cable_id
unique_cable_id += 1
cable_position = (0, 0)
cable_steps = 1
# Warning: Ugly code ahead 🙈
for instruction in cable_instruction_list:
if instruction['op'] == 'L':
for i in range(instruction['val']):
add_cable_position_to_map(grid_map, current_cable_id, (cable_position[0] - (i + 1), cable_position[1]), cable_steps)
cable_steps += 1
cable_position = (cable_position[0] - instruction['val'], cable_position[1])
elif instruction['op'] == 'R':
for i in range(instruction['val']):
add_cable_position_to_map(grid_map, current_cable_id, (cable_position[0] + (i + 1), cable_position[1]), cable_steps)
cable_steps += 1
cable_position = (cable_position[0] + instruction['val'], cable_position[1])
elif instruction['op'] == 'U':
for i in range(instruction['val']):
add_cable_position_to_map(grid_map, current_cable_id, (cable_position[0], cable_position[1] + (i + 1)), cable_steps)
cable_steps += 1
cable_position = (cable_position[0], cable_position[1] + instruction['val'])
elif instruction['op'] == 'D':
for i in range(instruction['val']):
add_cable_position_to_map(grid_map, current_cable_id, (cable_position[0], cable_position[1] - (i + 1)), cable_steps)
cable_steps += 1
cable_position = (cable_position[0], cable_position[1] - instruction['val'])
return grid_map
def find_nearest_intersection(grip_map):
min_distance_val = sys.maxsize
for position, cables in grip_map.items():
if len(cables) == 2:
distance = abs(position[0]) + abs(position[1])
if distance < min_distance_val:
min_distance_val = distance
return min_distance_val
def find_min_steps_intersection(grip_map):
min_steps = sys.maxsize
for position, cables in grip_map.items():
if len(cables) == 2:
steps = cables[0][1] + cables[1][1]
if steps < min_steps:
min_steps = steps
return min_steps
puzzle_input = None
with open(INPUT_FILE_NAME) as input_file:
puzzle_input = [parse_cable_input(line) for line in input_file.readlines()]
grid_map = create_cable_grid(puzzle_input)
solution_part_1 = find_nearest_intersection(grid_map)
print('Solution to part 1: %i' % (solution_part_1,))
solution_part_2 = find_min_steps_intersection(grid_map)
print('Solution to part 2: %i' % (solution_part_2,))
No need to install and configure Python on your host, just use VSCode and the remote development extension pack for running the code without polluting your host. It’s magic ✨
code --install-extension ms-vscode-remote.vscode-remote-extensionpack
Open folder in VSCode and re-open folder inside of the dev container by running the quick action
Select Remote-Containers: Open Folder in Container...
Run the code via the integrated VSCode terminal
python solution.py