aoc-2018-python

Advent of Code 2018 Solutions in Python
git clone https://git.sinitax.com/sinitax/aoc-2018-python
Log | Files | Refs | README | sfeed.txt

solve.py (1655B)


      1import sys
      2sys.path.append("../common")
      3import aoc
      4
      5data = aoc.data.split("\n")
      6
      7#data = "#1 @ 1,3: 4x4","#2 @ 3,1: 4x4","#3 @ 5,5: 2x2"
      8
      9def parse_rect(l):
     10    split = l.split("@")
     11    id = int(split[0].replace("#",""))
     12    split = split[1].split(":")
     13    pos = [int(x) for x in split[0].split(",")]
     14    size = [int(x) for x in split[1].split("x")]
     15    return pos, size, id
     16
     17def create_map():
     18    global rectdata
     19    rectdata = [parse_rect(l) for l in data]
     20    msize = list([0,0])
     21    for i in range(len(rectdata)):
     22        r = rectdata[i]
     23        xm = r[0][0] + r[1][0]
     24        ym = r[0][1] + r[1][1]
     25        if i == 0 or xm > msize[0]:
     26            msize[0] = xm
     27        if i == 0 or ym > msize[1]:
     28            msize[1] = ym
     29
     30    map = [[list() for y in range(msize[1])] for x in range(msize[0])]
     31    for r in rectdata:
     32        sx = r[0][0]
     33        sy = r[0][1]
     34        for x in range(sx, sx + r[1][0]):
     35            for y in range(sy, sy + r[1][1]):
     36                map[x][y].append(r[2])
     37
     38    return map
     39
     40def solve1(args):
     41    map = create_map()
     42
     43    overlap = 0
     44    for x in range(len(map)):
     45        for y in range(len(map[0])):
     46            if len(map[x][y]) > 1:
     47                overlap += 1
     48
     49    return overlap
     50
     51def solve2(args):
     52    map = create_map()
     53
     54    overlap = set()
     55    for x in range(len(map)):
     56        for y in range(len(map[0])):
     57            if len(map[x][y]) > 1:
     58                for id in map[x][y]:
     59                    overlap.add(id)
     60
     61    for i in range(1, len(rectdata)):
     62        if i not in overlap:
     63            return i
     64
     65aoc.run(solve1, solve2, sols=[114946, 877])