From 0ab90cce4d65533dad63a193c5dff5791828538c Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Thu, 20 Dec 2018 16:12:48 -0500 Subject: Solve day 18 part 1 --- 2018/day18.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 2018/day18.c (limited to '2018') diff --git a/2018/day18.c b/2018/day18.c new file mode 100644 index 0000000..686ae70 --- /dev/null +++ b/2018/day18.c @@ -0,0 +1,72 @@ +#include +#include + +typedef unsigned uint; + +struct Map { + char acres[50][50]; +}; + +static uint adjacent(struct Map map, uint y, uint x, char type) { + uint count = 0; + if (y > 0) { + count += (type == map.acres[y - 1][x]); + if (x > 0) count += (type == map.acres[y - 1][x - 1]); + if (x < 49) count += (type == map.acres[y - 1][x + 1]); + } + if (x > 0) count += (type == map.acres[y][x - 1]); + if (x < 49) count += (type == map.acres[y][x + 1]); + if (y < 49) { + count += (type == map.acres[y + 1][x]); + if (x > 0) count += (type == map.acres[y + 1][x - 1]); + if (x < 49) count += (type == map.acres[y + 1][x + 1]); + } + return count; +} + +static struct Map step(struct Map prev) { + struct Map next; + for (uint y = 0; y < 50; ++y) { + for (uint x = 0; x < 50; ++x) { + if (prev.acres[y][x] == '.') { + if (adjacent(prev, y, x, '|') > 2) { + next.acres[y][x] = '|'; + } else { + next.acres[y][x] = '.'; + } + } else if (prev.acres[y][x] == '|') { + if (adjacent(prev, y, x, '#') > 2) { + next.acres[y][x] = '#'; + } else { + next.acres[y][x] = '|'; + } + } else { + if (adjacent(prev, y, x, '#') && adjacent(prev, y, x, '|')) { + next.acres[y][x] = '#'; + } else { + next.acres[y][x] = '.'; + } + } + } + } + return next; +} + +int main(void) { + struct Map map; + for (uint y = 0; y < 50; ++y) { + scanf("%50c\n", map.acres[y]); + } + + for (uint i = 0; i < 10; ++i) { + map = step(map); + } + uint wood = 0, lumber = 0; + for (uint y = 0; y < 50; ++y) { + for (uint x = 0; x < 50; ++x) { + if (map.acres[y][x] == '|') wood++; + if (map.acres[y][x] == '#') lumber++; + } + } + printf("%u\n", wood * lumber); +} -- cgit 1.4.1 follow=1'>psf/sans6x10.psf (unfollow)
Commit message (Expand)Author
2019-07-17Add ^Q to ptee for MC sequenceJune McEnroe
2019-07-17Add Adulthood RitesJune McEnroe
2019-07-16Add catgirl shottyJune McEnroe
2019-07-16Fix SGRs 90 and 100June McEnroe
2019-07-15Add up -tJune McEnroe
2019-07-15Implement scrolling regionJune McEnroe
2019-07-14Add CSI names to shotty -dJune McEnroe
2019-07-14Ignore CSI tJune McEnroe
2019-07-14Add more info to shotty man pageJune McEnroe
2019-07-14Fix OSC PT handlingJune McEnroe
2019-07-14Handle OSC in shottyJune McEnroe
2019-07-14Add shotty -dJune McEnroe
2019-07-13Add shotty -cJune McEnroe
2019-07-13Add DL to shottyJune McEnroe
2019-07-12Color html rather than bodyJune McEnroe
2019-07-12Make author consistent and update URLsJune McEnroe
2019-07-12Move to www/text.causal.agencyJune McEnroe
2019-07-12Add new causal.agency with shotty shotsJune McEnroe
2019-07-12Use -s to infer terminal sizeJune McEnroe
2019-07-12Add DCH to shottyJune McEnroe
2019-07-12Support insert mode in shottyJune McEnroe
2019-07-11Don't do carriage return on line feedJune McEnroe
2019-07-11Interpret 256color-style SGRsJune McEnroe
2019-07-11Use inline style rather than <b>, <i>, <u>June McEnroe
2019-07-11Factor out clearJune McEnroe
2019-07-11Add bright option to shottyJune McEnroe
2019-07-11Output <b>, <i>, <u> in shottyJune McEnroe
2019-07-10Ignore SM and RMJune McEnroe
2019-07-09Add shotty man page and build itJune McEnroe
2019-07-09Add up -cJune McEnroe
2019-07-09Add options for default colors to shottyJune McEnroe
2019-07-08Use char literals consistentlyJune McEnroe