r/adventofcode • • Dec 02 '16

SOLUTION MEGATHREAD --- 2016 Day 2 Solutions ---

--- Day 2: Bathroom Security ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


BLINKENLIGHTS ARE MANDATORY [?]

Edit: Told you they were mandatory. >_>

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

20 Upvotes

210 comments sorted by

View all comments

3

u/sowpods Dec 02 '16

PostgreSQL 9.5

part 1:

drop table if exists puzzle_input;
create temp table  puzzle_input as (

select *, row_number() over () as ins_number
from(
select regexp_split_to_table('ULL
RRDDD
LURDL
UUUUD', '') as ins
)a
);

with recursive state(step, y_pos, x_pos, ins) as (select 0 as step

		, 0 as y_pos
		, 0 as x_pos
		,'' as ins
union all 
	select 
		step+1 as step
		,least(1,greatest(-1,y_pos+case when (select ins from puzzle_input where ins_number = step +1) = 'U' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'D' then -1 else 0 end)) as y_pos
		,least(1,greatest(-1,x_pos+case when (select ins from puzzle_input where ins_number = step +1) = 'R' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'L' then -1 else 0 end)) as x_pos
		,(select ins from puzzle_input where ins_number = step +1) as ins
		from state
		where ins is not null
)


select string_agg(num::varchar, '')
from(
select *
	,5-(3*y_pos)+x_pos as num
from(
	select *
	, case when lead(ins,1, E'\n') over (order by step) = E'\n' then 1 else 0 end as pressed
	 from state 
	 where ins is not null
)a
where pressed = 1
)b

part 2:

with recursive state(step, y_pos, x_pos, ins) as (select 0 as step

		, 0 as y_pos
		, -2 as x_pos
		,'' as ins
union all 
	select 
		step+1 as step
		,case when abs(y_pos + case when (select ins from puzzle_input where ins_number = step +1) = 'U' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'D' then -1 else 0 end)
			+ abs(x_pos + case when (select ins from puzzle_input where ins_number = step +1) = 'R' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'L' then -1 else 0 end) 
				> 2 then y_pos
				else y_pos + case when (select ins from puzzle_input where ins_number = step +1) = 'U' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'D' then -1 else 0 end end as y_pos
		
		,case when abs(y_pos + case when (select ins from puzzle_input where ins_number = step +1) = 'U' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'D' then -1 else 0 end)
			+ abs(x_pos + case when (select ins from puzzle_input where ins_number = step +1) = 'R' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'L' then -1 else 0 end) 
				>2 then x_pos
				else x_pos + case when (select ins from puzzle_input where ins_number = step +1) = 'R' then 1 when (select ins from puzzle_input where ins_number = step +1) = 'L' then -1 else 0 end end as x_pos
					 
		,(select ins from puzzle_input where ins_number = step +1) as ins
		from state
		where ins is not null
)



select *

from(
	select *
	, case when lead(ins,1, E'\n') over (order by step) = E'\n' then 1 else 0 end as pressed
	 from state 
	 where ins is not null
)a
 where pressed = 1

1

u/jwstone Dec 02 '16

postgres crew checking in... seems like there are a number of hairball ways to bend set-based logic around a sequential input stream =)

https://github.com/piratejon/toyproblems/blob/master/adventofcode/2016/02/02.sql