this post was submitted on 12 Dec 2025
14 points (100.0% liked)

Advent Of Code

1200 readers
2 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 12: Christmas Tree Farm

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[–] Avicenna@programming.dev 2 points 1 week ago* (last edited 1 week ago)

After reading multiple papers on stuff like polyomino and coverings etc over the weekend, I sat down to formulate an ILP approach. All the way through I had at the back of my mind "surely he would not expect people to solve something which requires reading research papers, there must be some angle to this which makes it easier". I don't think I have ever been more right in my life and I am really glad I made the obvious fail and succeed checks based on areas lol.

import numpy as np
import itertools as it
from pathlib import Path
from time import time

cwd = Path(__file__).parent.resolve()

def timing(f):
  def wrap(*args, **kw):
    ts = time()
    result = f(*args, **kw)
    te = time()
    print(f"func{f.__name__} args: {args} took: {te-ts:.4f} sec")

    return result
  return wrap

def parse_input(file_path):
  with file_path.open("r") as fp:
    data = list(map(str.strip, fp.readlines()))

  objects = []
  for i in range(6):
    i0 = data.index(f"{i}:")
    obj = np.array(list(map(list, data[i0+1:i0+4])))
    obj[obj=='#']=1
    obj[obj=='.']=0
    objects.append(obj.astype(int))

  i0 = data.index("5:")+5
  placements = []

  for line in data[i0:]:
    dims = list(map(int, line.split(':')[0].split('x')))
    nobjs = list(map(int, line.split(': ')[-1].split(' ')))
    placements.append((dims, nobjs))

  return objects, placements

@timing
def solve_problem(file_name):

  ref_objects, placements = parse_input(Path(cwd, file_name))
  areas = [np.count_nonzero(obj==1) for obj in ref_objects]

  counter_succesful = 0

  for grid_shape, nobjs in placements:
    obj_area = np.sum(np.array(nobjs)*areas)
    grid_area = np.prod(grid_shape)
    worse_area =  np.sum(np.array(nobjs)*9)

    if worse_area<=grid_area:
      counter_succesful += 1
      continue

    if obj_area>grid_area:
      continue

  return counter_succesful

if __name__ == "__main__":

  assert solve_problem("input") == 583