summary refs log tree commit diff stats
path: root/tests/stdlib/tstrtabs.nims
blob: 3563ad0ad6c99962ffe3cd43dfcfa22bb65666e0 (plain) (blame)
1
2
3
4
5
import std/[strtabs, assertions]

static:
  let t = {"name": "John", "city": "Monaco"}.newStringTable
  doAssert "${name} lives in ${city}" % t == "John lives in Monaco"
tring.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#!/usr/bin/env python

import numpy as np
import re

split_pattern = re.compile('(x|y)=([\d]*)')
xMax = yMax = 0
with open('day13.txt') as data:
    coords = []
    for line in data:
        if line == '\n':
            break
        else:
            x, y = map(int, line.strip().split(','))
            xMax = max(xMax, x)
            yMax = max(yMax, y)
            coords += [(x, y)]
    splits = []
    for line in data:
        splits += [split_pattern.search(line.strip()).group(1, 2)]

thermal = np.zeros((xMax+1, yMax+1)).astype(bool)
for x, y in coords:
    thermal[x, y] = True

# part 1
def fold_map(split):
    # folds always fold the map in half
    axis, val = split[0], int(split[1])
    if axis == 'x':
        folded = np.flip(thermal[val+1:], 0)
        length = folded.shape[0]
        thermal[val-length:val] = thermal[val-length:val] | folded
        return thermal[:val]
    else:
        folded = np.flip(thermal[:, val+1:], 1)
        length = folded.shape[1]
        thermal[:, val-length:val] = thermal[:, val-length:val] | folded
        return thermal[:, :val]
# part 1
print(np.count_nonzero(fold_map(splits[0])))

# part 2
for split in splits:
    thermal = fold_map(split)
thermal_str = thermal.astype(str)
thermal_str[thermal] = "*"
thermal_str[~thermal] = " "
for line in thermal_str.T:
    print("".join(line))