m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/20/a.lua
blob: ee469d116bfc4f0ecf1fbc63e3f2422196798938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
tiles = {}

current_tile_number = nil
current_tile = {}

function border(tile, horizontal, index)
    first_column = 0
    first_row = 0
    last_column = 0
    last_row = 0
    if horizontal then
        first_column = 1
        last_column = #tile[1]
        first_row = index
        last_row = index
    else
        first_column = index
        last_column = index
        first_row = 1
        last_row = #tile
    end

    b = ''
    for row = first_row, last_row do
        for column = first_column, last_column do
            b = b .. tile[row][column]
        end
    end

    if (horizontal and index > 1) or (not horizontal and index == 1) then
        b = b:reverse()
    end

    return b
end

function borders(tile)
    return {
        border(tile, true, 1),
        border(tile, true, 10),
        border(tile, false, 1),
        border(tile, false, 10)
    }
end

for line in io.lines('input.txt') do
    if line == "" and current_tile_number then
        tiles[current_tile_number] = current_tile
        current_tile = {}
    end

    space_index = line:find(' ')

    if space_index then
        current_tile_number = line:sub(space_index+1, -2)
    elseif #line > 0 then
        row = {}
        for i = 1, line:len() do
            table.insert(row, line:sub(i, i))
        end
        table.insert(current_tile, row)
    end
end

border_counts = {}
for _, tile in pairs(tiles) do
   for _, border in pairs(borders(tile)) do
       if not border_counts[border] then
            border_counts[border] = 0
        end
        border_counts[border] = border_counts[border] + 1
    end
end

corners = {}
for number,tile in pairs(tiles) do
    count_neighboorless = 0
    for _, border in pairs(borders(tile)) do
        has_flipped_neighbor = border_counts[border] > 1
        has_non_flipped_neighbor = border_counts[border:reverse()]
        if not (has_flipped_neighbor or has_non_flipped_neighbor) then
            count_neighboorless = count_neighboorless + 1
        end
    end
    if count_neighboorless == 2 then
        table.insert(corners, number)
    end
end

product = 1
for i,c in pairs(corners) do
    product = product * c
end

print(product)