From 979c51dcfaf7fd3b04c764ce28b81afcaa869a36 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 19 Dec 2017 09:52:41 +0000 Subject: [PATCH] Day 19 --- advent-of-code.cabal | 8 +- data/advent19.txt | 201 ++++++++++++++++++ problems/day19.html | 165 +++++++++++++++ src/advent19/advent19.hs | 89 ++++++++ src/advent19/advent19.ipynb | 394 +++++++++++++++++++++++++++++++++++ src/advent19/sample-maze.txt | 7 + 6 files changed, 863 insertions(+), 1 deletion(-) create mode 100644 data/advent19.txt create mode 100644 problems/day19.html create mode 100644 src/advent19/advent19.hs create mode 100644 src/advent19/advent19.ipynb create mode 100644 src/advent19/sample-maze.txt diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 05e1ef7..fbfac7d 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -202,4 +202,10 @@ executable advent18b , containers , mtl , text - , megaparsec \ No newline at end of file + , megaparsec + +executable advent19 + hs-source-dirs: src/advent19 + main-is: advent19.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 diff --git a/data/advent19.txt b/data/advent19.txt new file mode 100644 index 0000000..21ac1a5 --- /dev/null +++ b/data/advent19.txt @@ -0,0 +1,201 @@ + | + +-------+ +-------+ +-+ | +-------------------------------------------+ +---------------+ +-+ +---------------------------+ + | | | | | | | | | | | | | | | + +---------------------------+ | | +---------------------------------|-|---------+ +-----------------------------------------------------------------|-----|-|-----+ | + | | | | | | | | | | | | | | | | | + | +-----------------------|---------------------------------------|-|---------|---|-----------------+ | | | +---|-+ +-------------------------+ | + | | | | | | | | | | | | | | | | | + | | +-----|-+ | +-----------------|-+ +-------|---|-+ +---------------------------------------+ | | | +-+ | +-----+ +-+ +-|-|-+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +---------------------------------------------+ | +-----------|-|-----------+ | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | +-----|---------|-----|-|---------------+ | | | | +-|---------------|-----------+ | | | | | | | | +-+ | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | +----Q----------+ | | | | | | | | | | | +-|---+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | +---------------------------+ +-|---|---------|-------------------------------+ | +---------|-|-----------|---|-----|---------|-+ | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-+ +-+ | | +-----+ | | +-|---------+ | | | | | | +-+ | | +-------|-------------|-|---------|---|-|-----|---|-----|---|-----+ | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-----|-+ | | | | +-----|-------------+ | | | | | | | | | +-----+ | | +---|-------|-----|-|-----+ | | +-|-|-|-+ | +-|---+ | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | +-----------------------------|-------|-------------|---------------|-------|-----+ | +---|-|-|-|-----|-|---+ | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-----|-----|---------------|-----|---|---|---------|-|-------------------|-------------|-|---------+ | +-------------|---|---|-------|-------|-------+ | | +---|-----|-|-------|-|-------+ | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-------|---|-|---+ | +-+ | +---------|---|-|---------|---------|---------------|---|-----|---------------------------|---|-------|-------|-------------+ | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-|---------|-----|-------+ | | | | | | +---------------|-------------|-----|-------|-------------|---|-----------|-|-----|---------------------|-|-|-------------|-+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-+ | +-|---------+ | | | | | | | | | | | +-----------|-----------|-|-+ +---------|---|---|---|-|---------------------------------|-|---|---|---+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-------------|-|-----------|-----------------|-----|---------|-----------|-------------|---|-----|-+ | | | | | | +-|-------------|---------------------|-|-+ | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-------+ | | | | | | | | +---|---+ | +-------------|-|-----------|-+ | | +-----|-----|-+ | | +-----|-----|-------|---------+ | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-------------------------------|---|-------|---------|-|-|-------|-----------------|-----|---+ | | +---------------|---|---------|-----|---------------------+ | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | +-|-----------|---------|---|-------|---------|-------------|---------------|-----|---------|---|-------------------------------+ | +-|-|---|-|---+ | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | +-|-------|-----------|-|-|-|---|---------|-|---------------|-------|-------------------|-------------|-------------+ | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | +-|-------------------|-|---|-----|-----------|-----|---------|-----------------|-------|-|---|---------|-----|-------------------|-+ | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +---|-|-----|-------|-------|-----------------------|-|---|---------|-----+ | | | | | +-+ | | | | | +-|-----|---+ | | | | | | | | +-|-------+ | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | +---|---------|-----|-------|---------|---------|-----+ | | | | | | +---+ | +-----+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | +-|---------------------|---|-|---|-----------|---|-------|-----|-----|-----|-----------|-|---|---------|---------------|-+ | | | | | | +-+ | | | | | +-+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | N | | | | | | | | | | | | | | | | + | | +-|-----|-|-------|-|-|---+ | | | | | | | | | | | | | | | | | +-----|---|---|-----+ | | | | | | +-|---|---|-------+ A | | | | | +---|-+ | | + | | | | | | | | | | | | | | | | X | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | +-|-----------------------|-------|-------------|-|-----|-----|-----|---------------------|---|---------|---|-----------------|-|-----+ | | | | | | +-----+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | +-------------|---------|-|---|-|---|---------------|-|-----|---|-------------------------|-|-----------|-|---------------|-|-------------|-|-------|-+ | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Y | | | | | | | | | | | | | | | + +-------|-----|---+ | +-----|-|-------+ | | | | | | | +-----------|-|-------------|-------------+ +-------|---------|-------------|---------|---------+ | | | +-+ | | +---|-|-----+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | +---|---------|-----|-----|-------|-------|---------------|-----+ | | | | | | | | | | | +---+ +-----|-+ | +-----------|-+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-|---|-----+ | | | | | +-------|-----+ | | | | | | | | | | | | | | | | +-|---------|-|-------|---|---------|-+ | +-------|-----|-+ | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +---+ | | | | +-+ | | | | | | +-----------|-----|-----------|-------+ | | | | | | | | | | | | | | +-----|-|-----|-------+ | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | +-------|-----|---|-|-|-------------|-|-|-------+ | +---|-|-|---------|-------+ +-+ | | +-|-|---|-+ | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-----+ | | +---------|-----+ | | +-------|-+ | | | | | | | | | +---------|-|---|-|-------|---|-----|---|-------+ +-----|-----+ | | | +-|-----|-+ | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +-------------|-------|---------------|-----|-----|-----------|-------|---|---|---|-----|-----|-----|---|-|-|---|-|-|-----|---------|-|-|-|-|-|-|-|---|-|---+ +-----|-----+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | +---|-------|-------------------+ | | | | | | | | | | | | | | | | | | | | | | | | | +-|-----------+ | | | | | | | | +-------------+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +---------|---------------|---|-------|---------------|-------|-----+ | | +---|---|-|-|---|-----|---|-|-----|---|---|---|-|-----------|-----|---|---|-+ | | | | | | | | +-|-+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | +-|-------|---------------------|-----------------|-|-----|-----------------|-----|---|-|-|---|---------------|-+ | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | +-------|-----|---------|---------------|-------|---|-|-----------|-|---|-----+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | +-|-------|---|-|-|-----+ | +-----|-------------------+ | | | | | +-|-----|-------+ | | | | | | | | | | | | | | | | | | | | +-+ | | +-|-+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +-----------|---|-------|-+ | | | | | | | | | | | | | | | | | | +---|---+ | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +-----|---|---|-----|-|---------------------|-|-----------------------------------|-----|---|-|---|-|-----|-----|-|-------|---|---------|---|---|-------|-|-----------|-|-|-|---|-+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-------------|---------------|---|-----|-|-------------------+ | | | | | | | | | | | | | +-|-|-----|-+ | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | +-------+ | | | | | | | | +-|-------------|---------|-|---|-|-----|-|-----------|-----|-|-------+ | +---------+ | +-------|-|---+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-|---|---+ | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | R | | | | | | | | | | | | | | + | | | | | | | +-|-----|---+ | | | | | | | | | | | | +-----|---------|-------|---|-----+ | | | | | | | | | | | | | | | +-|-|-+ | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +---+ | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | +-|---------------------|-------------------|-------|-|---------|---|---|---|-------------|---|-|---------|-------|---------|---------|-|-------|-|-------|-------|-----|-+ | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | +---------------------------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | J | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | +-|-|---------|---+ | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-----|-|-|-----|-------|-|---|-|-----------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | +-------------|-------|---|---+ | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-------|-----|-+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | +---|-|-----|-----+ | | | | | | +---------|---------|---|-----|---|---|-------|---|---|-------|---------|-------|-|-------|-----------|-------|---+ | | | | | +---------+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +---------|---|-------------|---------|-|---|---------------|---|---|-----|-+ | +-----|---|-|---|---|-------|-----|---------|---|-------|---|-------|-------|-|-------|---+ | | | | | +-+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-----|-+ | | | +-|-----+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | C | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +---|-+ +-------|---|---------|-|---------|-------------------|---|-----|-------|-------|---|---|-|-----|-----|-----|---|-|-|-------|---------------------|-----|-----|-+ | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-----|-|-|-|---+ | | | | | | | | | +-|-------|-----|-|-------------|---------|---|-|-----|---------------|---------------------+ | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-------|-----+ +-|-----|---|-----|---|-----|-----|---------|---|---|-|-------------------|---|---------+ | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-|-----------|-|-+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +---|-|-+ | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-+ | | | | +-|-------|---|---|-----|-|---------|-------|-----------|-----|-------------------|---|-----|-----+ | | | | | | | | | | +-----|-|--D----|---|-+ | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | +-|-------------|-|---|---|-------+ | +-------|---|-----|-----------|-----|---------------|-+ | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | +-----------|-----|-----|---------------------|-------------|-|-------+ | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | +-|---------|---|-|---|-+ | | | | | +---|-|---|-------|-------|---|-|---|---------------------+ | | | | | +-------------------------|-|---+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | +---|---|-------------------------------|-------------+ | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +---+ +-+ +---------|---------------|---|-----------------+ | | | | | | | | | | | | | | | | | | | | | | +-------|-----------|-----+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | +-|-----|-----|---|---|-|-------|-------------|-|---------|-------+ | | | | | | | | | | | | | | | | | | | | +-----+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | +---------------------|---------|-------------|-|-------------|---|-|-----------------------|---|---+ | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +---|-------------|-----|---|-|-----|-------------------|-----+ | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-+ | +-+ | | | | | | | | | +-----|-------------|---|-----|-|-------|-----------------------------|---|-----|-|-------|---------------------------|-----|-------|-------------+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-----|-|-------|---------|---------|---|-------------------|-----------|-----------|-----------------------------------|-|---------|-|-------|-----------------+ | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | +-----------|-|-----------|---|-----|-------------|-----|-----|---------+ | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | +-+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | +---------+ | | | | | | | | | | | | | | | | | | | | +-----|---------------+ | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +---|-+ +-|-------------|-------------------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +---+ +-|---|-----+ | | | | | | | | +-----------------------------|-+ | | | | | | | +-----|-|-------|-+ | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | +---|---|-|-------------|---------|-----------------------|-------|-----|-----------|-------------|-------|-|---------|-------|-------|---------+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-------|-------|-------+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | +-|-|---------|-------------|-------------|---------------------------------+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | +---|-----|-|-------|---|-|---------|-----------|-|-----------+ | | +-------------------------------|-----+ | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | +---|-|-|---------------+ | | | | | +-------------------|-+ | | | | | | | | | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-+ +-+ | | | | | | | | | | | | | | | | | | | +-+ | | +-------------+ | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + +-------+ | +-|-|-+ | | | | +-----|-|---------|-----------|---------|-|-------|---------|-----------------------------|-----------+ | | | +---------------|---+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +---------+ | | | +-|-------|-----|-----------|-----------|---|-------|-----+ | | | | | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +-|-----|-|---------------|-----|-|---|-----------------------|-----|-----+ | +-|-----|---|-----------------|---------------------+ | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | +-------------+ | +-|-+ | | | | | | | | | +-------|-----------------|-----------+ | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | +---|-|-+ | | | +-------------|-------|---|---|-------------------+ | +-----+ | | +---|---------------------|---------------------------------------|-|-----------------------------------+ + | | | | | | | | | | | | | | | | | | | | | | | | | | + +-|-------------|---+ +---|-------+ +---|-|---------------------|-----------|-------------|---|-----------------|---------------------------------------|-|-------|---------------------------+ + | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | +-------------+ | | | | | | | | +-----|---------------------|-----------------------------------------|-----------------------------------+ + | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | +---|-----|----------F----|-----|---+ | | +-+ | | | | | | | +-----------------------------------|-------------------------------------+ + | | | | | | | | | | | | | | | | | | | | | | | | | | + | +-|-----|-----------------|-----------------------------|-----------------------|-------------|---------------------|---|---------------------+ | | | +-+ | +-+ + | | | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | | | +-----------|-|-----------|-----------|---------|-|-------------|---|---------------------|---------------------+ | | | | | | | + | | | | | | | | | | | | | | | | | | | | | | | | | | + | | | | | +---|---------|-------+ | | | | | | | +---------------------------------------|---------------------------+ | | | + | | | | | | | | | | | | | | | | | | | | | | + | | | | +-----------+ | +-+ | | +---------|-------------------|---------------------+ +---------------------------+ | | | | + | | | | | | | | | | | | | | | | | | + +-+ | +-----|-----------------------|-------|---|-------+ +-+ +---|-------------------------------+ | +---+ | +-+ + | | | | | | | | | | | | + +-----+ +-+ +-----------------------+ +---+ +---------------------------------------------------------+ +-----------------------+ + \ No newline at end of file diff --git a/problems/day19.html b/problems/day19.html new file mode 100644 index 0000000..2f66538 --- /dev/null +++ b/problems/day19.html @@ -0,0 +1,165 @@ + + + + +Day 19 - Advent of Code 2017 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 38*

   0x0000|2017

+ + + +
+

--- Day 19: A Series of Tubes ---

Somehow, a network packet got lost and ended up here. It's trying to follow a routing diagram (your puzzle input), but it's confused about where to go.

+

Its starting point is just off the top of the diagram. Lines (drawn with |, -, and +) show the path it needs to take, starting by going down onto the only line connected to the top of the diagram. It needs to follow this path until it reaches the end (located somewhere within the diagram) and stop there.

+

Sometimes, the lines cross over each other; in these cases, it needs to continue going the same direction, and only turn left or right when there's no other option. In addition, someone has left letters on the line; these also don't change its direction, but it can use them to keep track of where it's been. For example:

+
     |          
+     |  +--+    
+     A  |  C    
+ F---|----E|--+ 
+     |  |  |  D 
+     +B-+  +--+ 
+
+
+

Given this diagram, the packet needs to take the following path:

+
    +
  • Starting at the only line touching the top of the diagram, it must go down, pass through A, and continue onward to the first +.
  • +
  • Travel right, up, and right, passing through B in the process.
  • +
  • Continue down (collecting C), right, and up (collecting D).
  • +
  • Finally, go all the way left through E and stopping at F.
  • +
+

Following the path to the end, the letters it sees on its path are ABCDEF.

+

The little packet looks up at you, hoping you can help it find the way. What letters will it see (in the order it would see them) if it follows the path? (The routing diagram is very wide; make sure you view it without line wrapping.)

+
+

Your puzzle answer was XYFDJNRCQA.

--- Part Two ---

The packet is curious how many steps it needs to go.

+

For example, using the same routing diagram from the example above...

+
     |          
+     |  +--+    
+     A  |  C    
+ F---|--|-E---+ 
+     |  |  |  D 
+     +B-+  +--+ 
+
+
+

...the packet would go:

+
    +
  • 6 steps down (including the first line at the top of the diagram).
  • +
  • 3 steps right.
  • +
  • 4 steps up.
  • +
  • 3 steps right.
  • +
  • 4 steps down.
  • +
  • 3 steps right.
  • +
  • 2 steps up.
  • +
  • 13 steps left (including the F it stops on).
  • +
+

This would result in a total of 38 steps.

+

How many steps does the packet need to go?

+
+

Your puzzle answer was 17450.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/src/advent19/advent19.hs b/src/advent19/advent19.hs new file mode 100644 index 0000000..98f6d21 --- /dev/null +++ b/src/advent19/advent19.hs @@ -0,0 +1,89 @@ +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} + +import Prelude hiding (Left, Right) +import Data.List +import Data.Char + +type Maze = [String] + +data Direction = Up | Down | Left | Right deriving (Show, Eq) + +data Progress = Progress { row :: Int + , column :: Int + , direction :: Direction + , letters :: String + , stepCount :: Int + } deriving (Show, Eq) + + +-- Note: assumes the maze comes with a padding border of spaces +-- all around it. Makes the "next location" checking much easier! + +main :: IO () +main = do + text <- readFile "data/advent19.txt" + let maze = lines text + let progress = navigate maze + print $ letters progress + print $ stepCount progress + + +startProgress :: Maze -> Progress +startProgress maze = Progress { row = 0, column = startCol + , direction = Down + , letters = "", stepCount = 0} + where topRow = maze!!0 + startCol = head $ elemIndices '|' topRow + +delta :: Direction -> (Int, Int) +delta Up = (-1, 0) +delta Down = ( 1, 0) +delta Left = ( 0, -1) +delta Right = ( 0, 1) + +isJunction '+' = True +isJunction _ = False + +isFinished :: Maze -> Progress -> Bool +isFinished maze progress = isSpace $ location maze (row progress) (column progress) + +location :: Maze -> Int -> Int -> Char +location maze r c = (maze!!r)!!c + + +navigate :: Maze -> Progress +navigate maze = navigate' maze progress + where progress = startProgress maze + +navigate' :: Maze -> Progress -> Progress +navigate' maze progress = + if isFinished maze progress + then progress + else navigate' maze (step maze progress) + + +step :: Maze -> Progress -> Progress +step maze progress = progress {row = r', column = c', direction = d', letters = l', stepCount = sc'} + where r = row progress + c = column progress + thisChar = location maze r c + l' = if isAlpha thisChar then (letters progress) ++ [thisChar] else letters progress + d' = if isJunction thisChar then newDirection maze progress else direction progress + (dr, dc) = delta d' + r' = r + dr + c' = c + dc + sc' = stepCount progress + 1 + +newDirection :: Maze -> Progress -> Direction +newDirection maze progress = + if d == Up || d == Down + then if isSpace leftChar then Right else Left + else if isSpace upChar then Down else Up + where d = direction progress + r = row progress + c = column progress + upChar = location maze (r - 1) c + leftChar = location maze r (c - 1) diff --git a/src/advent19/advent19.ipynb b/src/advent19/advent19.ipynb new file mode 100644 index 0000000..09dce02 --- /dev/null +++ b/src/advent19/advent19.ipynb @@ -0,0 +1,394 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}\n", + "{-# LANGUAGE OverloadedStrings #-}\n", + "{-# LANGUAGE TypeFamilies #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import Data.List\n", + "import Data.Char" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\" | \",\" | +--+ \",\" A | C \",\" F---|----E|--+ \",\" | | | D \",\" +B-+ +--+ \",\" \"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "sampleText <- readFile \"sample-maze.txt\"\n", + "sample = lines sampleText\n", + "print sample" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "type Maze = [String]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "data Direction = Up | Down | Left | Right deriving (Show, Eq)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "data Progress = Progress { row :: Int\n", + " , column :: Int\n", + " , direction :: Direction\n", + " , letters :: String\n", + " , stepCount :: Int\n", + " } deriving (Show, Eq)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "startProgress :: Maze -> Progress\n", + "startProgress maze = Progress {row = 0, column = startCol, direction = Down, letters = \"\", stepCount = 0}\n", + " where topRow = maze!!0\n", + " startCol = head $ elemIndices '|' topRow" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "delta :: Direction -> (Int, Int)\n", + "delta Up = (-1, 0)\n", + "delta Down = ( 1, 0)\n", + "delta Left = ( 0, -1)\n", + "delta Right = ( 0, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "isContinuation '|' = True\n", + "isContinuation '-' = True\n", + "isContinuation _ = False\n", + "\n", + "isJunction '+' = True\n", + "isJunction _ = False " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "location :: Maze -> Int -> Int -> Char\n", + "location maze r c = (maze!!r)!!c" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "newDirection :: Maze -> Progress -> Direction\n", + "newDirection maze progress = \n", + " if d == Up || d == Down \n", + " then if isSpace leftChar then Right else Left\n", + " else if isSpace upChar then Down else Up\n", + " where d = direction progress\n", + " r = row progress\n", + " c = column progress\n", + " upChar = location maze (r - 1) c\n", + "-- downChar = location maze (r + 1) c\n", + " leftChar = location maze r (c - 1)\n", + "-- rightChar = location maze r (c + 1)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "step :: Maze -> Progress -> Progress\n", + "step maze progress = progress {row = r', column = c', direction = d', letters = l', stepCount = sc'}\n", + " where r = row progress\n", + " c = column progress\n", + " thisChar = location maze r c\n", + " l' = if isAlpha thisChar then (letters progress) ++ [thisChar] else letters progress\n", + " d' = if isJunction thisChar then newDirection maze progress else direction progress \n", + " (dr, dc) = delta d'\n", + " r' = r + dr\n", + " c' = c + dc\n", + " sc' = stepCount progress + 1" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "isFinished :: Maze -> Progress -> Bool\n", + "isFinished maze progress = isSpace $ location maze (row progress) (column progress)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "navigate' maze progress = \n", + " if isFinished maze progress \n", + " then progress\n", + " else navigate' maze (step maze progress)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "navigate :: Maze -> Progress\n", + "navigate maze = navigate' maze progress\n", + " where progress = startProgress maze" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Progress {row = 3, column = 0, direction = Left, letters = \"ABCDEF\", stepCount = 38}" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "navigate sample" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'+'" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "sample!!5!!8" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "isJunction '|'" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Progress {row = 5, column = 8, direction = Right, letters = \"\", stepCount = 0}" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "pt = (startProgress sample) {row = 5, column = 8, direction = Right}\n", + "pt" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Up" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "newDirection sample pt" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Progress {row = 4, column = 8, direction = Up, letters = \"\", stepCount = 1}" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "step sample pt" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "part1 = letters " + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "part2 = stepCount" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent19.txt\"\n", + " let maze = lines text\n", + " let progress = navigate maze\n", + " print $ part1 progress\n", + " print $ part2 progress" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"XYFDJNRCQA\"\n", + "17450" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "navigate " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Haskell", + "language": "haskell", + "name": "haskell" + }, + "language_info": { + "codemirror_mode": "ihaskell", + "file_extension": ".hs", + "name": "haskell", + "version": "8.0.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/src/advent19/sample-maze.txt b/src/advent19/sample-maze.txt new file mode 100644 index 0000000..45d439b --- /dev/null +++ b/src/advent19/sample-maze.txt @@ -0,0 +1,7 @@ + | + | +--+ + A | C + F---|----E|--+ + | | | D + +B-+ +--+ + \ No newline at end of file -- 2.34.1